Daniel@0
|
1 describe("ContextModule.Config", function() {
|
Daniel@0
|
2
|
Daniel@0
|
3 it("is promptly created", function() {
|
Daniel@0
|
4 var testedConfig = new App.ContextModule.Config();
|
Daniel@0
|
5
|
Daniel@0
|
6 expect(testedConfig).not.toBe(null);
|
Daniel@0
|
7
|
Daniel@0
|
8 expect(testedConfig.get("parameters")).not.toBe(null);
|
Daniel@0
|
9 expect(testedConfig.get("parameters").keys().length).toEqual(0);
|
Daniel@0
|
10
|
Daniel@0
|
11 expect(testedConfig.get("plannedParameterUpdates")).not.toBe(null);
|
Daniel@0
|
12 expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(0);
|
Daniel@0
|
13
|
Daniel@0
|
14 expect(_.isString(testedConfig.getClientId())).toBe(true);
|
Daniel@0
|
15 });
|
Daniel@0
|
16
|
Daniel@0
|
17
|
Daniel@0
|
18 it("is promptly created with parameters", function() {
|
Daniel@0
|
19 var testedConfig = new App.ContextModule.Config({
|
Daniel@0
|
20 parameters: {
|
Daniel@0
|
21 a: 10,
|
Daniel@0
|
22 b: "test"
|
Daniel@0
|
23 }
|
Daniel@0
|
24 });
|
Daniel@0
|
25
|
Daniel@0
|
26 expect(testedConfig.getParameterValue("a")).toEqual(10);
|
Daniel@0
|
27 expect(testedConfig.getParameterValue("b")).toEqual("test");
|
Daniel@0
|
28 expect(testedConfig.getParameterValue("c")).toEqual(undefined);
|
Daniel@0
|
29 });
|
Daniel@0
|
30
|
Daniel@0
|
31 it("is promptly created with parameters and planned parameter updates", function() {
|
Daniel@0
|
32 var testedConfig = new App.ContextModule.Config({
|
Daniel@0
|
33 parameters: {
|
Daniel@0
|
34 a: 10,
|
Daniel@0
|
35 b: "test"
|
Daniel@0
|
36 },
|
Daniel@0
|
37 plannedParameterUpdates: {
|
Daniel@0
|
38 a: 20,
|
Daniel@0
|
39 c: true
|
Daniel@0
|
40 }
|
Daniel@0
|
41 });
|
Daniel@0
|
42
|
Daniel@0
|
43 expect(testedConfig.getParameterValue("a")).toEqual(10);
|
Daniel@0
|
44 expect(testedConfig.getParameterValue("b")).toEqual("test");
|
Daniel@0
|
45 expect(testedConfig.getParameterValue("c")).toEqual(undefined);
|
Daniel@0
|
46
|
Daniel@0
|
47 expect(testedConfig.getPlannedParameterValue("a")).toEqual(20);
|
Daniel@0
|
48 expect(testedConfig.getPlannedParameterValue("b")).toEqual("test");
|
Daniel@0
|
49 expect(testedConfig.getPlannedParameterValue("c")).toEqual(true);
|
Daniel@0
|
50 });
|
Daniel@0
|
51
|
Daniel@0
|
52 it("sets and gets parameters instantly one by one", function() {
|
Daniel@0
|
53 var testedConfig = new App.ContextModule.Config();
|
Daniel@0
|
54
|
Daniel@0
|
55 testedConfig.updateParameter("p1", 10);
|
Daniel@0
|
56 testedConfig.updateParameter("p2", null);
|
Daniel@0
|
57 testedConfig.updateParameter("p3", "string");
|
Daniel@0
|
58 testedConfig.updateParameter("p4", false);
|
Daniel@0
|
59 testedConfig.updateParameter("p5", ["array"]);
|
Daniel@0
|
60
|
Daniel@0
|
61 expect(testedConfig.getParameterValue("p1")).toEqual(10);
|
Daniel@0
|
62 expect(testedConfig.getParameterValue("p2")).toEqual(null);
|
Daniel@0
|
63 expect(testedConfig.getParameterValue("p3")).toEqual("string");
|
Daniel@0
|
64 expect(testedConfig.getParameterValue("p4")).toEqual(false);
|
Daniel@0
|
65 expect(testedConfig.getParameterValue("p5")).toEqual(["array"]);
|
Daniel@0
|
66 expect(_.size(testedConfig.attributes.parameters.attributes)).toEqual(5);
|
Daniel@0
|
67
|
Daniel@0
|
68 testedConfig.updateParameter("p5", undefined);
|
Daniel@0
|
69 expect(testedConfig.getParameterValue("p5")).toEqual(undefined);
|
Daniel@0
|
70
|
Daniel@0
|
71 expect(_.size(testedConfig.attributes.parameters.attributes)).toEqual(4);
|
Daniel@0
|
72
|
Daniel@0
|
73 expect(function() {
|
Daniel@0
|
74 testedConfig.updateParameter();
|
Daniel@0
|
75 }).toThrow();
|
Daniel@0
|
76 expect(function() {
|
Daniel@0
|
77 testedConfig.updateParameter(1, 2);
|
Daniel@0
|
78 }).toThrow();
|
Daniel@0
|
79 expect(function() {
|
Daniel@0
|
80 testedConfig.updateParameter(["a", "b", "c"]);
|
Daniel@0
|
81 }).toThrow();
|
Daniel@0
|
82 expect(function() {
|
Daniel@0
|
83 testedConfig.updateParameter({x: 1, y: 2});
|
Daniel@0
|
84 }).toThrow();
|
Daniel@0
|
85 });
|
Daniel@0
|
86
|
Daniel@0
|
87 it("sets and gets parameters instantly in bulk", function() {
|
Daniel@0
|
88 var testedConfig = new App.ContextModule.Config();
|
Daniel@0
|
89
|
Daniel@0
|
90 testedConfig.updateParameters({
|
Daniel@0
|
91 "p1": 10,
|
Daniel@0
|
92 "p2": 42,
|
Daniel@0
|
93 "p3": false,
|
Daniel@0
|
94 "p100": undefined
|
Daniel@0
|
95 });
|
Daniel@0
|
96 expect(_.size(testedConfig.attributes.parameters.attributes)).toEqual(3);
|
Daniel@0
|
97
|
Daniel@0
|
98 testedConfig.updateParameters({
|
Daniel@0
|
99 "p2": null,
|
Daniel@0
|
100 "p3": "string",
|
Daniel@0
|
101 "p4": false,
|
Daniel@0
|
102 "p5": ["array"]
|
Daniel@0
|
103 });
|
Daniel@0
|
104
|
Daniel@0
|
105 expect(testedConfig.getParameterValue("p1")).toEqual(10);
|
Daniel@0
|
106 expect(testedConfig.getParameterValue("p2")).toEqual(null);
|
Daniel@0
|
107 expect(testedConfig.getParameterValue("p3")).toEqual("string");
|
Daniel@0
|
108 expect(testedConfig.getParameterValue("p4")).toEqual(false);
|
Daniel@0
|
109 expect(testedConfig.getParameterValue("p5")).toEqual(["array"]);
|
Daniel@0
|
110 expect(_.size(testedConfig.attributes.parameters.attributes)).toEqual(5);
|
Daniel@0
|
111
|
Daniel@0
|
112 testedConfig.updateParameters({
|
Daniel@0
|
113 "p3": undefined,
|
Daniel@0
|
114 "p4": undefined,
|
Daniel@0
|
115 "p5": undefined
|
Daniel@0
|
116 });
|
Daniel@0
|
117 expect(testedConfig.getParameterValue("p3")).toEqual(undefined);
|
Daniel@0
|
118 expect(testedConfig.getParameterValue("p4")).toEqual(undefined);
|
Daniel@0
|
119 expect(testedConfig.getParameterValue("p5")).toEqual(undefined);
|
Daniel@0
|
120 expect(_.size(testedConfig.attributes.parameters.attributes)).toEqual(2);
|
Daniel@0
|
121
|
Daniel@0
|
122 expect(function() {
|
Daniel@0
|
123 testedConfig.updateParameters();
|
Daniel@0
|
124 }).toThrow();
|
Daniel@0
|
125 expect(function() {
|
Daniel@0
|
126 testedConfig.updateParameters("test", 2);
|
Daniel@0
|
127 }).toThrow();
|
Daniel@0
|
128 expect(function() {
|
Daniel@0
|
129 testedConfig.updateParameters(["a", "b", "c"]);
|
Daniel@0
|
130 }).toThrow();
|
Daniel@0
|
131 });
|
Daniel@0
|
132
|
Daniel@0
|
133 it("sets and gets planned parameter updates and applies the updates one by one", function() {
|
Daniel@0
|
134 var testedConfig = new App.ContextModule.Config();
|
Daniel@0
|
135
|
Daniel@0
|
136 testedConfig.planParameterUpdate("p1", 10);
|
Daniel@0
|
137 expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(1);
|
Daniel@0
|
138 testedConfig.planParameterUpdate("p2", "test");
|
Daniel@0
|
139 expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(2);
|
Daniel@0
|
140 testedConfig.planParameterUpdate("p3", undefined);
|
Daniel@0
|
141 expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(2);
|
Daniel@0
|
142
|
Daniel@0
|
143 expect(testedConfig.get("parameters").keys().length).toEqual(0);
|
Daniel@0
|
144
|
Daniel@0
|
145 expect(testedConfig.isPlannedToUpdate("p2")).toBe(true);
|
Daniel@0
|
146 testedConfig.cancelPlannedParameterUpdate("p2");
|
Daniel@0
|
147 expect(testedConfig.isPlannedToUpdate("p2")).toBe(false);
|
Daniel@0
|
148 expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(1);
|
Daniel@0
|
149 expect(testedConfig.isPlannedToUpdate("p1")).toBe(true);
|
Daniel@0
|
150 expect(testedConfig.isPlannedToUpdate("p3")).toBe(false);
|
Daniel@0
|
151
|
Daniel@0
|
152 expect(testedConfig.hasPlannedParameterUpdates()).toBe(true);
|
Daniel@0
|
153
|
Daniel@0
|
154 testedConfig.applyPlannedParameterUpdates();
|
Daniel@0
|
155
|
Daniel@0
|
156 testedConfig.getParameterValue("myVar");
|
Daniel@0
|
157 testedConfig.getPlannedParameterValue("myVar");
|
Daniel@0
|
158 testedConfig.isPlannedToUpdate("myVar");
|
Daniel@0
|
159
|
Daniel@0
|
160 testedConfig.applyPlannedParameterUpdates();
|
Daniel@0
|
161
|
Daniel@0
|
162 expect(function() {
|
Daniel@0
|
163 testedConfig.planParameterUpdate();
|
Daniel@0
|
164 }).toThrow();
|
Daniel@0
|
165 expect(function() {
|
Daniel@0
|
166 testedConfig.planParameterUpdate(1, 2);
|
Daniel@0
|
167 }).toThrow();
|
Daniel@0
|
168 expect(function() {
|
Daniel@0
|
169 testedConfig.planParameterUpdate(["a", "b", "c"]);
|
Daniel@0
|
170 }).toThrow();
|
Daniel@0
|
171 expect(function() {
|
Daniel@0
|
172 testedConfig.planParameterUpdate({x: 1, y: 2});
|
Daniel@0
|
173 }).toThrow();
|
Daniel@0
|
174
|
Daniel@0
|
175 });
|
Daniel@0
|
176
|
Daniel@0
|
177 it("sets and gets planned parameter updates and applies the updates in bulk", function() {
|
Daniel@0
|
178 var testedConfig = new App.ContextModule.Config();
|
Daniel@0
|
179
|
Daniel@0
|
180 testedConfig.planParameterUpdates({"p1": 10});
|
Daniel@0
|
181 expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(1);
|
Daniel@0
|
182 testedConfig.planParameterUpdates({"p2": "test", "p3": false, "p4": undefined});
|
Daniel@0
|
183 expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(3);
|
Daniel@0
|
184
|
Daniel@0
|
185 expect(testedConfig.get("parameters").keys().length).toEqual(0);
|
Daniel@0
|
186
|
Daniel@0
|
187 expect(testedConfig.isPlannedToUpdate("p2")).toBe(true);
|
Daniel@0
|
188 testedConfig.planParameterUpdates({"p3": undefined});
|
Daniel@0
|
189 expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(2);
|
Daniel@0
|
190 expect(testedConfig.isPlannedToUpdate("p1")).toBe(true);
|
Daniel@0
|
191 expect(testedConfig.isPlannedToUpdate("p2")).toBe(true);
|
Daniel@0
|
192 expect(testedConfig.isPlannedToUpdate("p3")).toBe(false);
|
Daniel@0
|
193
|
Daniel@0
|
194 expect(testedConfig.hasPlannedParameterUpdates()).toBe(true);
|
Daniel@0
|
195
|
Daniel@0
|
196 testedConfig.applyPlannedParameterUpdates();
|
Daniel@0
|
197
|
Daniel@0
|
198 expect(function() {
|
Daniel@0
|
199 testedConfig.planParameterUpdates();
|
Daniel@0
|
200 }).toThrow();
|
Daniel@0
|
201 expect(function() {
|
Daniel@0
|
202 testedConfig.planParameterUpdates("test", 2);
|
Daniel@0
|
203 }).toThrow();
|
Daniel@0
|
204 expect(function() {
|
Daniel@0
|
205 testedConfig.planParameterUpdates(["a", "b", "c"]);
|
Daniel@0
|
206 }).toThrow();
|
Daniel@0
|
207 });
|
Daniel@0
|
208
|
Daniel@0
|
209 it("sets planned parameter updates when they are undefined", function() {
|
Daniel@0
|
210 var testedConfig = new App.ContextModule.Config();
|
Daniel@0
|
211 testedConfig.updateParameters({
|
Daniel@0
|
212 "p1": "a",
|
Daniel@0
|
213 "p2": "b"
|
Daniel@0
|
214 });
|
Daniel@0
|
215
|
Daniel@0
|
216 var spy = jasmine.createSpy();
|
Daniel@0
|
217 testedConfig.on("change:plannedParameterUpdates", spy);
|
Daniel@0
|
218 expect(spy.calls.count()).toEqual(0);
|
Daniel@0
|
219
|
Daniel@0
|
220 var hashForPlannedParameterUpdatesBeforeUpdates1 = testedConfig.getHashForPlannedParameterUpdates();
|
Daniel@0
|
221
|
Daniel@0
|
222 testedConfig.planParameterUpdates({
|
Daniel@0
|
223 "p2": undefined,
|
Daniel@0
|
224 "p3": undefined,
|
Daniel@0
|
225 });
|
Daniel@0
|
226 expect(spy.calls.count()).toEqual(1);
|
Daniel@0
|
227
|
Daniel@0
|
228 expect(testedConfig.get("parameters").keys().length).toEqual(2);
|
Daniel@0
|
229 expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(1);
|
Daniel@0
|
230
|
Daniel@0
|
231 expect(testedConfig.isPlannedToUpdate("p1")).toBe(false);
|
Daniel@0
|
232 expect(testedConfig.isPlannedToUpdate("p2")).toBe(true);
|
Daniel@0
|
233 expect(testedConfig.isPlannedToUpdate("p3")).toBe(false);
|
Daniel@0
|
234
|
Daniel@0
|
235 expect(testedConfig.getPlannedParameterValue("p1")).toBe("a");
|
Daniel@0
|
236 expect(testedConfig.getPlannedParameterValue("p2")).toBe(undefined);
|
Daniel@0
|
237 expect(testedConfig.getPlannedParameterValue("p3")).toBe(undefined);
|
Daniel@0
|
238
|
Daniel@0
|
239 var hashForPlannedParameterUpdatesBeforeUpdates2 = testedConfig.getHashForPlannedParameterUpdates();
|
Daniel@0
|
240 expect(hashForPlannedParameterUpdatesBeforeUpdates2).not.toEqual(hashForPlannedParameterUpdatesBeforeUpdates1);
|
Daniel@0
|
241
|
Daniel@0
|
242 testedConfig.applyPlannedParameterUpdates();
|
Daniel@0
|
243 expect(spy.calls.count()).toEqual(2);
|
Daniel@0
|
244
|
Daniel@0
|
245 expect(testedConfig.get("parameters").keys().length).toEqual(1);
|
Daniel@0
|
246 expect(testedConfig.get("plannedParameterUpdates").keys().length).toEqual(0);
|
Daniel@0
|
247
|
Daniel@0
|
248 var hashForPlannedParameterUpdatesBeforeUpdates3 = testedConfig.getHashForPlannedParameterUpdates();
|
Daniel@0
|
249
|
Daniel@0
|
250 expect(hashForPlannedParameterUpdatesBeforeUpdates3).not.toEqual(hashForPlannedParameterUpdatesBeforeUpdates2);
|
Daniel@0
|
251 expect(hashForPlannedParameterUpdatesBeforeUpdates3).toEqual(hashForPlannedParameterUpdatesBeforeUpdates1);
|
Daniel@0
|
252 });
|
Daniel@0
|
253
|
Daniel@0
|
254 it("cancels planned changes on demand", function() {
|
Daniel@0
|
255 var testedConfig = new App.ContextModule.Config();
|
Daniel@0
|
256
|
Daniel@0
|
257 testedConfig.updateParameter("p1", 10);
|
Daniel@0
|
258
|
Daniel@0
|
259 expect(testedConfig.hasPlannedParameterUpdates()).toBe(false);
|
Daniel@0
|
260
|
Daniel@0
|
261 testedConfig.planParameterUpdate("p1", 11);
|
Daniel@0
|
262 testedConfig.planParameterUpdate("p2", "test");
|
Daniel@0
|
263
|
Daniel@0
|
264 expect(testedConfig.hasPlannedParameterUpdates()).toBe(true);
|
Daniel@0
|
265
|
Daniel@0
|
266 testedConfig.cancelPlannedParameterUpdate("p2");
|
Daniel@0
|
267 expect(testedConfig.hasPlannedParameterUpdates()).toBe(true);
|
Daniel@0
|
268 testedConfig.cancelPlannedParameterUpdate("p1");
|
Daniel@0
|
269 expect(testedConfig.hasPlannedParameterUpdates()).toBe(false);
|
Daniel@0
|
270
|
Daniel@0
|
271 testedConfig.planParameterUpdate("p1", 11);
|
Daniel@0
|
272 testedConfig.planParameterUpdate("p2", "test");
|
Daniel@0
|
273 expect(testedConfig.hasPlannedParameterUpdates()).toBe(true);
|
Daniel@0
|
274
|
Daniel@0
|
275 testedConfig.cancelPlannedParameterUpdates();
|
Daniel@0
|
276 expect(testedConfig.hasPlannedParameterUpdates()).toBe(false);
|
Daniel@0
|
277
|
Daniel@0
|
278 testedConfig.planParameterUpdate("p1", 11);
|
Daniel@0
|
279 testedConfig.planParameterUpdate("p2", "test");
|
Daniel@0
|
280 testedConfig.planParameterUpdate("p3", "test");
|
Daniel@0
|
281 testedConfig.cancelPlannedParameterUpdates(["p2", "p3"]);
|
Daniel@0
|
282 expect(testedConfig.hasPlannedParameterUpdates()).toBe(true);
|
Daniel@0
|
283 testedConfig.cancelPlannedParameterUpdates(["p100"]);
|
Daniel@0
|
284 expect(testedConfig.hasPlannedParameterUpdates()).toBe(true);
|
Daniel@0
|
285 testedConfig.cancelPlannedParameterUpdates(["p1"]);
|
Daniel@0
|
286 expect(testedConfig.hasPlannedParameterUpdates()).toBe(false);
|
Daniel@0
|
287
|
Daniel@0
|
288
|
Daniel@0
|
289 });
|
Daniel@0
|
290
|
Daniel@0
|
291 xit("returns config grid type and dimension", function() {
|
Daniel@0
|
292 //testedConfig.getConfigGridType();
|
Daniel@0
|
293 //testedConfig.getDimension();
|
Daniel@0
|
294 });
|
Daniel@0
|
295
|
Daniel@0
|
296 xit("calculates and obtains its own hashes", function() {
|
Daniel@0
|
297 //testedConfig.getHashForParameters()
|
Daniel@0
|
298 //testedConfig.getHashForPlannedParameterUpdates()
|
Daniel@0
|
299 //testedConfig.getHashForPermanent()
|
Daniel@0
|
300 //testedConfig.getHashForTemp()
|
Daniel@0
|
301 //testedConfig.getHash()
|
Daniel@0
|
302 });
|
Daniel@0
|
303
|
Daniel@0
|
304 it("serializes and unserializes itself", function() {
|
Daniel@0
|
305 var testedConfig = new App.ContextModule.Config();
|
Daniel@0
|
306 var clientId = testedConfig.getClientId();
|
Daniel@0
|
307 var originalSerializedObject = {
|
Daniel@0
|
308 "clientId": clientId,
|
Daniel@0
|
309 "parameters": {
|
Daniel@0
|
310 "p1": 10,
|
Daniel@0
|
311 "p2": "test",
|
Daniel@0
|
312 "p3": false,
|
Daniel@0
|
313 "p4": null,
|
Daniel@0
|
314 "p5": [1, "test"],
|
Daniel@0
|
315 },
|
Daniel@0
|
316 "plannedParameterUpdates": {
|
Daniel@0
|
317 "p1": 11,
|
Daniel@0
|
318 "p3": undefined,
|
Daniel@0
|
319 "p4": ["10", null],
|
Daniel@0
|
320 }
|
Daniel@0
|
321 };
|
Daniel@0
|
322 testedConfig.unserialize(originalSerializedObject);
|
Daniel@0
|
323
|
Daniel@0
|
324 expect(testedConfig.getParameterValue("p1")).toEqual(10);
|
Daniel@0
|
325 expect(testedConfig.getParameterValue("p2")).toEqual("test");
|
Daniel@0
|
326 expect(testedConfig.getParameterValue("p3")).toEqual(false);
|
Daniel@0
|
327 expect(testedConfig.getParameterValue("p4")).toEqual(null);
|
Daniel@0
|
328 expect(testedConfig.getParameterValue("p5")).toEqual([1, "test"]);
|
Daniel@0
|
329 expect(testedConfig.getParameterValue("p9")).toEqual(undefined);
|
Daniel@0
|
330
|
Daniel@0
|
331 expect(testedConfig.getPlannedParameterValue("p1")).toEqual(11);
|
Daniel@0
|
332 expect(testedConfig.getPlannedParameterValue("p2")).toEqual("test");
|
Daniel@0
|
333 expect(testedConfig.getPlannedParameterValue("p3")).toEqual(undefined);
|
Daniel@0
|
334 expect(testedConfig.getPlannedParameterValue("p4")).toEqual(["10", null]);
|
Daniel@0
|
335 expect(testedConfig.getPlannedParameterValue("p5")).toEqual([1, "test"]);
|
Daniel@0
|
336 expect(testedConfig.getPlannedParameterValue("p9")).toEqual(undefined);
|
Daniel@0
|
337
|
Daniel@0
|
338 expect(testedConfig.isPlannedToUpdate("p1")).toEqual(true);
|
Daniel@0
|
339 expect(testedConfig.isPlannedToUpdate("p2")).toEqual(false);
|
Daniel@0
|
340 expect(testedConfig.isPlannedToUpdate("p3")).toEqual(true);
|
Daniel@0
|
341 expect(testedConfig.isPlannedToUpdate("p4")).toEqual(true);
|
Daniel@0
|
342 expect(testedConfig.isPlannedToUpdate("p5")).toEqual(false);
|
Daniel@0
|
343 expect(testedConfig.isPlannedToUpdate("p9")).toEqual(false);
|
Daniel@0
|
344
|
Daniel@0
|
345 expect(testedConfig.serialize()).toEqual(originalSerializedObject);
|
Daniel@0
|
346
|
Daniel@0
|
347 testedConfig.applyPlannedParameterUpdates();
|
Daniel@0
|
348
|
Daniel@0
|
349 expect(testedConfig.serialize()).toEqual({
|
Daniel@0
|
350 "clientId": clientId,
|
Daniel@0
|
351 "parameters": {
|
Daniel@0
|
352 "p1": 11,
|
Daniel@0
|
353 "p2": "test",
|
Daniel@0
|
354 "p4": ["10", null],
|
Daniel@0
|
355 "p5": [1, "test"]
|
Daniel@0
|
356 },
|
Daniel@0
|
357 "plannedParameterUpdates": {
|
Daniel@0
|
358 }
|
Daniel@0
|
359 });
|
Daniel@0
|
360
|
Daniel@0
|
361 testedConfig.planParameterUpdate("p1", 12);
|
Daniel@0
|
362 testedConfig.planParameterUpdate("p2", undefined);
|
Daniel@0
|
363 testedConfig.planParameterUpdate("p5", [1, 2, 3]);
|
Daniel@0
|
364 testedConfig.planParameterUpdate("p9", undefined);
|
Daniel@0
|
365
|
Daniel@0
|
366 expect(testedConfig.serialize()).toEqual({
|
Daniel@0
|
367 "clientId": clientId,
|
Daniel@0
|
368 "parameters": {
|
Daniel@0
|
369 "p1": 11,
|
Daniel@0
|
370 "p2": "test",
|
Daniel@0
|
371 "p4": ["10", null],
|
Daniel@0
|
372 "p5": [1, "test"]
|
Daniel@0
|
373 },
|
Daniel@0
|
374 "plannedParameterUpdates": {
|
Daniel@0
|
375 "p1": 12,
|
Daniel@0
|
376 "p2": undefined,
|
Daniel@0
|
377 "p5": [1, 2, 3]
|
Daniel@0
|
378 }
|
Daniel@0
|
379 });
|
Daniel@0
|
380
|
Daniel@0
|
381 expect(function(){
|
Daniel@0
|
382 originalSerializedObject.clientId = 42;
|
Daniel@0
|
383 testedConfig.unserialize(originalSerializedObject);
|
Daniel@0
|
384 }).toThrow();
|
Daniel@0
|
385 });
|
Daniel@0
|
386
|
Daniel@0
|
387 it("unserializes itself from faulty serialized objects", function() {
|
Daniel@0
|
388 // Config can be unserialized from anything...
|
Daniel@0
|
389 var faultySerializedObjectPairs = [
|
Daniel@0
|
390 [null,
|
Daniel@0
|
391 {parameters: {}, plannedParameterUpdates: {}}],
|
Daniel@0
|
392 [undefined,
|
Daniel@0
|
393 {parameters: {}, plannedParameterUpdates: {}}],
|
Daniel@0
|
394 [42,
|
Daniel@0
|
395 {parameters: {}, plannedParameterUpdates: {}}],
|
Daniel@0
|
396 ["test",
|
Daniel@0
|
397 {parameters: {}, plannedParameterUpdates: {}}],
|
Daniel@0
|
398 [{parameters: 42},
|
Daniel@0
|
399 {parameters: {}, plannedParameterUpdates: {}}],
|
Daniel@0
|
400 [{parameters: ["foo", "bar"]},
|
Daniel@0
|
401 {parameters: {}, plannedParameterUpdates: {}}],
|
Daniel@0
|
402 [{plannedParameterUpdates: 10},
|
Daniel@0
|
403 {parameters: {}, plannedParameterUpdates: {}}],
|
Daniel@0
|
404 [{plannedParameterUpdates: {"x": 10}},
|
Daniel@0
|
405 {parameters: {}, plannedParameterUpdates: {"x": 10}}]
|
Daniel@0
|
406 ];
|
Daniel@0
|
407
|
Daniel@0
|
408
|
Daniel@0
|
409 _.each(faultySerializedObjectPairs, function(faultySerializedObjectPair) {
|
Daniel@0
|
410 var testedConfig = new App.ContextModule.Config(faultySerializedObjectPair[0]);
|
Daniel@0
|
411 faultySerializedObjectPair[1].clientId = testedConfig.getClientId();
|
Daniel@0
|
412 expect(testedConfig.serialize()).toEqual(faultySerializedObjectPair[1]);
|
Daniel@0
|
413
|
Daniel@0
|
414 var testedConfig2 = new App.ContextModule.Config();
|
Daniel@0
|
415 testedConfig2.unserialize(faultySerializedObjectPair[0]);
|
Daniel@0
|
416 faultySerializedObjectPair[1].clientId = testedConfig2.getClientId();
|
Daniel@0
|
417 expect(testedConfig2.serialize()).toEqual(faultySerializedObjectPair[1]);
|
Daniel@0
|
418 });
|
Daniel@0
|
419
|
Daniel@0
|
420 // ... except for when clientId does not match the internal clientId()
|
Daniel@0
|
421 var faultySerializedObjects = [
|
Daniel@0
|
422 {clientId: "wrong"},
|
Daniel@0
|
423 {clientId: 0},
|
Daniel@0
|
424 {parameters: 42, clientId: "wrong"},
|
Daniel@0
|
425 {plannedParameterUpdates: {"x": "y"}, clientId: 42},
|
Daniel@0
|
426 ];
|
Daniel@0
|
427
|
Daniel@0
|
428 var testedConfig = new App.ContextModule.Config();
|
Daniel@0
|
429
|
Daniel@0
|
430 _.each(faultySerializedObjects, function(faultySerializedObject) {
|
Daniel@0
|
431 expect(function() {
|
Daniel@0
|
432 testedConfig.unserialize(faultySerializedObject);
|
Daniel@0
|
433 }).toThrow();
|
Daniel@0
|
434 });
|
Daniel@0
|
435 });
|
Daniel@0
|
436
|
Daniel@0
|
437 it("clones itself", function() {
|
Daniel@0
|
438 var testedConfig = new App.ContextModule.Config({
|
Daniel@0
|
439 parameters: {
|
Daniel@0
|
440 a: 10,
|
Daniel@0
|
441 b: "test"
|
Daniel@0
|
442 },
|
Daniel@0
|
443 plannedParameterUpdates: {
|
Daniel@0
|
444 a: 20,
|
Daniel@0
|
445 c: true
|
Daniel@0
|
446 }
|
Daniel@0
|
447 });
|
Daniel@0
|
448
|
Daniel@0
|
449 var clonedConfig = testedConfig.clone();
|
Daniel@0
|
450
|
Daniel@0
|
451 expect(clonedConfig.getClientId()).not.toEqual(testedConfig.getClientId());
|
Daniel@0
|
452
|
Daniel@0
|
453 expect(clonedConfig.getParameterValue("a")).toEqual(10);
|
Daniel@0
|
454 expect(clonedConfig.getParameterValue("b")).toEqual("test");
|
Daniel@0
|
455 expect(clonedConfig.getParameterValue("c")).toEqual(undefined);
|
Daniel@0
|
456
|
Daniel@0
|
457 expect(clonedConfig.getPlannedParameterValue("a")).toEqual(20);
|
Daniel@0
|
458 expect(clonedConfig.getPlannedParameterValue("b")).toEqual("test");
|
Daniel@0
|
459 expect(clonedConfig.getPlannedParameterValue("c")).toEqual(true);
|
Daniel@0
|
460
|
Daniel@0
|
461 clonedConfig.updateParameter("a", 42);
|
Daniel@0
|
462 clonedConfig.planParameterUpdate("a", 43);
|
Daniel@0
|
463
|
Daniel@0
|
464 expect(clonedConfig.getParameterValue("a")).toEqual(42);
|
Daniel@0
|
465 expect(clonedConfig.getPlannedParameterValue("a")).toEqual(43);
|
Daniel@0
|
466 expect(testedConfig.getParameterValue("a")).toEqual(10);
|
Daniel@0
|
467 expect(testedConfig.getPlannedParameterValue("a")).toEqual(20);
|
Daniel@0
|
468
|
Daniel@0
|
469 });
|
Daniel@0
|
470
|
Daniel@0
|
471 it("triggers events when real changes occur", function() {
|
Daniel@0
|
472 var testedConfig = new App.ContextModule.Config();
|
Daniel@0
|
473
|
Daniel@0
|
474 var spy = jasmine.createSpyObj("listener", ["change", "changeParameters", "changePlannedParameterUpdates"]);
|
Daniel@0
|
475
|
Daniel@0
|
476 var expectSpyCallCountAndReset = function(one, two, three) {
|
Daniel@0
|
477 // console.log("~~~~~");
|
Daniel@0
|
478 // console.log("1", spy.change.calls.count(), one);
|
Daniel@0
|
479 // console.log("2", spy.changeParameters.calls.count(), two);
|
Daniel@0
|
480 // console.log("3", spy.changePlannedParameterUpdates.calls.count(), three);
|
Daniel@0
|
481 expect(spy.change.calls.count()).toEqual(one);
|
Daniel@0
|
482 expect(spy.changeParameters.calls.count()).toEqual(two);
|
Daniel@0
|
483 expect(spy.changePlannedParameterUpdates.calls.count()).toEqual(three);
|
Daniel@0
|
484 spy.changeParameters.calls.reset();
|
Daniel@0
|
485 spy.changePlannedParameterUpdates.calls.reset();
|
Daniel@0
|
486 spy.change.calls.reset();
|
Daniel@0
|
487 };
|
Daniel@0
|
488
|
Daniel@0
|
489 testedConfig.on("change", spy.change, spy);
|
Daniel@0
|
490 testedConfig.on("change:parameters", spy.changeParameters, spy);
|
Daniel@0
|
491 testedConfig.on("change:plannedParameterUpdates", spy.changePlannedParameterUpdates, spy);
|
Daniel@0
|
492
|
Daniel@0
|
493 expect(spy.change.calls.count()).toEqual(0);
|
Daniel@0
|
494 expect(spy.changeParameters.calls.count()).toEqual(0);
|
Daniel@0
|
495 expect(spy.changePlannedParameterUpdates.calls.count()).toEqual(0);
|
Daniel@0
|
496
|
Daniel@0
|
497 testedConfig.planParameterUpdate("p1", 12);
|
Daniel@0
|
498 expectSpyCallCountAndReset(1, 0, 1);
|
Daniel@0
|
499
|
Daniel@0
|
500 testedConfig.planParameterUpdate("p1", 12);
|
Daniel@0
|
501 expectSpyCallCountAndReset(0, 0, 0);
|
Daniel@0
|
502
|
Daniel@0
|
503 testedConfig.planParameterUpdate("p2", 42);
|
Daniel@0
|
504 expectSpyCallCountAndReset(1, 0, 1);
|
Daniel@0
|
505
|
Daniel@0
|
506 testedConfig.cancelPlannedParameterUpdate("p2");
|
Daniel@0
|
507 expectSpyCallCountAndReset(1, 0, 1);
|
Daniel@0
|
508
|
Daniel@0
|
509 testedConfig.applyPlannedParameterUpdates();
|
Daniel@0
|
510 expectSpyCallCountAndReset(1, 1, 1);
|
Daniel@0
|
511
|
Daniel@0
|
512 testedConfig.unserialize({"clientId": testedConfig.getClientId(), "parameters": {x: 42}, "plannedParameterUpdates": {}});
|
Daniel@0
|
513 expectSpyCallCountAndReset(1, 1, 0);
|
Daniel@0
|
514 testedConfig.unserialize({"clientId": testedConfig.getClientId(), "parameters": {x: 42}, "plannedParameterUpdates": {}});
|
Daniel@0
|
515 expectSpyCallCountAndReset(0, 0, 0);
|
Daniel@0
|
516
|
Daniel@0
|
517 testedConfig.updateParameter("p10", 10);
|
Daniel@0
|
518 expectSpyCallCountAndReset(1, 1, 0);
|
Daniel@0
|
519
|
Daniel@0
|
520 testedConfig.planParameterUpdate("p1", 12);
|
Daniel@0
|
521 expectSpyCallCountAndReset(1, 0, 1);
|
Daniel@0
|
522 testedConfig.updateParameter("p1", 100);
|
Daniel@0
|
523 expectSpyCallCountAndReset(1, 1, 1);
|
Daniel@0
|
524
|
Daniel@0
|
525 console.log("!!!", testedConfig);
|
Daniel@0
|
526 testedConfig.cancelPlannedParameterUpdates();
|
Daniel@0
|
527 expectSpyCallCountAndReset(0, 0, 0);
|
Daniel@0
|
528
|
Daniel@0
|
529 testedConfig.planParameterUpdate("p1", 12);
|
Daniel@0
|
530 expectSpyCallCountAndReset(1, 0, 1);
|
Daniel@0
|
531 testedConfig.unserialize(null);
|
Daniel@0
|
532 expectSpyCallCountAndReset(1, 1, 1);
|
Daniel@0
|
533 testedConfig.unserialize(null);
|
Daniel@0
|
534 expectSpyCallCountAndReset(0, 0, 0);
|
Daniel@0
|
535
|
Daniel@0
|
536 // BULK
|
Daniel@0
|
537 testedConfig.updateParameters({"p1": 42, "p2": 0});
|
Daniel@0
|
538 expectSpyCallCountAndReset(1, 1, 0);
|
Daniel@0
|
539 testedConfig.planParameterUpdate("p1", 42);
|
Daniel@0
|
540 expectSpyCallCountAndReset(0, 0, 0);
|
Daniel@0
|
541 testedConfig.planParameterUpdates({"p1": 42});
|
Daniel@0
|
542 expectSpyCallCountAndReset(0, 0, 0);
|
Daniel@0
|
543 testedConfig.planParameterUpdates({"p1": 4, "p2": 2, "p3": 3, "p4": 4});
|
Daniel@0
|
544 expectSpyCallCountAndReset(1, 0, 1);
|
Daniel@0
|
545 testedConfig.cancelPlannedParameterUpdates(["p3", "p4"]);
|
Daniel@0
|
546 expectSpyCallCountAndReset(1, 0, 1);
|
Daniel@0
|
547 testedConfig.cancelPlannedParameterUpdates(["p3", "p4", "p42"]);
|
Daniel@0
|
548 expectSpyCallCountAndReset(0, 0, 0);
|
Daniel@0
|
549 testedConfig.applyPlannedParameterUpdates();
|
Daniel@0
|
550 expectSpyCallCountAndReset(1, 1, 1);
|
Daniel@0
|
551 testedConfig.planParameterUpdates({"p1": 4, "p2": 2, "p3": 3, "p4": 4});
|
Daniel@0
|
552 expectSpyCallCountAndReset(1, 0, 1);
|
Daniel@0
|
553 testedConfig.cancelPlannedParameterUpdates();
|
Daniel@0
|
554 expectSpyCallCountAndReset(1, 0, 1);
|
Daniel@0
|
555
|
Daniel@0
|
556 expect(testedConfig.hasChanged()).toEqual(false);
|
Daniel@0
|
557 testedConfig.on("change", function() {
|
Daniel@0
|
558 expect(testedConfig.hasChanged()).toEqual(true);
|
Daniel@0
|
559 });
|
Daniel@0
|
560
|
Daniel@0
|
561 testedConfig.updateParameter("test", 42);
|
Daniel@0
|
562
|
Daniel@0
|
563 expect(testedConfig.hasChanged()).toEqual(false);
|
Daniel@0
|
564
|
Daniel@0
|
565 });
|
Daniel@0
|
566 });
|