From af4214b2b1863d7e326a29a5d807d895f724c7f4 Mon Sep 17 00:00:00 2001 From: Manuel Wiedenmann Date: Wed, 7 Feb 2018 22:38:58 +0100 Subject: [PATCH] Implement namespace for adapter --- addon/adapters/base.js | 3 +- addon/adapters/local.js | 6 +- addon/adapters/session.js | 6 +- addon/helpers/storage.js | 3 +- tests/unit/adapters/indices-test.js | 69 ++++++++++++++- tests/unit/models/post-test.js | 126 ++++++++++++++++++++++++++++ 6 files changed, 205 insertions(+), 8 deletions(-) diff --git a/addon/adapters/base.js b/addon/adapters/base.js index 2010db67..10eeaa48 100644 --- a/addon/adapters/base.js +++ b/addon/adapters/base.js @@ -1,6 +1,7 @@ import Ember from 'ember'; import DS from 'ember-data'; import ImportExportMixin from '../mixins/adapters/import-export'; +import { _buildKey } from '../helpers/storage'; const keys = Object.keys || Ember.keys; @@ -323,7 +324,7 @@ export default JSONAPIAdapter.extend(ImportExportMixin, { }, _storageKey(type, id) { - return type + '-' + id; + return _buildKey(this, type + '-' + id); }, // Should be overwriten diff --git a/addon/adapters/local.js b/addon/adapters/local.js index 5ab99590..0e2a7057 100644 --- a/addon/adapters/local.js +++ b/addon/adapters/local.js @@ -1,6 +1,6 @@ import Ember from 'ember'; import BaseAdapter from './base'; -import { getStorage } from '../helpers/storage'; +import { getStorage, _buildKey } from '../helpers/storage'; import StorageArray from '../local/array'; const { @@ -14,8 +14,10 @@ export default BaseAdapter.extend({ const indices = get(this, '_indices'); if (!indices[type]) { + let storageKey = _buildKey(this, 'index-' + type); + indices[type] = StorageArray - .extend({ _storageKey: 'index-' + type }) + .extend({ _storageKey: storageKey }) .create(); } diff --git a/addon/adapters/session.js b/addon/adapters/session.js index 21ff8de0..26e944ef 100644 --- a/addon/adapters/session.js +++ b/addon/adapters/session.js @@ -1,6 +1,6 @@ import Ember from 'ember'; import BaseAdapter from './base'; -import { getStorage } from '../helpers/storage'; +import { getStorage, _buildKey } from '../helpers/storage'; import StorageArray from '../session/array'; const { @@ -14,8 +14,10 @@ export default BaseAdapter.extend({ const indices = get(this, '_indices'); if (!indices[type]) { + let storageKey = _buildKey(this, 'index-' + type); + indices[type] = StorageArray - .extend({ _storageKey: 'index-' + type }) + .extend({ _storageKey: storageKey }) .create(); } diff --git a/addon/helpers/storage.js b/addon/helpers/storage.js index 26d13f2b..39fe0181 100644 --- a/addon/helpers/storage.js +++ b/addon/helpers/storage.js @@ -179,5 +179,6 @@ export { tryStorage, getStorage, storageFor, - _resetStorages + _resetStorages, + _buildKey }; diff --git a/tests/unit/adapters/indices-test.js b/tests/unit/adapters/indices-test.js index 0c75246e..18edeb13 100644 --- a/tests/unit/adapters/indices-test.js +++ b/tests/unit/adapters/indices-test.js @@ -1,13 +1,17 @@ import { moduleFor, test } from 'ember-qunit'; import { storageEqual, - storageDeepEqual + storageDeepEqual, + registerConfigEnvironment, + setConfigEnvironment } from '../../helpers/storage'; moduleFor('adapter:application', 'Unit | Adapter | indices', { // Specify the other units that are required for this test. // needs: ['serializer:foo'] beforeEach: function() { + registerConfigEnvironment(this); + window.localStorage.clear(); window.sessionStorage.clear(); } @@ -34,7 +38,7 @@ test('it does not persists duplicates to index', function(assert) { storageDeepEqual(assert, window.localStorage['index-projects'], ['1234']); }); -test('it removes ids from index', function(assert) { +test('it removes ids from index (namespace not set)', function(assert) { assert.expect(3); var adapter = this.subject(); @@ -46,3 +50,64 @@ test('it removes ids from index', function(assert) { adapter._removeFromIndex('projects', '1234'); storageDeepEqual(assert, window.localStorage['index-projects'], []); }); + +test('it removes ids from index (namespace: true)', function(assert) { + assert.expect(3); + + setConfigEnvironment(this, 'namespace', true); + + var adapter = this.subject(); + + storageEqual(assert, window.localStorage['index-projects'], undefined); + + adapter._addToIndex('projects', '1234'); + storageDeepEqual( + assert, + window.localStorage['my-app:index-projects'], + ['1234'] + ); + + adapter._removeFromIndex('projects', '1234'); + storageDeepEqual(assert, window.localStorage['my-app:index-projects'], []); +}); + +test('it removes ids from index (namespace: "custom")', function(assert) { + assert.expect(3); + + setConfigEnvironment(this, 'namespace', 'custom'); + + var adapter = this.subject(); + + storageEqual(assert, window.localStorage['index-projects'], undefined); + + adapter._addToIndex('projects', '1234'); + storageDeepEqual( + assert, + window.localStorage['custom:index-projects'], + ['1234'] + ); + + adapter._removeFromIndex('projects', '1234'); + storageDeepEqual(assert, window.localStorage['custom:index-projects'], []); +}); + +test('it removes ids from index (keyDelimiter: "/")', function(assert) { + assert.expect(3); + + setConfigEnvironment(this, 'namespace', true); + setConfigEnvironment(this, 'keyDelimiter', '/'); + + var adapter = this.subject(); + + storageEqual(assert, window.localStorage['index-projects'], undefined); + + adapter._addToIndex('projects', '1234'); + storageDeepEqual( + assert, + window.localStorage['my-app/index-projects'], + ['1234'] + ); + + adapter._removeFromIndex('projects', '1234'); + storageDeepEqual(assert, window.localStorage['my-app/index-projects'], []); +}); diff --git a/tests/unit/models/post-test.js b/tests/unit/models/post-test.js index f9d8f3f4..0f15b9d9 100644 --- a/tests/unit/models/post-test.js +++ b/tests/unit/models/post-test.js @@ -1,6 +1,10 @@ import Ember from 'ember'; import DS from 'ember-data'; import { moduleForModel, test } from 'ember-qunit'; +import { + registerConfigEnvironment, + setConfigEnvironment +} from '../../helpers/storage'; const { get, @@ -19,6 +23,8 @@ moduleForModel('post', 'Unit | Model | post', { 'model:book-publication' ], beforeEach: function() { + registerConfigEnvironment(this); + window.localStorage.clear(); window.sessionStorage.clear(); } @@ -202,3 +208,123 @@ test('queryRecord empty store', function(assert) { done(); }); }); + +test('create a record (namespace: true)', function(assert) { + assert.expect(1); + + setConfigEnvironment(this, 'namespace', true); + + const done = assert.async(); + const store = this.store(); + + run(function() { + store.createRecord('post', { name: 'Just a Name' }).save(); + }); + + store.findAll('post') + .then(function(posts) { + assert.equal(get(posts, 'length'), 1); + done(); + }); +}); + +test('create a record (namespace: "custom")', function(assert) { + assert.expect(1); + + setConfigEnvironment(this, 'namespace', 'custom'); + + const done = assert.async(); + const store = this.store(); + + run(function() { + store.createRecord('post', { name: 'Just a Name' }).save(); + }); + + store.findAll('post') + .then(function(posts) { + assert.equal(get(posts, 'length'), 1); + done(); + }); +}); + +test('create a record (keyDelimiter: "/")', function(assert) { + assert.expect(1); + + setConfigEnvironment(this, 'namespace', 'custom'); + setConfigEnvironment(this, 'keyDelimiter', '/'); + + const done = assert.async(); + const store = this.store(); + + run(function() { + store.createRecord('post', { name: 'Just a Name' }).save(); + }); + + store.findAll('post') + .then(function(posts) { + assert.equal(get(posts, 'length'), 1); + done(); + }); +}); + +test('push a record (namespace: true)', function(assert) { + assert.expect(2); + + setConfigEnvironment(this, 'namespace', true); + + const done = assert.async(); + const store = this.store(); + + let posts = store.findAll('post'); + + assert.equal(get(posts, 'length'), 0); + + run(function() { + store.push({data: [ + { + id: '1', + type: 'post', + attributes: {name: 'Super Name'} + }, + { + id: '2', + type: 'post', + attributes: {name: 'Totally rad'} + } + ]}); + }); + + store.findAll('post') + .then(function(posts) { + assert.equal(get(posts, 'length'), 2); + done(); + }); +}); + +test('find a single record (namespace: true)', function(assert) { + assert.expect(2); + + setConfigEnvironment(this, 'namespace', true); + + const done = assert.async(); + const store = this.store(); + + let newPost; + + run(function() { + newPost = store.createRecord('post', { + name: 'Ember.js: 10 most common mistakes' + }); + + newPost.save(); + }); + + run(function() { + store.find('post', get(newPost, 'id')) + .then(function(post) { + assert.equal(get(post, 'id'), get(newPost, 'id')); + assert.equal(get(post, 'name'), 'Ember.js: 10 most common mistakes'); + done(); + }); + }); +});