comparison src/DML/MainVisBundle/Resources/assets/jasmine/marionette/[t]ContextModule.01-Config.js @ 0:493bcb69166c

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