Skip to content

Commit

Permalink
fix: prevent call to document with isFastboot
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Mar 6, 2018
1 parent 3387855 commit ca412ec
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
4 changes: 4 additions & 0 deletions addon/services/router-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
36 changes: 33 additions & 3 deletions tests/unit/services/router-scroll-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();
Expand Down

0 comments on commit ca412ec

Please sign in to comment.