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

Warn if store relies on store.name #65

Merged
merged 1 commit into from
May 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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