spec/javascripts/TransistorBackboneCollectionSpec.js in transistor-0.1.4 vs spec/javascripts/TransistorBackboneCollectionSpec.js in transistor-0.1.5
- old
+ new
@@ -1,7 +1,7 @@
describe("Transistor.Backbone.Collection", function () {
- var radio, collection;
+ var radio, control, collection;
beforeEach(function () {
radio = (function () {
var stub = {};
@@ -17,16 +17,18 @@
}
return stub;
}());
- collection = Transistor.Backbone.Collection(radio, 'news')
- console.log(collection)
+ collection = Transistor.Backbone.Collection({
+ channel: 'news',
+ radio: radio
+ })
});
- describe("write access", function () {
- it("is not granted per default (readonly)", function () {
+ describe("default write access", function () {
+ it("is not granted", function () {
expect(function () {
collection.add({})
}).toThrow(new Error('BackboneCollection is not mutable by user.'));
expect(function () {
@@ -39,9 +41,67 @@
expect(function () {
collection.set({})
}).toThrow(new Error('BackboneCollection is not mutable by user.'));
+ });
+ });
+
+ describe('write access via control', function () {
+ beforeEach(function () {
+ control = (function () {
+ var calls = [],
+ record = function (call, args) { calls.push([call, args]) };
+ return {
+ set: function () { record("set", arguments); },
+ insert: function () { record("insert", arguments); },
+ update: function () { record("update", arguments); },
+ remove: function () { record("remove", arguments); },
+ calls: calls
+ };
+ }());
+
+ collection = Transistor.Backbone.Collection({
+ channel: 'news',
+ radio: radio,
+ control: control
+ });
+ });
+
+ it('delegates add to insert', function () {
+ collection.add({a: 12, b: 13});
+
+ assertEqual(control.calls, [
+ ['insert', ['news', {a: 12, b: 13}]]
+ ]);
+ });
+
+ it('delegates remove to remove', function () {
+ radio.trigger('news', 'init', [{id: 100, a:2, b:3}, {id: 101, a:12, b:23}]);
+
+ var model = collection.get(101);
+
+ collection.remove(model)
+
+ assertEqual(control.calls, [
+ ['remove', ['news', 101]]
+ ]);
+ });
+
+ it('delegates reset to set', function () {
+ collection.reset([{a: 12}, {b: 13}])
+
+ assertEqual(control.calls, [
+ ['set', ['news', [{a: 12}, {b: 13}]]]
+ ]);
+ });
+
+ it('delegates set to set', function () {
+ collection.set([{a: 12}, {b: 13}])
+
+ assertEqual(control.calls, [
+ ['set', ['news', [{a: 12}, {b: 13}]]]
+ ]);
});
});
describe("init event", function () {
it("maps arg 'array' to a clear and set", function () {