describe("Transistor.Binder", function () { var binder, array; beforeEach(function () { array = []; binder = Transistor.Binder(array); }); describe("init event", function () { it("throws on undefined", function () { array.push(23); array.push('a string'); expect(function () { binder('init', undefined); }).toThrow(new Error('undefined arguments')); refuteEmpty(array); }); it("maps arg 'empty array' to a clear", function () { array.push(23); array.push('a string'); binder('init', []); assertEmpty(array); }); it("maps arg 'array' to a clear and set", function () { array.push(23); array.push('a string'); binder('init', ['my', 'objects']); assertEqual(['my', 'objects'], array); }); }); describe("set event", function () { it("throws on undefined args", function () { array.push(23); array.push('a string'); expect(function () { binder('set', undefined); }).toThrow(new Error('undefined arguments')); refuteEmpty(array); }); it("throws on undefined collection", function () { array.push(23); array.push('a string'); expect(function () { binder('set', { collection: undefined }); }).toThrow(new Error('undefined collection')); refuteEmpty(array); }); it("accepts an empty array as collection", function () { array.push(23); array.push('a string'); binder('set', { collection: [] }); assertEmpty(array); }); it("accepts a filled array as collection", function () { array.push(23); array.push('a string'); binder('set', { collection: ['my', 'objects'] }); assertEqual(['my', 'objects'], array); }); }); describe("insert event", function () { it("throws on undefined args", function () { array.push(23); array.push('a string'); expect(function () { binder('insert', undefined); }).toThrow(new Error('undefined arguments')); refuteEmpty(array); }); it("throws on undefined entry", function () { array.push(23); array.push('a string'); expect(function () { binder('insert', { entry: undefined }); }).toThrow(new Error('undefined entry')); refuteEmpty(array); }); it("maps collection arg 'entry' to a push", function () { array.push(23); array.push('a string'); binder('insert', { entry: 'my_object' }); assertEqual([23, 'a string', 'my_object'], array); }); }); describe("update event", function () { it("throws on undefined args", function () { array.push(23); array.push('a string'); expect(function () { binder('update', undefined); }).toThrow(new Error('undefined arguments')); refuteEmpty(array); }); it("throws on undefined entry", function () { array.push(23); array.push('a string'); expect(function () { binder('update', { id: 12, entry: undefined }); }).toThrow(new Error('undefined entry')); assertEqual([23, 'a string'], array); }); it("throws on undefined id", function () { array.push(23); array.push('a string'); expect(function () { binder('update', { id: undefined, entry: {} }); }).toThrow(new Error('undefined id')); assertEqual([23, 'a string'], array); }); it("maps collection arg 'entry' to a replace at given id", function () { array.push({ id: 1, entry: {'a': 13} }); array.push({ id: 2, entry: {'b': 42} }); binder('update', { id: 2, entry: {id: 2, entry: {'x': 12}} }); assertEqual([ {id: 1, entry: {'a': 13}}, {id: 2, entry: {'x': 12}} ], array) }); it("throws if id was not found", function() { array.push({ id: 1, entry: {'a': 13} }); array.push({ id: 2, entry: {'b': 42} }); expect(function () { binder('update', { id: 3, entry: {} }); }).toThrow(new Error('unknown id: 3')); assertEqual([ {id: 1, entry: {'a': 13}}, {id: 2, entry: {'b': 42}} ], array) }); }); describe("remove event", function () { it("throws on undefined args", function () { array.push(23); array.push('a string'); expect(function () { binder('remove', undefined); }).toThrow(new Error('undefined arguments')); refuteEmpty(array); }); it("throws on undefined id", function () { array.push(23); array.push('a string'); expect(function () { binder('remove', { id: undefined }); }).toThrow(new Error('undefined id')); assertEqual([23, 'a string'], array); }); it("removes entry at given id", function () { array.push({ id: 1, entry: {'a': 13} }); array.push({ id: 2, entry: {'b': 42} }); binder('remove', { id: 1, }); assertEqual([ {id: 2, entry: {'b': 42}} ], array) }); it("throws if id was not found", function() { array.push({ id: 1, entry: {'a': 13} }); array.push({ id: 2, entry: {'b': 42} }); expect(function () { binder('remove', { id: 3 }); }).toThrow(new Error('unknown id: 3')); assertEqual([ {id: 1, entry: {'a': 13}}, {id: 2, entry: {'b': 42}} ], array) }); }); });