diff --git a/spec/javascripts/inputReaders.spec.js b/spec/javascripts/inputReaders.spec.js index 7424046..d490e54 100644 --- a/spec/javascripts/inputReaders.spec.js +++ b/spec/javascripts/inputReaders.spec.js @@ -23,7 +23,7 @@ describe('input readers', function() { }); afterEach(function() { - Backbone.Syphon.InputReaders.register('text'); + Backbone.Syphon.InputReaders.unregister('text'); }); it('should retrieve the registered "text" reader', function() { @@ -39,7 +39,7 @@ describe('input readers', function() { }); afterEach(function() { - Backbone.Syphon.InputReaders.register('textarea'); + Backbone.Syphon.InputReaders.unregister('textarea'); }); it('should be able to retrieve the input reader for that type', function() { diff --git a/spec/javascripts/typeregistry.spec.js b/spec/javascripts/typeregistry.spec.js new file mode 100644 index 0000000..751b2b9 --- /dev/null +++ b/spec/javascripts/typeregistry.spec.js @@ -0,0 +1,61 @@ +describe('Type Registry', function () { + var typeRegistry; + beforeEach(function () { + typeRegistry = new Backbone.Syphon.TypeRegistry(); + }); + + it('should register a default item', function () { + var item = {}; + typeRegistry.registerDefault(item); + expect(typeRegistry.get('default')).to.be.eql(item); + expect(typeRegistry.get()).to.be.eql(item); + expect(typeRegistry.get('some type that doesnt exist')).to.be.eql(item); + }); + + it('should get an item', function () { + var item = {}; + typeRegistry.register('foo', item); + expect(typeRegistry.get('foo')).to.be.eql(item); + }); + + it('should not get a non existing item', function () { + var item = {}; + typeRegistry.register('foo', item); + expect(typeRegistry.get('moo')).to.be.undefined; + }); + + it('should register an item', function () { + var item = 10; + typeRegistry.register('foo', item); + expect(typeRegistry.get('foo')).to.be.equal(item); + }); + + it('should register a falsy item', function () { + var item = null; + typeRegistry.register('foo', item); + expect(typeRegistry.get('foo')).to.be.equal(item); + }); + + it('should re-register and item', function () { + var item1 = {}; + var item2 = {}; + typeRegistry.register('foo', item1); + typeRegistry.register('foo', item2); + expect(typeRegistry.get('foo')).to.be.eql(item2); + }); + + it('should unregister a type', function () { + var item = {}; + typeRegistry.register('foo', item); + typeRegistry.unregister('foo'); + expect(typeRegistry.get('foo')).to.be.undefined; + }); + + it('should unregister a falsy value', function () { + typeRegistry.register('foo', null); + typeRegistry.unregister('foo'); + expect(typeRegistry.get('foo')).to.be.undefined; + }); + + +}); \ No newline at end of file diff --git a/src/backbone.syphon.typeregistry.js b/src/backbone.syphon.typeregistry.js index 81af6ec..7fe6f2f 100644 --- a/src/backbone.syphon.typeregistry.js +++ b/src/backbone.syphon.typeregistry.js @@ -4,7 +4,7 @@ // Type Registries allow you to register something to // an input type, and retrieve either the item registered // for a specific type or the default registration -var TypeRegistry = Syphon.TypeRegistry = function() { +var TypeRegistry = Syphon.TypeRegistry = function () { this.registeredTypes = {}; }; @@ -16,24 +16,28 @@ _.extend(TypeRegistry.prototype, { // Get the registered item by type. If nothing is // found for the specified type, the default is // returned. - get: function(type){ - return this.registeredTypes[type] || this.registeredTypes['default']; + get: function (type) { + if (_.has(this.registeredTypes, type)) { + return this.registeredTypes[type]; + } else { + return this.registeredTypes['default']; + } }, // Register a new item for a specified type - register: function(type, item) { + register: function (type, item) { this.registeredTypes[type] = item; }, // Register a default item to be used when no // item for a specified type is found - registerDefault: function(item) { + registerDefault: function (item) { this.registeredTypes['default'] = item; }, // Remove an item from a given type registration - unregister: function(type) { - if (this.registeredTypes[type]) { + unregister: function (type) { + if (_.has(this.registeredTypes, type)) { delete this.registeredTypes[type]; } }