Skip to content

Commit

Permalink
Implement namespace for storageFor
Browse files Browse the repository at this point in the history
  • Loading branch information
fsmanuel committed Feb 7, 2018
1 parent 3cdf12e commit 4153b9e
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 16 deletions.
25 changes: 25 additions & 0 deletions addon/helpers/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ function createStorage(context, key, modelKey, options) {
storageKey = modelKey ? `${storageFactory}:${modelKey}` : storageFactory;
}

storageKey = _buildKey(context, storageKey);

const initialState = {},
defaultState = {
_storageKey: storageKey
Expand Down Expand Up @@ -145,6 +147,29 @@ function _modelKey(model) {
return `${modelName}:${id}`;
}

// TODO: v2.0 - Make modulePrefix the default
function _getNamespace(appConfig, addonConfig) {
// For backward compatibility this is a opt-in feature
let namespace = addonConfig.namespace;

// Shortcut for modulePrefix
if (namespace === true) {
namespace = appConfig.modulePrefix
}

return namespace;
}

// TODO: Add migration helper
function _buildKey(context, key) {
let appConfig = getOwner(context).resolveRegistration('config:environment');
let addonConfig = appConfig && appConfig['ember-local-storage'] || {};
let namespace = _getNamespace(appConfig, addonConfig);
let delimiter = addonConfig.keyDelimiter || ':';

return namespace ? `${namespace}${delimiter}${key}` : key;
}

// Testing helper
function _resetStorages() {
storages = {};
Expand Down
12 changes: 11 additions & 1 deletion tests/helpers/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ function storageDeepEqual(assert, actual, expected, message) {
assert.deepEqual(actual, expected, message);
}

function registerConfigEnvironment(context) {
let environment = {
modulePrefix: 'my-app',
'ember-local-storage': {}
};

context.register('config:environment', environment, { instantiate: false });
}

export {
storageEqual,
storageDeepEqual
storageDeepEqual,
registerConfigEnvironment
};
133 changes: 118 additions & 15 deletions tests/unit/storage-for-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Ember from 'ember';
import { moduleFor, test } from 'ember-qunit';
import {
storageDeepEqual
storageDeepEqual,
registerConfigEnvironment
} from '../helpers/storage';

import StorageObject from 'ember-local-storage/local/object';
Expand All @@ -10,8 +11,12 @@ import {
_resetStorages
} from 'ember-local-storage/helpers/storage';

moduleFor('router:main', 'legacy - config', {
let subject;

moduleFor('router:main', 'storageFor', {
beforeEach() {
registerConfigEnvironment(this);

let mockStorage = StorageObject.extend();

mockStorage.reopenClass({
Expand All @@ -24,28 +29,28 @@ moduleFor('router:main', 'legacy - config', {

this.register('storage:settings', mockStorage);
this.register('storage:options', mockStorage);

let post = Ember.Object.extend({
modelName: 'post',
id: '123'
}).create();

this.register('object:test', Ember.Object.extend({
post: post,
settings: storageFor('settings', 'post'),
options: storageFor('options', 'post')
}));
subject = this.container.lookup('object:test');
},
afterEach() {
window.localStorage.clear();
_resetStorages();
}
});

test('it has the correct key', function(assert) {
test('it has the correct key (namespace not set)', function(assert) {
assert.expect(4);

let post = Ember.Object.extend({
modelName: 'post',
id: '123'
}).create();

this.register('object:test', Ember.Object.extend({
post: post,
settings: storageFor('settings', 'post'),
options: storageFor('options', 'post')
}));
let subject = this.container.lookup('object:test');

assert.equal(
subject.get('settings._storageKey'),
'storage:settings:post:123'
Expand All @@ -64,3 +69,101 @@ test('it has the correct key', function(assert) {
perPage: 10
});
});

test('it has the correct key (namespace: true)', function(assert) {
assert.expect(4);

let appConfig = this.container.lookup('config:environment');
let addonConfig = appConfig['ember-local-storage'] || {};
addonConfig.namespace = true;

assert.equal(
subject.get('settings._storageKey'),
'my-app:storage:settings:post:123'
);

assert.equal(
subject.get('options._storageKey'),
'my-app:storage:options:post:123'
);

storageDeepEqual(assert,
window.localStorage['my-app:storage:settings:post:123'],
{
perPage: 10
}
);

storageDeepEqual(assert,
window.localStorage['my-app:storage:options:post:123'],
{
perPage: 10
}
);
});

test('it has the correct key (namespace: "custom")', function(assert) {
assert.expect(4);

let appConfig = this.container.lookup('config:environment');
let addonConfig = appConfig['ember-local-storage'] || {};
addonConfig.namespace = 'custom';

assert.equal(
subject.get('settings._storageKey'),
'custom:storage:settings:post:123'
);

assert.equal(
subject.get('options._storageKey'),
'custom:storage:options:post:123'
);

storageDeepEqual(assert,
window.localStorage['custom:storage:settings:post:123'],
{
perPage: 10
}
);

storageDeepEqual(assert,
window.localStorage['custom:storage:options:post:123'],
{
perPage: 10
}
);
});

test('it has the correct key (keyDelimiter: "/")', function(assert) {
assert.expect(4);

let appConfig = this.container.lookup('config:environment');
let addonConfig = appConfig['ember-local-storage'] || {};
addonConfig.namespace = true;
addonConfig.keyDelimiter = '/';

assert.equal(
subject.get('settings._storageKey'),
'my-app/storage:settings:post:123'
);

assert.equal(
subject.get('options._storageKey'),
'my-app/storage:options:post:123'
);

storageDeepEqual(assert,
window.localStorage['my-app/storage:settings:post:123'],
{
perPage: 10
}
);

storageDeepEqual(assert,
window.localStorage['my-app/storage:options:post:123'],
{
perPage: 10
}
);
});

0 comments on commit 4153b9e

Please sign in to comment.