Skip to content

Commit

Permalink
Implement namespace for adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
fsmanuel committed Feb 7, 2018
1 parent 45fad18 commit 30e7d45
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 8 deletions.
3 changes: 2 additions & 1 deletion addon/adapters/base.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -323,7 +324,7 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
},

_storageKey(type, id) {
return type + '-' + id;
return _buildKey(this, type + '-' + id);
},

// Should be overwriten
Expand Down
6 changes: 4 additions & 2 deletions addon/adapters/local.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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();
}

Expand Down
6 changes: 4 additions & 2 deletions addon/adapters/session.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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();
}

Expand Down
3 changes: 2 additions & 1 deletion addon/helpers/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,6 @@ export {
tryStorage,
getStorage,
storageFor,
_resetStorages
_resetStorages,
_buildKey
};
69 changes: 67 additions & 2 deletions tests/unit/adapters/indices-test.js
Original file line number Diff line number Diff line change
@@ -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();
}
Expand All @@ -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();

Expand All @@ -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'], []);
});
126 changes: 126 additions & 0 deletions tests/unit/models/post-test.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -19,6 +23,8 @@ moduleForModel('post', 'Unit | Model | post', {
'model:book-publication'
],
beforeEach: function() {
registerConfigEnvironment(this);

window.localStorage.clear();
window.sessionStorage.clear();
}
Expand Down Expand Up @@ -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();
});
});
});

0 comments on commit 30e7d45

Please sign in to comment.