diff --git a/addon/services/router-scroll.js b/addon/services/router-scroll.js index 6c9b2bed..8cfd382d 100644 --- a/addon/services/router-scroll.js +++ b/addon/services/router-scroll.js @@ -1,11 +1,15 @@ /* eslint-disable */ -import Ember from 'ember'; import Service from '@ember/service'; import { getWithDefault, computed, set, get } from '@ember/object'; - -const { getOwner, typeOf } = Ember; +import { typeOf } from '@ember/utils'; +import { getOwner } from '@ember/application'; export default Service.extend({ + isFastBoot: computed(function() { + const fastboot = getOwner(this).lookup('service:fastboot'); + return fastboot ? fastboot.get('isFastBoot') : false; + }), + scrollElement: 'window', init(...args) { @@ -26,6 +30,10 @@ export default Service.extend({ x = window.scrollX; y = window.scrollY; } else if ('#' === scrollElement.charAt(0)) { + if (get(this, 'isFastBoot')) { + return; + } + let element = document.getElementById(scrollElement.substring(1)); if (element) { diff --git a/tests/unit/services/router-scroll-test.js b/tests/unit/services/router-scroll-test.js index 5321839d..851e0236 100644 --- a/tests/unit/services/router-scroll-test.js +++ b/tests/unit/services/router-scroll-test.js @@ -3,13 +3,26 @@ import { moduleFor, test } from 'ember-qunit'; moduleFor('service:router-scroll'); -// Replace this with your real tests. test('it inits `scrollMap` and `key`', function init(assert) { const service = this.subject(); assert.deepEqual(get(service, 'scrollMap'), {}); assert.deepEqual(get(service, 'key'), null); }); +test('it inits `scrollMap` and `key` with scrollElement other than window', function init(assert) { + const service = this.subject({ scrollElement: '#other-elem' }); + assert.deepEqual(get(service, 'scrollMap'), {}); + assert.deepEqual(get(service, 'key'), null); +}); + +test('updating will not set `scrollMap` to the current scroll position if `key` is not yet set', +function scrollMapCurrentPos(assert) { + const service = this.subject(); + + service.update(); + assert.deepEqual(get(service, 'scrollMap'), { }); +}); + test('updating will set `scrollMap` to the current scroll position', function scrollMap(assert) { const service = this.subject(); @@ -19,14 +32,46 @@ test('updating will set `scrollMap` to the current scroll position', function sc assert.deepEqual(get(service, 'scrollMap'), { 123: expected }); }); -test('updating will not set `scrollMap` to the current scroll position if `key` is not yet set', +test('updating will not set `scrollMap` if scrollElement is defined', function scrollMapCurrentPos(assert) { - const service = this.subject(); + const service = this.subject({ scrollElement: '#other-elem' }); service.update(); + const expected = { x: 0, y: 0 }; + assert.deepEqual(get(service, 'position'), expected); assert.deepEqual(get(service, 'scrollMap'), { }); }); +test('updating will not set `scrollMap` if scrollElement is defined and in fastboot', +function scrollMapCurrentPos(assert) { + const otherElem = document.createElement('div'); + otherElem.setAttribute('id', 'other-elem'); + const testing = document.querySelector('#ember-testing'); + testing.appendChild(otherElem); + const service = this.subject({ scrollElement: '#other-elem', isFastBoot: true }); + window.history.replaceState({ uuid: '123' }, null); + + let expected = { x: 0, y: 0 }; + assert.deepEqual(get(service, 'position'), expected, 'position is defaulted'); + service.update(); + assert.deepEqual(get(service, 'scrollMap'), { }, 'does not set scrollMap b/c in fastboot'); +}); + +test('updating will set `scrollMap` if scrollElement is defined', +function scrollMapCurrentPos(assert) { + const otherElem = document.createElement('div'); + otherElem.setAttribute('id', 'other-elem'); + const testing = document.querySelector('#ember-testing'); + testing.appendChild(otherElem); + const service = this.subject({ scrollElement: '#other-elem' }); + window.history.replaceState({ uuid: '123' }, null); + + let expected = { x: 0, y: 0 }; + assert.deepEqual(get(service, 'position'), expected, 'position is defaulted'); + service.update(); + assert.deepEqual(get(service, 'scrollMap'), { '123': expected }, 'sets scrollMap'); +}); + test('computing the position for an existing state uuid return the coords', function existingUUID(assert) { const service = this.subject();