Skip to content

Commit

Permalink
Prevent you from registering multiple classes with the same Id
Browse files Browse the repository at this point in the history
  • Loading branch information
jhollingworth committed Feb 23, 2015
1 parent b803040 commit 43229f7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class Container {
throw CannotRegisterClassError(clazz, type);
}

if (this.types[type][id]) {
throw ClassAlreadyRegisteredWithId(clazz, type);
}

clazz.id = id;
clazz.type = type;

Expand Down Expand Up @@ -156,4 +160,14 @@ function CannotRegisterClassError(clazz, type) {
}

return new Error(message + type + ' because it does not have an Id.');
}

function ClassAlreadyRegisteredWithId(clazz, type) {
var message = 'Cannot register ';

if (clazz && clazz.displayName) {
message += clazz.displayName + ' ';
}

return new Error(message + type + ' because there is already a class with that Id.');
}
18 changes: 18 additions & 0 deletions test/browser/containerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ describe('Container', function () {
sandbox.restore();
});

describe('when there are two classes with the same Id', function () {
var Store1, Store2;

beforeEach(function () {
expectedId = 'Foo';

Marty.createStore({ id: expectedId });
});

it('should throw an error when you try to register the second class', function () {
expect(registerSecondClass).to.throw(Error);

function registerSecondClass() {
Marty.createStore({ id: expectedId });
}
});
})

describe('stores', function () {
var expectedStore, expectedFoo, actualStore, actionCreators, defaultStore;

Expand Down
2 changes: 1 addition & 1 deletion test/browser/stateMixinSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ describe('StateMixin', function () {

function createStore(state) {
return Marty.createStore({
id: 'stateMixin',
id: uuid.type('StateMixin'),
getInitialState: function () {
return state || {};
}
Expand Down

0 comments on commit 43229f7

Please sign in to comment.