From ca412ecb7572a2ccf08ce56fc9a46fe1a5e72289 Mon Sep 17 00:00:00 2001 From: snewcomer <snewcomer24@gmail.com> Date: Mon, 5 Mar 2018 10:44:54 -0800 Subject: [PATCH] fix: prevent call to document with isFastboot --- addon/services/router-scroll.js | 4 +++ tests/unit/services/router-scroll-test.js | 36 +++++++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/addon/services/router-scroll.js b/addon/services/router-scroll.js index 38024d23..8cfd382d 100644 --- a/addon/services/router-scroll.js +++ b/addon/services/router-scroll.js @@ -30,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..1f496c7c 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,31 @@ 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 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();