Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't skip cross-window syncing under normal circumstances #193

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions addon/mixins/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ export default Mixin.create({

if (event.storageArea === storage && event.key === storageKey) {
if (
('hidden' in document && !document.hidden && !this._testing) ||
event.newValue === event.oldValue ||
event.newValue === JSON.stringify(this.get('content'))
) {
Expand All @@ -112,6 +111,10 @@ export default Mixin.create({
},

_save() {
Ember.run.once(this, '_doSave');
},

_doSave() {
const storage = this._storage(),
content = get(this, 'content'),
storageKey = get(this, '_storageKey'),
Expand All @@ -135,7 +138,6 @@ export default Mixin.create({

this._super(...arguments);
},

// Public API

// returns boolean
Expand Down
13 changes: 7 additions & 6 deletions tests/unit/adapters/indices-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
storageEqual,
storageDeepEqual
} from '../../helpers/storage';
import run from 'ember-runloop';

const {
getOwner
Expand All @@ -16,7 +17,7 @@ moduleFor('adapter:application', 'Unit | Adapter | indices', {
const adapter = getOwner(this).lookup('adapter:application');

['projects'].forEach(function(key) {
adapter._getIndex(key).reset();
run(adapter._getIndex(key), 'reset');
});

window.localStorage.clear();
Expand All @@ -29,7 +30,7 @@ test('it persists the index', function(assert) {

storageEqual(assert, window.localStorage['index-projects'], undefined);

adapter._addToIndex('projects', '1234');
run(adapter, '_addToIndex', 'projects', '1234');
storageDeepEqual(assert, window.localStorage['index-projects'], ['1234']);
});

Expand All @@ -39,8 +40,8 @@ test('it does not persists duplicates to index', function(assert) {

storageEqual(assert, window.localStorage['index-projects'], undefined);

adapter._addToIndex('projects', '1234');
adapter._addToIndex('projects', '1234');
run(adapter, '_addToIndex', 'projects', '1234');
run(adapter, '_addToIndex', 'projects', '1234');
storageDeepEqual(assert, window.localStorage['index-projects'], ['1234']);
});

Expand All @@ -50,9 +51,9 @@ test('it removes ids from index', function(assert) {

storageEqual(assert, window.localStorage['index-projects'], undefined);

adapter._addToIndex('projects', '1234');
run(adapter, '_addToIndex', 'projects', '1234');
storageDeepEqual(assert, window.localStorage['index-projects'], ['1234']);

adapter._removeFromIndex('projects', '1234');
run(adapter, '_removeFromIndex', 'projects', '1234');
storageDeepEqual(assert, window.localStorage['index-projects'], []);
});
2 changes: 1 addition & 1 deletion tests/unit/array-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ test('reset method restores initialContent', function(assert) {
assert.deepEqual(get(subject, 'postLikes.content'), ['martin']);

//reset
get(subject, 'postLikes').reset();
run(get(subject, 'postLikes'), 'reset');

//data is back to initial values
assert.deepEqual(get(subject, 'postLikes.content'), []);
Expand Down
26 changes: 16 additions & 10 deletions tests/unit/legacy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { moduleFor, test } from 'ember-qunit';
import {
storageDeepEqual
} from '../helpers/storage';
import run from 'ember-runloop';

import StorageObject from 'ember-local-storage/local/object';
import {
Expand Down Expand Up @@ -43,20 +44,25 @@ moduleFor('router:main', 'legacy - config', {

test('it has correct defaults', function(assert) {
assert.expect(3);

assert.equal(subject.get('settings._storageType'), 'local');
assert.equal(subject.get('settings._storageKey'), 'settings');
assert.deepEqual(subject.get('settings._initialContent'), {
token: 1234
run(function() {
assert.equal(subject.get('settings._storageType'), 'local');
assert.equal(subject.get('settings._storageKey'), 'settings');
assert.deepEqual(subject.get('settings._initialContent'), {
token: 1234
});
});
});

test('serialized content can be used', function(assert) {
assert.expect(2);

assert.equal(subject.get('settings.mapStyle'), 'dark');
storageDeepEqual(assert, window.localStorage.settings, {
mapStyle: 'dark',
token: 1234
run(function() {
assert.equal(subject.get('settings.mapStyle'), 'dark');
});
// above runloop must finish first for changes to propagate
run(function() {
storageDeepEqual(assert, window.localStorage.settings, {
mapStyle: 'dark',
token: 1234
});
});
});
77 changes: 38 additions & 39 deletions tests/unit/object-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ moduleFor('router:main', 'object - settings', {
localCache: storageFor('local-cache')
}));
subject = this.container.lookup('object:test');
// }).create();
// run(subject, 'getProperties', 'settings', 'nestedObjects', 'cache', 'localCache');
},
afterEach() {
window.localStorage.clear();
Expand All @@ -67,16 +69,16 @@ moduleFor('router:main', 'object - settings', {

test('it has correct defaults', function(assert) {
assert.expect(6);

assert.equal(get(subject, 'settings._storageType'), 'local');
assert.equal(get(subject, 'settings._storageKey'), 'storage:settings');
assert.deepEqual(get(subject, 'settings._initialContent'), {
welcomeMessageSeen: false
run(function() {
assert.equal(get(subject, 'settings._storageType'), 'local');
assert.equal(get(subject, 'settings._storageKey'), 'storage:settings');
assert.deepEqual(get(subject, 'settings._initialContent'), {
welcomeMessageSeen: false
});
assert.equal(get(subject, 'cache._storageType'), 'session');
assert.equal(get(subject, 'cache._storageKey'), 'storage:cache');
assert.deepEqual(get(subject, 'cache._initialContent'), {});
});

assert.equal(get(subject, 'cache._storageType'), 'session');
assert.equal(get(subject, 'cache._storageKey'), 'storage:cache');
assert.deepEqual(get(subject, 'cache._initialContent'), {});
});

test('it saves changes to sessionStorage', function(assert) {
Expand Down Expand Up @@ -111,40 +113,37 @@ test('it saves changes to localStorage', function(assert) {

test('it does not share data', function(assert) {
assert.expect(10);

assert.equal(get(subject, 'cache._storageType'), 'session');
assert.equal(get(subject, 'cache._storageKey'), 'storage:cache');
assert.deepEqual(get(subject, 'cache._initialContent'), {});

run(function() {
assert.equal(get(subject, 'cache._storageType'), 'session');
assert.equal(get(subject, 'cache._storageKey'), 'storage:cache');
assert.deepEqual(get(subject, 'cache._initialContent'), {});
subject.set('cache.key1', '123456');
});

assert.deepEqual(get(subject, 'cache.key1'), '123456');
assert.deepEqual(get(subject, 'cache.key1'), '123456');

assert.equal(get(subject, 'localCache._storageType'), 'local');
assert.equal(get(subject, 'localCache._storageKey'), 'storage:local-cache');
assert.deepEqual(get(subject, 'localCache._initialContent'), {});
assert.equal(get(subject, 'localCache._storageType'), 'local');
assert.equal(get(subject, 'localCache._storageKey'), 'storage:local-cache');
assert.deepEqual(get(subject, 'localCache._initialContent'), {});

assert.deepEqual(get(subject, 'cache.key1'), '123456');
assert.deepEqual(get(subject, 'cache.key1'), '123456');

run(function() {
subject.set('localCache.key1', 'abcde');
});

assert.deepEqual(get(subject, 'localCache.key1'), 'abcde');
assert.deepEqual(get(subject, 'localCache.key1'), 'abcde');

assert.deepEqual(get(subject, 'cache.key1'), '123456');
assert.deepEqual(get(subject, 'cache.key1'), '123456');
});
});

test('it updates when change events fire', function(assert) {
assert.expect(3);

// setup testing
get(subject, 'settings')._testing = true;
run(function() {
get(subject, 'settings')._testing = true;
});

assert.equal(get(subject, 'settings.changeFired'), undefined);
window.dispatchEvent(new window.StorageEvent('storage', {
run(window, 'dispatchEvent', new window.StorageEvent('storage', {
key: 'storage:settings',
newValue: '{"welcomeMessageSeen":false,"changeFired":true}',
oldValue: '{"welcomeMessageSeen":false}',
Expand All @@ -157,11 +156,11 @@ test('it updates when change events fire', function(assert) {
test('nested values get persisted', function(assert) {
assert.expect(4);

storageDeepEqual(assert, window.localStorage['storage:nested-objects'], undefined);
run(function() {
storageDeepEqual(assert, window.localStorage['storage:nested-objects'], undefined);

assert.equal(get(subject, 'nestedObjects.address.first'), null);
assert.equal(get(subject, 'nestedObjects.address.first'), null);

run(function() {
get(subject, 'nestedObjects').set('address.first', {
street: 'Somestreet 1',
city: 'A City'
Expand All @@ -188,13 +187,13 @@ test('nested values get persisted', function(assert) {
test('reset method restores initialContent', function(assert) {
assert.expect(5);

//initialContent is set properly
assert.deepEqual(get(subject, 'settings.content'), {
welcomeMessageSeen: false
});

//set new properties and overwrite others
run(function() {
//initialContent is set properly
assert.deepEqual(get(subject, 'settings.content'), {
welcomeMessageSeen: false
});

//set new properties and overwrite others
subject.set('settings.newProp', 'some-value');
subject.set('settings.welcomeMessageSeen', true);
});
Expand All @@ -204,7 +203,7 @@ test('reset method restores initialContent', function(assert) {
assert.equal(get(subject, 'settings.welcomeMessageSeen'), true);

//reset
get(subject, 'settings').reset();
run(get(subject, 'settings'), 'reset');

//data is back to initial values
assert.deepEqual(get(subject, 'settings.content'), {
Expand All @@ -216,9 +215,9 @@ test('reset method restores initialContent', function(assert) {
test('it updates _isInitialContent', function(assert) {
assert.expect(2);

assert.equal(get(subject, 'settings').isInitialContent(), true);

run(function() {
assert.equal(get(subject, 'settings').isInitialContent(), true);

subject.set('settings.welcomeMessageSeen', true);
});

Expand Down
3 changes: 3 additions & 0 deletions tests/unit/storage-for-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { moduleFor, test } from 'ember-qunit';
import {
storageDeepEqual
} from '../helpers/storage';
import run from 'ember-runloop';

import StorageObject from 'ember-local-storage/local/object';
import {
Expand Down Expand Up @@ -44,6 +45,8 @@ test('it has the correct key', function(assert) {
}));
let subject = this.container.lookup('object:test');

run(subject, 'get', 'settings._storageKey');

assert.equal(
subject.get('settings._storageKey'),
'storage:settings:post:123'
Expand Down