diff --git a/addon/mixins/storage.js b/addon/mixins/storage.js index 60d7d26e..3fd21bae 100644 --- a/addon/mixins/storage.js +++ b/addon/mixins/storage.js @@ -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')) ) { @@ -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'), @@ -135,7 +138,6 @@ export default Mixin.create({ this._super(...arguments); }, - // Public API // returns boolean diff --git a/tests/unit/adapters/indices-test.js b/tests/unit/adapters/indices-test.js index 81beaa8c..a68a6078 100644 --- a/tests/unit/adapters/indices-test.js +++ b/tests/unit/adapters/indices-test.js @@ -4,6 +4,7 @@ import { storageEqual, storageDeepEqual } from '../../helpers/storage'; +import run from 'ember-runloop'; const { getOwner @@ -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(); @@ -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']); }); @@ -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']); }); @@ -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'], []); }); diff --git a/tests/unit/array-test.js b/tests/unit/array-test.js index d72e22b2..5f9e6057 100644 --- a/tests/unit/array-test.js +++ b/tests/unit/array-test.js @@ -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'), []); diff --git a/tests/unit/legacy-test.js b/tests/unit/legacy-test.js index 29732858..c785269d 100644 --- a/tests/unit/legacy-test.js +++ b/tests/unit/legacy-test.js @@ -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 { @@ -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 + }); }); }); diff --git a/tests/unit/object-test.js b/tests/unit/object-test.js index ce15dd39..dea13e1b 100644 --- a/tests/unit/object-test.js +++ b/tests/unit/object-test.js @@ -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(); @@ -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) { @@ -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}', @@ -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' @@ -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); }); @@ -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'), { @@ -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); }); diff --git a/tests/unit/storage-for-test.js b/tests/unit/storage-for-test.js index 1b3eb034..0987da25 100644 --- a/tests/unit/storage-for-test.js +++ b/tests/unit/storage-for-test.js @@ -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 { @@ -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'