Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
Merge pull request #65 from yahoo/warnAboutStoreNameProperty
Browse files Browse the repository at this point in the history
Warn if store relies on store.name
  • Loading branch information
mridgway committed May 18, 2015
2 parents 56c6ba9 + 25a117f commit 204ab25
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
19 changes: 18 additions & 1 deletion lib/Dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ function Dispatcher (options) {
this.stores = {};
this.handlers = {};
this.handlers[DEFAULT] = [];
this.hasWarnedAboutNameProperty = false;
options.stores.forEach(function (store) {
this.registerStore(store);
}, this);

}

Dispatcher.prototype.createContext = function createContext(context) {
Expand Down Expand Up @@ -97,7 +99,22 @@ Dispatcher.prototype.getStoreName = function getStoreName(store) {
if ('string' === typeof store) {
return store;
}
return store.storeName || store.name;
if (store.storeName) {
return store.storeName;
}

if (process.env.NODE_ENV !== 'production') {
if (store.name && !this.hasWarnedAboutNameProperty) {
console.warn('A store has been registered that relies on the ' +
'constructor\'s name property. This name may change when you ' +
'minify your stores during build time and could break string ' +
'references to your store. It is advised that you add a ' +
'static `storeName` property to your store to ensure the ' +
'store name does not change during your build.');
this.hasWarnedAboutNameProperty = true;
}
}
return store.name;
};

/**
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/lib/Dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ describe('Dispatchr', function () {

describe('#registerStore', function () {
it('should throw if store name is already registered', function () {
var NewStore = function Store () {};
NewStore.storeName = 'Store';
expect(function () {
dispatcher.registerStore(function Store () {});
dispatcher.registerStore(NewStore);
}).to.throw(Error);
});

Expand All @@ -55,6 +57,17 @@ describe('Dispatchr', function () {
dispatcher.registerStore('store');
}).to.throw(Error);
});

it('should warn if registering store that relies on name property', function () {
var oldWarn = console.warn;
var warning;
console.warn = function(message) {
warning = message;
};
dispatcher.registerStore(function NewStore() {});
console.warn = oldWarn;
expect(warning).to.not.equal(undefined);
});
});

describe('#isRegistered', function () {
Expand Down

0 comments on commit 204ab25

Please sign in to comment.