Daniel@0: describe("ContextModule.Config", function() { Daniel@0: Daniel@0: it("is promptly created", function() { Daniel@0: var testedConfig = new App.ContextModule.Config(); Daniel@0: Daniel@0: expect(testedConfig).not.toBe(null); Daniel@0: Daniel@0: expect(testedConfig.get("parameters")).not.toBe(null); Daniel@0: expect(testedConfig.get("parameters").keys().length).toEqual(0); Daniel@0: Daniel@0: expect(testedConfig.get("plannedParameterUpdates")).not.toBe(null); Daniel@0: expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(0); Daniel@0: Daniel@0: expect(_.isString(testedConfig.getClientId())).toBe(true); Daniel@0: }); Daniel@0: Daniel@0: Daniel@0: it("is promptly created with parameters", function() { Daniel@0: var testedConfig = new App.ContextModule.Config({ Daniel@0: parameters: { Daniel@0: a: 10, Daniel@0: b: "test" Daniel@0: } Daniel@0: }); Daniel@0: Daniel@0: expect(testedConfig.getParameterValue("a")).toEqual(10); Daniel@0: expect(testedConfig.getParameterValue("b")).toEqual("test"); Daniel@0: expect(testedConfig.getParameterValue("c")).toEqual(undefined); Daniel@0: }); Daniel@0: Daniel@0: it("is promptly created with parameters and planned parameter updates", function() { Daniel@0: var testedConfig = new App.ContextModule.Config({ Daniel@0: parameters: { Daniel@0: a: 10, Daniel@0: b: "test" Daniel@0: }, Daniel@0: plannedParameterUpdates: { Daniel@0: a: 20, Daniel@0: c: true Daniel@0: } Daniel@0: }); Daniel@0: Daniel@0: expect(testedConfig.getParameterValue("a")).toEqual(10); Daniel@0: expect(testedConfig.getParameterValue("b")).toEqual("test"); Daniel@0: expect(testedConfig.getParameterValue("c")).toEqual(undefined); Daniel@0: Daniel@0: expect(testedConfig.getPlannedParameterValue("a")).toEqual(20); Daniel@0: expect(testedConfig.getPlannedParameterValue("b")).toEqual("test"); Daniel@0: expect(testedConfig.getPlannedParameterValue("c")).toEqual(true); Daniel@0: }); Daniel@0: Daniel@0: it("sets and gets parameters instantly one by one", function() { Daniel@0: var testedConfig = new App.ContextModule.Config(); Daniel@0: Daniel@0: testedConfig.updateParameter("p1", 10); Daniel@0: testedConfig.updateParameter("p2", null); Daniel@0: testedConfig.updateParameter("p3", "string"); Daniel@0: testedConfig.updateParameter("p4", false); Daniel@0: testedConfig.updateParameter("p5", ["array"]); Daniel@0: Daniel@0: expect(testedConfig.getParameterValue("p1")).toEqual(10); Daniel@0: expect(testedConfig.getParameterValue("p2")).toEqual(null); Daniel@0: expect(testedConfig.getParameterValue("p3")).toEqual("string"); Daniel@0: expect(testedConfig.getParameterValue("p4")).toEqual(false); Daniel@0: expect(testedConfig.getParameterValue("p5")).toEqual(["array"]); Daniel@0: expect(_.size(testedConfig.attributes.parameters.attributes)).toEqual(5); Daniel@0: Daniel@0: testedConfig.updateParameter("p5", undefined); Daniel@0: expect(testedConfig.getParameterValue("p5")).toEqual(undefined); Daniel@0: Daniel@0: expect(_.size(testedConfig.attributes.parameters.attributes)).toEqual(4); Daniel@0: Daniel@0: expect(function() { Daniel@0: testedConfig.updateParameter(); Daniel@0: }).toThrow(); Daniel@0: expect(function() { Daniel@0: testedConfig.updateParameter(1, 2); Daniel@0: }).toThrow(); Daniel@0: expect(function() { Daniel@0: testedConfig.updateParameter(["a", "b", "c"]); Daniel@0: }).toThrow(); Daniel@0: expect(function() { Daniel@0: testedConfig.updateParameter({x: 1, y: 2}); Daniel@0: }).toThrow(); Daniel@0: }); Daniel@0: Daniel@0: it("sets and gets parameters instantly in bulk", function() { Daniel@0: var testedConfig = new App.ContextModule.Config(); Daniel@0: Daniel@0: testedConfig.updateParameters({ Daniel@0: "p1": 10, Daniel@0: "p2": 42, Daniel@0: "p3": false, Daniel@0: "p100": undefined Daniel@0: }); Daniel@0: expect(_.size(testedConfig.attributes.parameters.attributes)).toEqual(3); Daniel@0: Daniel@0: testedConfig.updateParameters({ Daniel@0: "p2": null, Daniel@0: "p3": "string", Daniel@0: "p4": false, Daniel@0: "p5": ["array"] Daniel@0: }); Daniel@0: Daniel@0: expect(testedConfig.getParameterValue("p1")).toEqual(10); Daniel@0: expect(testedConfig.getParameterValue("p2")).toEqual(null); Daniel@0: expect(testedConfig.getParameterValue("p3")).toEqual("string"); Daniel@0: expect(testedConfig.getParameterValue("p4")).toEqual(false); Daniel@0: expect(testedConfig.getParameterValue("p5")).toEqual(["array"]); Daniel@0: expect(_.size(testedConfig.attributes.parameters.attributes)).toEqual(5); Daniel@0: Daniel@0: testedConfig.updateParameters({ Daniel@0: "p3": undefined, Daniel@0: "p4": undefined, Daniel@0: "p5": undefined Daniel@0: }); Daniel@0: expect(testedConfig.getParameterValue("p3")).toEqual(undefined); Daniel@0: expect(testedConfig.getParameterValue("p4")).toEqual(undefined); Daniel@0: expect(testedConfig.getParameterValue("p5")).toEqual(undefined); Daniel@0: expect(_.size(testedConfig.attributes.parameters.attributes)).toEqual(2); Daniel@0: Daniel@0: expect(function() { Daniel@0: testedConfig.updateParameters(); Daniel@0: }).toThrow(); Daniel@0: expect(function() { Daniel@0: testedConfig.updateParameters("test", 2); Daniel@0: }).toThrow(); Daniel@0: expect(function() { Daniel@0: testedConfig.updateParameters(["a", "b", "c"]); Daniel@0: }).toThrow(); Daniel@0: }); Daniel@0: Daniel@0: it("sets and gets planned parameter updates and applies the updates one by one", function() { Daniel@0: var testedConfig = new App.ContextModule.Config(); Daniel@0: Daniel@0: testedConfig.planParameterUpdate("p1", 10); Daniel@0: expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(1); Daniel@0: testedConfig.planParameterUpdate("p2", "test"); Daniel@0: expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(2); Daniel@0: testedConfig.planParameterUpdate("p3", undefined); Daniel@0: expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(2); Daniel@0: Daniel@0: expect(testedConfig.get("parameters").keys().length).toEqual(0); Daniel@0: Daniel@0: expect(testedConfig.isPlannedToUpdate("p2")).toBe(true); Daniel@0: testedConfig.cancelPlannedParameterUpdate("p2"); Daniel@0: expect(testedConfig.isPlannedToUpdate("p2")).toBe(false); Daniel@0: expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(1); Daniel@0: expect(testedConfig.isPlannedToUpdate("p1")).toBe(true); Daniel@0: expect(testedConfig.isPlannedToUpdate("p3")).toBe(false); Daniel@0: Daniel@0: expect(testedConfig.hasPlannedParameterUpdates()).toBe(true); Daniel@0: Daniel@0: testedConfig.applyPlannedParameterUpdates(); Daniel@0: Daniel@0: testedConfig.getParameterValue("myVar"); Daniel@0: testedConfig.getPlannedParameterValue("myVar"); Daniel@0: testedConfig.isPlannedToUpdate("myVar"); Daniel@0: Daniel@0: testedConfig.applyPlannedParameterUpdates(); Daniel@0: Daniel@0: expect(function() { Daniel@0: testedConfig.planParameterUpdate(); Daniel@0: }).toThrow(); Daniel@0: expect(function() { Daniel@0: testedConfig.planParameterUpdate(1, 2); Daniel@0: }).toThrow(); Daniel@0: expect(function() { Daniel@0: testedConfig.planParameterUpdate(["a", "b", "c"]); Daniel@0: }).toThrow(); Daniel@0: expect(function() { Daniel@0: testedConfig.planParameterUpdate({x: 1, y: 2}); Daniel@0: }).toThrow(); Daniel@0: Daniel@0: }); Daniel@0: Daniel@0: it("sets and gets planned parameter updates and applies the updates in bulk", function() { Daniel@0: var testedConfig = new App.ContextModule.Config(); Daniel@0: Daniel@0: testedConfig.planParameterUpdates({"p1": 10}); Daniel@0: expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(1); Daniel@0: testedConfig.planParameterUpdates({"p2": "test", "p3": false, "p4": undefined}); Daniel@0: expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(3); Daniel@0: Daniel@0: expect(testedConfig.get("parameters").keys().length).toEqual(0); Daniel@0: Daniel@0: expect(testedConfig.isPlannedToUpdate("p2")).toBe(true); Daniel@0: testedConfig.planParameterUpdates({"p3": undefined}); Daniel@0: expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(2); Daniel@0: expect(testedConfig.isPlannedToUpdate("p1")).toBe(true); Daniel@0: expect(testedConfig.isPlannedToUpdate("p2")).toBe(true); Daniel@0: expect(testedConfig.isPlannedToUpdate("p3")).toBe(false); Daniel@0: Daniel@0: expect(testedConfig.hasPlannedParameterUpdates()).toBe(true); Daniel@0: Daniel@0: testedConfig.applyPlannedParameterUpdates(); Daniel@0: Daniel@0: expect(function() { Daniel@0: testedConfig.planParameterUpdates(); Daniel@0: }).toThrow(); Daniel@0: expect(function() { Daniel@0: testedConfig.planParameterUpdates("test", 2); Daniel@0: }).toThrow(); Daniel@0: expect(function() { Daniel@0: testedConfig.planParameterUpdates(["a", "b", "c"]); Daniel@0: }).toThrow(); Daniel@0: }); Daniel@0: Daniel@0: it("sets planned parameter updates when they are undefined", function() { Daniel@0: var testedConfig = new App.ContextModule.Config(); Daniel@0: testedConfig.updateParameters({ Daniel@0: "p1": "a", Daniel@0: "p2": "b" Daniel@0: }); Daniel@0: Daniel@0: var spy = jasmine.createSpy(); Daniel@0: testedConfig.on("change:plannedParameterUpdates", spy); Daniel@0: expect(spy.calls.count()).toEqual(0); Daniel@0: Daniel@0: var hashForPlannedParameterUpdatesBeforeUpdates1 = testedConfig.getHashForPlannedParameterUpdates(); Daniel@0: Daniel@0: testedConfig.planParameterUpdates({ Daniel@0: "p2": undefined, Daniel@0: "p3": undefined, Daniel@0: }); Daniel@0: expect(spy.calls.count()).toEqual(1); Daniel@0: Daniel@0: expect(testedConfig.get("parameters").keys().length).toEqual(2); Daniel@0: expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(1); Daniel@0: Daniel@0: expect(testedConfig.isPlannedToUpdate("p1")).toBe(false); Daniel@0: expect(testedConfig.isPlannedToUpdate("p2")).toBe(true); Daniel@0: expect(testedConfig.isPlannedToUpdate("p3")).toBe(false); Daniel@0: Daniel@0: expect(testedConfig.getPlannedParameterValue("p1")).toBe("a"); Daniel@0: expect(testedConfig.getPlannedParameterValue("p2")).toBe(undefined); Daniel@0: expect(testedConfig.getPlannedParameterValue("p3")).toBe(undefined); Daniel@0: Daniel@0: var hashForPlannedParameterUpdatesBeforeUpdates2 = testedConfig.getHashForPlannedParameterUpdates(); Daniel@0: expect(hashForPlannedParameterUpdatesBeforeUpdates2).not.toEqual(hashForPlannedParameterUpdatesBeforeUpdates1); Daniel@0: Daniel@0: testedConfig.applyPlannedParameterUpdates(); Daniel@0: expect(spy.calls.count()).toEqual(2); Daniel@0: Daniel@0: expect(testedConfig.get("parameters").keys().length).toEqual(1); Daniel@0: expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(0); Daniel@0: Daniel@0: var hashForPlannedParameterUpdatesBeforeUpdates3 = testedConfig.getHashForPlannedParameterUpdates(); Daniel@0: Daniel@0: expect(hashForPlannedParameterUpdatesBeforeUpdates3).not.toEqual(hashForPlannedParameterUpdatesBeforeUpdates2); Daniel@0: expect(hashForPlannedParameterUpdatesBeforeUpdates3).toEqual(hashForPlannedParameterUpdatesBeforeUpdates1); Daniel@0: }); Daniel@0: Daniel@0: it("cancels planned changes on demand", function() { Daniel@0: var testedConfig = new App.ContextModule.Config(); Daniel@0: Daniel@0: testedConfig.updateParameter("p1", 10); Daniel@0: Daniel@0: expect(testedConfig.hasPlannedParameterUpdates()).toBe(false); Daniel@0: Daniel@0: testedConfig.planParameterUpdate("p1", 11); Daniel@0: testedConfig.planParameterUpdate("p2", "test"); Daniel@0: Daniel@0: expect(testedConfig.hasPlannedParameterUpdates()).toBe(true); Daniel@0: Daniel@0: testedConfig.cancelPlannedParameterUpdate("p2"); Daniel@0: expect(testedConfig.hasPlannedParameterUpdates()).toBe(true); Daniel@0: testedConfig.cancelPlannedParameterUpdate("p1"); Daniel@0: expect(testedConfig.hasPlannedParameterUpdates()).toBe(false); Daniel@0: Daniel@0: testedConfig.planParameterUpdate("p1", 11); Daniel@0: testedConfig.planParameterUpdate("p2", "test"); Daniel@0: expect(testedConfig.hasPlannedParameterUpdates()).toBe(true); Daniel@0: Daniel@0: testedConfig.cancelPlannedParameterUpdates(); Daniel@0: expect(testedConfig.hasPlannedParameterUpdates()).toBe(false); Daniel@0: Daniel@0: testedConfig.planParameterUpdate("p1", 11); Daniel@0: testedConfig.planParameterUpdate("p2", "test"); Daniel@0: testedConfig.planParameterUpdate("p3", "test"); Daniel@0: testedConfig.cancelPlannedParameterUpdates(["p2", "p3"]); Daniel@0: expect(testedConfig.hasPlannedParameterUpdates()).toBe(true); Daniel@0: testedConfig.cancelPlannedParameterUpdates(["p100"]); Daniel@0: expect(testedConfig.hasPlannedParameterUpdates()).toBe(true); Daniel@0: testedConfig.cancelPlannedParameterUpdates(["p1"]); Daniel@0: expect(testedConfig.hasPlannedParameterUpdates()).toBe(false); Daniel@0: Daniel@0: Daniel@0: }); Daniel@0: Daniel@0: xit("returns config grid type and dimension", function() { Daniel@0: //testedConfig.getConfigGridType(); Daniel@0: //testedConfig.getDimension(); Daniel@0: }); Daniel@0: Daniel@0: xit("calculates and obtains its own hashes", function() { Daniel@0: //testedConfig.getHashForParameters() Daniel@0: //testedConfig.getHashForPlannedParameterUpdates() Daniel@0: //testedConfig.getHashForPermanent() Daniel@0: //testedConfig.getHashForTemp() Daniel@0: //testedConfig.getHash() Daniel@0: }); Daniel@0: Daniel@0: it("serializes and unserializes itself", function() { Daniel@0: var testedConfig = new App.ContextModule.Config(); Daniel@0: var clientId = testedConfig.getClientId(); Daniel@0: var originalSerializedObject = { Daniel@0: "clientId": clientId, Daniel@0: "parameters": { Daniel@0: "p1": 10, Daniel@0: "p2": "test", Daniel@0: "p3": false, Daniel@0: "p4": null, Daniel@0: "p5": [1, "test"], Daniel@0: }, Daniel@0: "plannedParameterUpdates": { Daniel@0: "p1": 11, Daniel@0: "p3": undefined, Daniel@0: "p4": ["10", null], Daniel@0: } Daniel@0: }; Daniel@0: testedConfig.unserialize(originalSerializedObject); Daniel@0: Daniel@0: expect(testedConfig.getParameterValue("p1")).toEqual(10); Daniel@0: expect(testedConfig.getParameterValue("p2")).toEqual("test"); Daniel@0: expect(testedConfig.getParameterValue("p3")).toEqual(false); Daniel@0: expect(testedConfig.getParameterValue("p4")).toEqual(null); Daniel@0: expect(testedConfig.getParameterValue("p5")).toEqual([1, "test"]); Daniel@0: expect(testedConfig.getParameterValue("p9")).toEqual(undefined); Daniel@0: Daniel@0: expect(testedConfig.getPlannedParameterValue("p1")).toEqual(11); Daniel@0: expect(testedConfig.getPlannedParameterValue("p2")).toEqual("test"); Daniel@0: expect(testedConfig.getPlannedParameterValue("p3")).toEqual(undefined); Daniel@0: expect(testedConfig.getPlannedParameterValue("p4")).toEqual(["10", null]); Daniel@0: expect(testedConfig.getPlannedParameterValue("p5")).toEqual([1, "test"]); Daniel@0: expect(testedConfig.getPlannedParameterValue("p9")).toEqual(undefined); Daniel@0: Daniel@0: expect(testedConfig.isPlannedToUpdate("p1")).toEqual(true); Daniel@0: expect(testedConfig.isPlannedToUpdate("p2")).toEqual(false); Daniel@0: expect(testedConfig.isPlannedToUpdate("p3")).toEqual(true); Daniel@0: expect(testedConfig.isPlannedToUpdate("p4")).toEqual(true); Daniel@0: expect(testedConfig.isPlannedToUpdate("p5")).toEqual(false); Daniel@0: expect(testedConfig.isPlannedToUpdate("p9")).toEqual(false); Daniel@0: Daniel@0: expect(testedConfig.serialize()).toEqual(originalSerializedObject); Daniel@0: Daniel@0: testedConfig.applyPlannedParameterUpdates(); Daniel@0: Daniel@0: expect(testedConfig.serialize()).toEqual({ Daniel@0: "clientId": clientId, Daniel@0: "parameters": { Daniel@0: "p1": 11, Daniel@0: "p2": "test", Daniel@0: "p4": ["10", null], Daniel@0: "p5": [1, "test"] Daniel@0: }, Daniel@0: "plannedParameterUpdates": { Daniel@0: } Daniel@0: }); Daniel@0: Daniel@0: testedConfig.planParameterUpdate("p1", 12); Daniel@0: testedConfig.planParameterUpdate("p2", undefined); Daniel@0: testedConfig.planParameterUpdate("p5", [1, 2, 3]); Daniel@0: testedConfig.planParameterUpdate("p9", undefined); Daniel@0: Daniel@0: expect(testedConfig.serialize()).toEqual({ Daniel@0: "clientId": clientId, Daniel@0: "parameters": { Daniel@0: "p1": 11, Daniel@0: "p2": "test", Daniel@0: "p4": ["10", null], Daniel@0: "p5": [1, "test"] Daniel@0: }, Daniel@0: "plannedParameterUpdates": { Daniel@0: "p1": 12, Daniel@0: "p2": undefined, Daniel@0: "p5": [1, 2, 3] Daniel@0: } Daniel@0: }); Daniel@0: Daniel@0: expect(function(){ Daniel@0: originalSerializedObject.clientId = 42; Daniel@0: testedConfig.unserialize(originalSerializedObject); Daniel@0: }).toThrow(); Daniel@0: }); Daniel@0: Daniel@0: it("unserializes itself from faulty serialized objects", function() { Daniel@0: // Config can be unserialized from anything... Daniel@0: var faultySerializedObjectPairs = [ Daniel@0: [null, Daniel@0: {parameters: {}, plannedParameterUpdates: {}}], Daniel@0: [undefined, Daniel@0: {parameters: {}, plannedParameterUpdates: {}}], Daniel@0: [42, Daniel@0: {parameters: {}, plannedParameterUpdates: {}}], Daniel@0: ["test", Daniel@0: {parameters: {}, plannedParameterUpdates: {}}], Daniel@0: [{parameters: 42}, Daniel@0: {parameters: {}, plannedParameterUpdates: {}}], Daniel@0: [{parameters: ["foo", "bar"]}, Daniel@0: {parameters: {}, plannedParameterUpdates: {}}], Daniel@0: [{plannedParameterUpdates: 10}, Daniel@0: {parameters: {}, plannedParameterUpdates: {}}], Daniel@0: [{plannedParameterUpdates: {"x": 10}}, Daniel@0: {parameters: {}, plannedParameterUpdates: {"x": 10}}] Daniel@0: ]; Daniel@0: Daniel@0: Daniel@0: _.each(faultySerializedObjectPairs, function(faultySerializedObjectPair) { Daniel@0: var testedConfig = new App.ContextModule.Config(faultySerializedObjectPair[0]); Daniel@0: faultySerializedObjectPair[1].clientId = testedConfig.getClientId(); Daniel@0: expect(testedConfig.serialize()).toEqual(faultySerializedObjectPair[1]); Daniel@0: Daniel@0: var testedConfig2 = new App.ContextModule.Config(); Daniel@0: testedConfig2.unserialize(faultySerializedObjectPair[0]); Daniel@0: faultySerializedObjectPair[1].clientId = testedConfig2.getClientId(); Daniel@0: expect(testedConfig2.serialize()).toEqual(faultySerializedObjectPair[1]); Daniel@0: }); Daniel@0: Daniel@0: // ... except for when clientId does not match the internal clientId() Daniel@0: var faultySerializedObjects = [ Daniel@0: {clientId: "wrong"}, Daniel@0: {clientId: 0}, Daniel@0: {parameters: 42, clientId: "wrong"}, Daniel@0: {plannedParameterUpdates: {"x": "y"}, clientId: 42}, Daniel@0: ]; Daniel@0: Daniel@0: var testedConfig = new App.ContextModule.Config(); Daniel@0: Daniel@0: _.each(faultySerializedObjects, function(faultySerializedObject) { Daniel@0: expect(function() { Daniel@0: testedConfig.unserialize(faultySerializedObject); Daniel@0: }).toThrow(); Daniel@0: }); Daniel@0: }); Daniel@0: Daniel@0: it("clones itself", function() { Daniel@0: var testedConfig = new App.ContextModule.Config({ Daniel@0: parameters: { Daniel@0: a: 10, Daniel@0: b: "test" Daniel@0: }, Daniel@0: plannedParameterUpdates: { Daniel@0: a: 20, Daniel@0: c: true Daniel@0: } Daniel@0: }); Daniel@0: Daniel@0: var clonedConfig = testedConfig.clone(); Daniel@0: Daniel@0: expect(clonedConfig.getClientId()).not.toEqual(testedConfig.getClientId()); Daniel@0: Daniel@0: expect(clonedConfig.getParameterValue("a")).toEqual(10); Daniel@0: expect(clonedConfig.getParameterValue("b")).toEqual("test"); Daniel@0: expect(clonedConfig.getParameterValue("c")).toEqual(undefined); Daniel@0: Daniel@0: expect(clonedConfig.getPlannedParameterValue("a")).toEqual(20); Daniel@0: expect(clonedConfig.getPlannedParameterValue("b")).toEqual("test"); Daniel@0: expect(clonedConfig.getPlannedParameterValue("c")).toEqual(true); Daniel@0: Daniel@0: clonedConfig.updateParameter("a", 42); Daniel@0: clonedConfig.planParameterUpdate("a", 43); Daniel@0: Daniel@0: expect(clonedConfig.getParameterValue("a")).toEqual(42); Daniel@0: expect(clonedConfig.getPlannedParameterValue("a")).toEqual(43); Daniel@0: expect(testedConfig.getParameterValue("a")).toEqual(10); Daniel@0: expect(testedConfig.getPlannedParameterValue("a")).toEqual(20); Daniel@0: Daniel@0: }); Daniel@0: Daniel@0: it("triggers events when real changes occur", function() { Daniel@0: var testedConfig = new App.ContextModule.Config(); Daniel@0: Daniel@0: var spy = jasmine.createSpyObj("listener", ["change", "changeParameters", "changePlannedParameterUpdates"]); Daniel@0: Daniel@0: var expectSpyCallCountAndReset = function(one, two, three) { Daniel@0: // console.log("~~~~~"); Daniel@0: // console.log("1", spy.change.calls.count(), one); Daniel@0: // console.log("2", spy.changeParameters.calls.count(), two); Daniel@0: // console.log("3", spy.changePlannedParameterUpdates.calls.count(), three); Daniel@0: expect(spy.change.calls.count()).toEqual(one); Daniel@0: expect(spy.changeParameters.calls.count()).toEqual(two); Daniel@0: expect(spy.changePlannedParameterUpdates.calls.count()).toEqual(three); Daniel@0: spy.changeParameters.calls.reset(); Daniel@0: spy.changePlannedParameterUpdates.calls.reset(); Daniel@0: spy.change.calls.reset(); Daniel@0: }; Daniel@0: Daniel@0: testedConfig.on("change", spy.change, spy); Daniel@0: testedConfig.on("change:parameters", spy.changeParameters, spy); Daniel@0: testedConfig.on("change:plannedParameterUpdates", spy.changePlannedParameterUpdates, spy); Daniel@0: Daniel@0: expect(spy.change.calls.count()).toEqual(0); Daniel@0: expect(spy.changeParameters.calls.count()).toEqual(0); Daniel@0: expect(spy.changePlannedParameterUpdates.calls.count()).toEqual(0); Daniel@0: Daniel@0: testedConfig.planParameterUpdate("p1", 12); Daniel@0: expectSpyCallCountAndReset(1, 0, 1); Daniel@0: Daniel@0: testedConfig.planParameterUpdate("p1", 12); Daniel@0: expectSpyCallCountAndReset(0, 0, 0); Daniel@0: Daniel@0: testedConfig.planParameterUpdate("p2", 42); Daniel@0: expectSpyCallCountAndReset(1, 0, 1); Daniel@0: Daniel@0: testedConfig.cancelPlannedParameterUpdate("p2"); Daniel@0: expectSpyCallCountAndReset(1, 0, 1); Daniel@0: Daniel@0: testedConfig.applyPlannedParameterUpdates(); Daniel@0: expectSpyCallCountAndReset(1, 1, 1); Daniel@0: Daniel@0: testedConfig.unserialize({"clientId": testedConfig.getClientId(), "parameters": {x: 42}, "plannedParameterUpdates": {}}); Daniel@0: expectSpyCallCountAndReset(1, 1, 0); Daniel@0: testedConfig.unserialize({"clientId": testedConfig.getClientId(), "parameters": {x: 42}, "plannedParameterUpdates": {}}); Daniel@0: expectSpyCallCountAndReset(0, 0, 0); Daniel@0: Daniel@0: testedConfig.updateParameter("p10", 10); Daniel@0: expectSpyCallCountAndReset(1, 1, 0); Daniel@0: Daniel@0: testedConfig.planParameterUpdate("p1", 12); Daniel@0: expectSpyCallCountAndReset(1, 0, 1); Daniel@0: testedConfig.updateParameter("p1", 100); Daniel@0: expectSpyCallCountAndReset(1, 1, 1); Daniel@0: Daniel@0: console.log("!!!", testedConfig); Daniel@0: testedConfig.cancelPlannedParameterUpdates(); Daniel@0: expectSpyCallCountAndReset(0, 0, 0); Daniel@0: Daniel@0: testedConfig.planParameterUpdate("p1", 12); Daniel@0: expectSpyCallCountAndReset(1, 0, 1); Daniel@0: testedConfig.unserialize(null); Daniel@0: expectSpyCallCountAndReset(1, 1, 1); Daniel@0: testedConfig.unserialize(null); Daniel@0: expectSpyCallCountAndReset(0, 0, 0); Daniel@0: Daniel@0: // BULK Daniel@0: testedConfig.updateParameters({"p1": 42, "p2": 0}); Daniel@0: expectSpyCallCountAndReset(1, 1, 0); Daniel@0: testedConfig.planParameterUpdate("p1", 42); Daniel@0: expectSpyCallCountAndReset(0, 0, 0); Daniel@0: testedConfig.planParameterUpdates({"p1": 42}); Daniel@0: expectSpyCallCountAndReset(0, 0, 0); Daniel@0: testedConfig.planParameterUpdates({"p1": 4, "p2": 2, "p3": 3, "p4": 4}); Daniel@0: expectSpyCallCountAndReset(1, 0, 1); Daniel@0: testedConfig.cancelPlannedParameterUpdates(["p3", "p4"]); Daniel@0: expectSpyCallCountAndReset(1, 0, 1); Daniel@0: testedConfig.cancelPlannedParameterUpdates(["p3", "p4", "p42"]); Daniel@0: expectSpyCallCountAndReset(0, 0, 0); Daniel@0: testedConfig.applyPlannedParameterUpdates(); Daniel@0: expectSpyCallCountAndReset(1, 1, 1); Daniel@0: testedConfig.planParameterUpdates({"p1": 4, "p2": 2, "p3": 3, "p4": 4}); Daniel@0: expectSpyCallCountAndReset(1, 0, 1); Daniel@0: testedConfig.cancelPlannedParameterUpdates(); Daniel@0: expectSpyCallCountAndReset(1, 0, 1); Daniel@0: Daniel@0: expect(testedConfig.hasChanged()).toEqual(false); Daniel@0: testedConfig.on("change", function() { Daniel@0: expect(testedConfig.hasChanged()).toEqual(true); Daniel@0: }); Daniel@0: Daniel@0: testedConfig.updateParameter("test", 42); Daniel@0: Daniel@0: expect(testedConfig.hasChanged()).toEqual(false); Daniel@0: Daniel@0: }); Daniel@0: });