From 38a2ed377353493f8efbf79633422ae0f0d52793 Mon Sep 17 00:00:00 2001 From: Arjan Singh Date: Sun, 7 Aug 2016 18:19:24 -0700 Subject: [PATCH 1/5] Fastboot compatibility --- addon/index.js | 28 ++++++++++----- package.json | 1 + testem.js | 2 +- tests/index.html | 3 ++ tests/unit/mixins/router-scroll-test.js | 43 ++++++++++++++++++++--- tests/unit/services/router-scroll-test.js | 2 +- 6 files changed, 64 insertions(+), 15 deletions(-) diff --git a/addon/index.js b/addon/index.js index 52b45282..1528c54e 100644 --- a/addon/index.js +++ b/addon/index.js @@ -1,6 +1,8 @@ import Ember from 'ember'; +import getOwner from 'ember-getowner-polyfill'; const { + computed, get, inject, run: { next }, @@ -9,6 +11,11 @@ const { export default Ember.Mixin.create({ service: inject.service('router-scroll'), + isFastBoot: computed(function() { + const fastboot = getOwner(this).lookup('service:fastboot'); + return fastboot ? fastboot.get('isFastBoot') : false; + }), + willTransition(...args) { this._super(...args); get(this, 'service').update(); @@ -16,15 +23,20 @@ export default Ember.Mixin.create({ didTransition(transitions, ...args) { this._super(transitions, ...args); - next(() => { - let scrollPosition = get(this, 'service.position'); - let preserveScrollPosition = transitions[transitions.length - 1] - .handler.controller.get('preserveScrollPosition'); + if (get(this, 'isFastBoot')) return; + + next(() => this.updateScrollPosition(transitions)); + }, + + updateScrollPosition(transitions) { + let scrollPosition = get(this, 'service.position'); + + let preserveScrollPosition = transitions[transitions.length - 1] + .handler.controller.get('preserveScrollPosition'); - if (!preserveScrollPosition) { - window.scrollTo(scrollPosition.x, scrollPosition.y); - } - }); + if (!preserveScrollPosition) { + window.scrollTo(scrollPosition.x, scrollPosition.y); + } } }); diff --git a/package.json b/package.json index cc927c00..e30966e6 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "ember-data": "^2.6.0", "ember-disable-prototype-extensions": "^1.1.0", "ember-export-application-global": "^1.0.5", + "ember-getowner-polyfill": "1.0.1", "ember-load-initializers": "^0.5.1", "ember-resolver": "^2.0.3", "ember-welcome-page": "^1.0.1", diff --git a/testem.js b/testem.js index 26044b2f..8c56a4a5 100644 --- a/testem.js +++ b/testem.js @@ -1,7 +1,7 @@ /*jshint node:true*/ module.exports = { "framework": "qunit", - "test_page": "tests/index.html?hidepassed", + "test_page": "tests/index.html?hidepassed&nolint", "disable_watching": true, "launch_in_ci": [ "PhantomJS" diff --git a/tests/index.html b/tests/index.html index 64cb47e3..ed7d13d9 100644 --- a/tests/index.html +++ b/tests/index.html @@ -24,6 +24,9 @@ + diff --git a/tests/unit/mixins/router-scroll-test.js b/tests/unit/mixins/router-scroll-test.js index 2327e625..36cef824 100644 --- a/tests/unit/mixins/router-scroll-test.js +++ b/tests/unit/mixins/router-scroll-test.js @@ -2,11 +2,44 @@ import Ember from 'ember'; import RouterScroll from 'ember-router-scroll'; import { module, test } from 'qunit'; -module('Unit | Mixin | router scroll'); +module('mixin:router-scroll'); -// Replace this with your real tests. -test('it works', (assert) => { +test('when the application is FastBooted', (assert) => { + assert.expect(1); + + const done = assert.async(); const RouterScrollObject = Ember.Object.extend(RouterScroll); - const subject = RouterScrollObject.create(); - assert.ok(subject); + const subject = RouterScrollObject.create({ + isFastBoot: true, + updateScrollPosition() { + assert.notOk(true, 'it should not call updateScrollPosition.'); + done(); + }, + }); + + Ember.run(() => { + subject.didTransition(); + Ember.run.next(() => { + assert.ok(true, 'it should not call updateScrollPosition.'); + done(); + }); + }); +}); + +test('when the application is not FastBooted', (assert) => { + assert.expect(1); + + const done = assert.async(); + const RouterScrollObject = Ember.Object.extend(RouterScroll); + const subject = RouterScrollObject.create({ + isFastBoot: false, + updateScrollPosition() { + assert.ok(true, 'it should call updateScrollPosition.'); + done(); + }, + }); + + Ember.run(() => { + subject.didTransition(); + }); }); diff --git a/tests/unit/services/router-scroll-test.js b/tests/unit/services/router-scroll-test.js index dba33361..6b602b15 100644 --- a/tests/unit/services/router-scroll-test.js +++ b/tests/unit/services/router-scroll-test.js @@ -6,7 +6,7 @@ const { set } = Ember; -moduleFor('service:router-scroll', 'Unit | Service | router scroll', { +moduleFor('service:router-scroll', { // Specify the other units that are required for this test. // needs: ['service:foo'] }); From a00e1b9602d6721818177401ac06431618004699 Mon Sep 17 00:00:00 2001 From: Arjan Singh Date: Mon, 15 Aug 2016 15:50:01 -0700 Subject: [PATCH 2/5] pre-git@^3.10.0 --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index e30966e6..574eddea 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,8 @@ "eslint": "^2.4.0", "eslint-config-airbnb": "^6.1.0", "eslint-plugin-dollarshaveclub": "^1.0.0", - "loader.js": "^4.0.1" + "loader.js": "^4.0.1", + "pre-git": "^3.10.0" }, "keywords": [ "ember-addon", From 9b0be6214252c3ed7411c3352341164340199a18 Mon Sep 17 00:00:00 2001 From: Arjan Singh Date: Mon, 15 Aug 2016 12:45:30 -0700 Subject: [PATCH 3/5] lint --- addon/index.js | 2 +- addon/locations/router-scroll.js | 16 ++++++++-------- addon/services/router-scroll.js | 19 +++++++++++-------- package.json | 2 +- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/addon/index.js b/addon/index.js index 1528c54e..53ccaa31 100644 --- a/addon/index.js +++ b/addon/index.js @@ -24,7 +24,7 @@ export default Ember.Mixin.create({ didTransition(transitions, ...args) { this._super(transitions, ...args); - if (get(this, 'isFastBoot')) return; + if (get(this, 'isFastBoot')) { return; } next(() => this.updateScrollPosition(transitions)); }, diff --git a/addon/locations/router-scroll.js b/addon/locations/router-scroll.js index c713b6cc..76501f65 100644 --- a/addon/locations/router-scroll.js +++ b/addon/locations/router-scroll.js @@ -2,7 +2,7 @@ import Ember from 'ember'; const { get, - HistoryLocation + HistoryLocation, } = Ember; export default HistoryLocation.extend({ @@ -11,15 +11,15 @@ export default HistoryLocation.extend({ this.stateCounter = 0; }, pushState(path) { - let id = `${this.stateCounter++}`; - let state = { path, id }; + const id = `${this.stateCounter++}`; + const state = { path, id }; get(this, 'history').pushState(state, null, path); - this._previousURL = this.getURL(); + this._previousURL = this.getURL(); // eslint-disable-line no-underscore-dangle }, replaceState(path) { - let id = `${this.stateCounter++}`; - let state = { path, id }; + const id = `${this.stateCounter++}`; + const state = { path, id }; get(this, 'history').replaceState(state, null, path); - this._previousURL = this.getURL(); - } + this._previousURL = this.getURL(); // eslint-disable-line no-underscore-dangle + }, }); diff --git a/addon/services/router-scroll.js b/addon/services/router-scroll.js index 48557bbb..1b7ee130 100644 --- a/addon/services/router-scroll.js +++ b/addon/services/router-scroll.js @@ -1,3 +1,4 @@ +/* global window */ import Ember from 'ember'; const { @@ -5,7 +6,7 @@ const { set, computed, getWithDefault, - Service + Service, } = Ember; export default Service.extend({ @@ -14,21 +15,23 @@ export default Service.extend({ set(this, 'scrollMap', {}); set(this, 'key', null); }, + update() { - let scrollMap = get(this, 'scrollMap'); - let key = get(this, 'key'); + const scrollMap = get(this, 'scrollMap'); + const key = get(this, 'key'); if (key) { set(scrollMap, key, { x: window.scrollX, y: window.scrollY }); } }, - position: computed(function() { - let scrollMap = get(this, 'scrollMap'); - let stateId = get(window, 'history.state.id'); + + position: computed(function position() { + const scrollMap = get(this, 'scrollMap'); + const stateId = get(window, 'history.state.id'); set(this, 'key', stateId); - let key = getWithDefault(this, 'key', '-1'); + const key = getWithDefault(this, 'key', '-1'); return getWithDefault(scrollMap, key, { x: 0, y: 0 }); - }).volatile() + }).volatile(), }); diff --git a/package.json b/package.json index 574eddea..30355ff8 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ }, "scripts": { "build": "ember build", - "eslint": "eslint . --ignore-path .eslintignore", + "eslint": "eslint app/**/*.js addon/**/*.js tests/**/*.js --quiet --ignore-path .eslintignore", "start": "ember server", "test": "ember try:each" }, From 8ba5eb6ac05f35a4d51d32edd9410bda71c84513 Mon Sep 17 00:00:00 2001 From: Arjan Singh Date: Mon, 15 Aug 2016 15:34:00 -0700 Subject: [PATCH 4/5] [service:router-scroll] Use `get` instead of `getWithDefaults`. `getWithDefaults` returns the value (including `null`) for any key that is defined. This caused a failure in ember@1.13 because you cannot do a get lookup if the lookup value is `null`. --- addon/services/router-scroll.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addon/services/router-scroll.js b/addon/services/router-scroll.js index 1b7ee130..32b1a4e4 100644 --- a/addon/services/router-scroll.js +++ b/addon/services/router-scroll.js @@ -30,7 +30,7 @@ export default Service.extend({ const stateId = get(window, 'history.state.id'); set(this, 'key', stateId); - const key = getWithDefault(this, 'key', '-1'); + const key = get(this, 'key') || '-1'; return getWithDefault(scrollMap, key, { x: 0, y: 0 }); }).volatile(), From 5850271db25101e8bb17ad39feafe897cc6a9521 Mon Sep 17 00:00:00 2001 From: Arjan Singh Date: Mon, 15 Aug 2016 15:33:51 -0700 Subject: [PATCH 5/5] [testing] Use Chrome, not PhantomJS --- .travis.yml | 14 +++++++++++--- testem.js | 29 ++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 00c58228..f1897fbc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,14 @@ --- +sudo: required +dist: trusty language: node_js node_js: - "4" -sudo: false - cache: directories: - node_modules + - bower_components env: - EMBER_TRY_SCENARIO=default @@ -22,9 +23,16 @@ matrix: - env: EMBER_TRY_SCENARIO=ember-canary before_install: + - export CHROME_BIN=google-chrome + - export DISPLAY=:99.0 + - sh -e /etc/init.d/xvfb start + - sudo apt-get update + - sudo apt-get install -y libappindicator1 fonts-liberation + - wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb + - sudo dpkg -i google-chrome*.deb - npm config set spin false - npm install -g bower - - npm install phantomjs-prebuilt + - bower --version install: - npm install diff --git a/testem.js b/testem.js index 8c56a4a5..7fdb3e27 100644 --- a/testem.js +++ b/testem.js @@ -9,5 +9,32 @@ module.exports = { "launch_in_dev": [ "PhantomJS", "Chrome" - ] + ], + launch_in_ci: [ + 'Chrome Custom', + ], + launch_in_dev: [ + 'Chrome Custom', + ], + launchers: { + 'Chrome Custom': { + exe: [ + 'google-chrome', + '~/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', + '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', + ], + args: [ + '--user-data-dir=/tmp/testem.chrome.custom', + '--no-default-browser-check', + '--no-first-run', + '--ignore-certificate-errors', + '--test-type', + '--disable-extensions', + '--disable-web-security', + '--disable-renderer-backgrounding', + '--disable-background-timer-throttling', + ], + protocol: 'browser', + }, + }, };