Skip to content

Commit

Permalink
Ensure service is fastboot compatible. Specifically with using `scrol… (
Browse files Browse the repository at this point in the history
#98)

* Ensure service is fastboot compatible. Specifically with using `scrollElement`

* fix: prevent call to document with isFastboot
  • Loading branch information
snewcomer authored and RobbieTheWagner committed Mar 6, 2018
1 parent 77d7822 commit 743e28a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
14 changes: 11 additions & 3 deletions addon/services/router-scroll.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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) {
Expand Down
51 changes: 48 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,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();
Expand Down

0 comments on commit 743e28a

Please sign in to comment.