comparison src/DML/MainVisBundle/Resources/assets/jasmine/marionette/[t]ContextModule.10-ConfigGrid.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 "use strict";
2
3 describe("ContextModule.ConfigGrid", function() {
4
5 var testEntityConfigs = null;
6 var testViewConfigs = null;
7 var testIrrelevantEntityConfigs = null;
8 var testIrrelevantViewConfigs = null;
9
10 beforeEach(function() {
11 testEntityConfigs = [
12 new App.ContextModule.Config({parameters: {x: 10, y: "test"}}),
13 new App.ContextModule.Config({parameters: {x: 10, y: "test"}}),
14 new App.ContextModule.Config({parameters: {x: 20}}),
15 ];
16
17 testViewConfigs = [
18 new App.ContextModule.Config({parameters: {x: 10, y: "test"}}),
19 new App.ContextModule.Config({parameters: {x: 10, y: "test"}}),
20 new App.ContextModule.Config({parameters: {x: 20}}),
21 ];
22
23 testIrrelevantEntityConfigs = [
24 new App.ContextModule.Config({parameters: {x: 10, y: "test"}}),
25 ];
26 testIrrelevantViewConfigs = [
27 new App.ContextModule.Config({parameters: {x: 10, y: "test"}}),
28 ];
29 });
30
31 it("is promptly created", function() {
32 var testedConfigGrid = new App.ContextModule.ConfigGrid();
33
34 expect(testedConfigGrid).not.toBe(null);
35
36 expect(testedConfigGrid.entityConfigs).not.toBe(null);
37 expect(testedConfigGrid.entityConfigs.size()).toEqual(0);
38
39 expect(testedConfigGrid.viewConfigs).not.toBe(null);
40 expect(testedConfigGrid.viewConfigs.size()).toEqual(0);
41
42 expect(testedConfigGrid.getType()).toBe(undefined);
43 });
44
45 it("is promptly created with type", function() {
46 var testedConfigGrid = new App.ContextModule.ConfigGrid("abc");
47
48 expect(testedConfigGrid).not.toBe(null);
49
50 expect(testedConfigGrid.getType()).toBe("abc");
51 });
52
53 it("can add parameter bags", function() {
54 var testedConfigGrid = new App.ContextModule.ConfigGrid();
55
56 testedConfigGrid.entityConfigs.add(testEntityConfigs[0]);
57 testedConfigGrid.entityConfigs.add(testEntityConfigs[1]);
58 testedConfigGrid.entityConfigs.add(testEntityConfigs[2]);
59
60 testedConfigGrid.viewConfigs.add(testViewConfigs[0]);
61 testedConfigGrid.viewConfigs.add(testViewConfigs[1]);
62 testedConfigGrid.viewConfigs.add(testViewConfigs[2]);
63
64 expect(testedConfigGrid.viewConfigs.size()).toEqual(3);
65 expect(testedConfigGrid.entityConfigs.size()).toEqual(3);
66
67 // Can't add what's already there
68 testedConfigGrid.entityConfigs.add(testEntityConfigs[2]);
69 testedConfigGrid.viewConfigs.add(testViewConfigs[2]);
70
71 expect(testedConfigGrid.viewConfigs.size()).toEqual(3);
72 expect(testedConfigGrid.entityConfigs.size()).toEqual(3);
73 });
74
75 it("can add parameter bags at a particular place", function() {
76 var testedConfigGrid = new App.ContextModule.ConfigGrid();
77
78 testedConfigGrid.entityConfigs.add(testEntityConfigs[0]);
79 testedConfigGrid.entityConfigs.add(testEntityConfigs[1]);
80 testedConfigGrid.entityConfigs.add(testEntityConfigs[2], {at: 0});
81
82 testedConfigGrid.viewConfigs.add(testViewConfigs[0]);
83 testedConfigGrid.viewConfigs.add(testViewConfigs[1]);
84 testedConfigGrid.viewConfigs.add(testViewConfigs[2], {at: 1});
85
86 expect(testedConfigGrid.viewConfigs.size()).toEqual(3);
87 expect(testedConfigGrid.entityConfigs.size()).toEqual(3);
88
89 expect(testedConfigGrid.entityConfigs.at(0)).toEqual(testEntityConfigs[2]);
90 expect(testedConfigGrid.entityConfigs.at(1)).toEqual(testEntityConfigs[0]);
91 expect(testedConfigGrid.entityConfigs.at(2)).toEqual(testEntityConfigs[1]);
92
93 expect(testedConfigGrid.viewConfigs.at(0)).toEqual(testViewConfigs[0]);
94 expect(testedConfigGrid.viewConfigs.at(1)).toEqual(testViewConfigs[2]);
95 expect(testedConfigGrid.viewConfigs.at(2)).toEqual(testViewConfigs[1]);
96 });
97
98 it("can reset", function() {
99 var testedConfigGrid = new App.ContextModule.ConfigGrid();
100
101 testedConfigGrid.entityConfigs.add(testEntityConfigs[0]);
102 testedConfigGrid.entityConfigs.add(testEntityConfigs[1]);
103 testedConfigGrid.entityConfigs.add(testEntityConfigs[2]);
104
105 testedConfigGrid.viewConfigs.add(testViewConfigs[0]);
106 testedConfigGrid.viewConfigs.add(testViewConfigs[1]);
107 testedConfigGrid.viewConfigs.add(testViewConfigs[2]);
108
109 expect(testedConfigGrid.entityConfigs.size()).toEqual(3);
110 expect(testedConfigGrid.viewConfigs.size()).toEqual(3);
111
112 testedConfigGrid.entityConfigs.reset();
113 testedConfigGrid.viewConfigs.reset();
114
115 expect(testedConfigGrid.entityConfigs.size()).toEqual(0);
116 expect(testedConfigGrid.viewConfigs.size()).toEqual(0);
117
118 testedConfigGrid.entityConfigs.add(testEntityConfigs[0]);
119 testedConfigGrid.entityConfigs.add(testEntityConfigs[1]);
120 testedConfigGrid.entityConfigs.add(testEntityConfigs[2]);
121
122 testedConfigGrid.viewConfigs.add(testViewConfigs[0]);
123 testedConfigGrid.viewConfigs.add(testViewConfigs[1]);
124 testedConfigGrid.viewConfigs.add(testViewConfigs[2]);
125 });
126
127 it("retreives neighbour entityConfigs and viewConfigs", function() {
128 var testedConfigGrid = new App.ContextModule.ConfigGrid();
129
130 testedConfigGrid.entityConfigs.add(testEntityConfigs[0]);
131 testedConfigGrid.entityConfigs.add(testEntityConfigs[1]);
132 testedConfigGrid.entityConfigs.add(testEntityConfigs[2]);
133
134 testedConfigGrid.viewConfigs.add(testViewConfigs[0]);
135 testedConfigGrid.viewConfigs.add(testViewConfigs[1]);
136 testedConfigGrid.viewConfigs.add(testViewConfigs[2]);
137
138 expect(testedConfigGrid.getPrevEntityNeighbour(testEntityConfigs[0])).toBe(undefined);
139 expect(testedConfigGrid.getPrevEntityNeighbour(testEntityConfigs[1])).toEqual(testEntityConfigs[0]);
140 expect(testedConfigGrid.getPrevEntityNeighbour(testEntityConfigs[2])).toEqual(testEntityConfigs[1]);
141 expect(testedConfigGrid.getNextEntityNeighbour(testEntityConfigs[0])).toEqual(testEntityConfigs[1]);
142 expect(testedConfigGrid.getNextEntityNeighbour(testEntityConfigs[1])).toEqual(testEntityConfigs[2]);
143 expect(testedConfigGrid.getNextEntityNeighbour(testEntityConfigs[2])).toBe(undefined);
144
145 expect(function(){testedConfigGrid.getPrevEntityNeighbour(testIrrelevantEntityConfigs[0]);}).toThrow();
146 expect(function(){testedConfigGrid.getNextEntityNeighbour(testIrrelevantEntityConfigs[0]);}).toThrow();
147 expect(function(){testedConfigGrid.getPrevEntityNeighbour(testViewConfigs[0]);}).toThrow();
148 expect(function(){testedConfigGrid.getNextEntityNeighbour(testViewConfigs[0]);}).toThrow();
149
150 expect(testedConfigGrid.getPrevViewNeighbour(testViewConfigs[0])).toEqual(undefined);
151 expect(testedConfigGrid.getPrevViewNeighbour(testViewConfigs[1])).toEqual(testViewConfigs[0]);
152 expect(testedConfigGrid.getPrevViewNeighbour(testViewConfigs[2])).toEqual(testViewConfigs[1]);
153 expect(testedConfigGrid.getNextViewNeighbour(testViewConfigs[0])).toEqual(testViewConfigs[1]);
154 expect(testedConfigGrid.getNextViewNeighbour(testViewConfigs[1])).toEqual(testViewConfigs[2]);
155 expect(testedConfigGrid.getNextViewNeighbour(testViewConfigs[2])).toEqual(undefined);
156
157 expect(function(){testedConfigGrid.getPrevViewNeighbour(testIrrelevantViewConfigs[0]);}).toThrow();
158 expect(function(){testedConfigGrid.getNextViewNeighbour(testIrrelevantViewConfigs[0]);}).toThrow();
159 expect(function(){testedConfigGrid.getPrevViewNeighbour(testEntityConfigs[0]);}).toThrow();
160 expect(function(){testedConfigGrid.getNextViewNeighbour(testEntityConfigs[0]);}).toThrow();
161 });
162
163 it("relocates entityConfigs and viewConfigs", function() {
164 var testedConfigGrid = new App.ContextModule.ConfigGrid();
165
166 _.each([
167 {
168 configCollection: testedConfigGrid.entityConfigs,
169 relocateFunctionName: "relocateEntityConfig",
170 relevantConfigs: testEntityConfigs,
171 irrelevantConfigs: testIrrelevantEntityConfigs,
172 },
173 {
174 configCollection: testedConfigGrid.viewConfigs,
175 relocateFunctionName: "relocateViewConfig",
176 relevantConfigs: testViewConfigs,
177 irrelevantConfigs: testIrrelevantViewConfigs
178 }
179 ], function(currentDimension) {
180
181 currentDimension.configCollection.add(currentDimension.relevantConfigs[0]);
182 currentDimension.configCollection.add(currentDimension.relevantConfigs[1]);
183 currentDimension.configCollection.add(currentDimension.relevantConfigs[2]);
184 expect(currentDimension.configCollection.size()).toEqual(3);
185
186 // index
187 testedConfigGrid[currentDimension.relocateFunctionName](currentDimension.relevantConfigs[1], 0);
188 expect(currentDimension.configCollection.at(0)).toEqual(currentDimension.relevantConfigs[1]);
189 expect(currentDimension.configCollection.at(1)).toEqual(currentDimension.relevantConfigs[0]);
190 expect(currentDimension.configCollection.at(2)).toEqual(currentDimension.relevantConfigs[2]);
191
192 // config
193 testedConfigGrid[currentDimension.relocateFunctionName](currentDimension.relevantConfigs[1], currentDimension.relevantConfigs[2]);
194 expect(currentDimension.configCollection.at(0)).toEqual(currentDimension.relevantConfigs[0]);
195 expect(currentDimension.configCollection.at(1)).toEqual(currentDimension.relevantConfigs[1]);
196 expect(currentDimension.configCollection.at(2)).toEqual(currentDimension.relevantConfigs[2]);
197
198 // config cid
199 testedConfigGrid[currentDimension.relocateFunctionName](currentDimension.relevantConfigs[2], currentDimension.relevantConfigs[0].getClientId());
200 expect(currentDimension.configCollection.at(0)).toEqual(currentDimension.relevantConfigs[2]);
201 expect(currentDimension.configCollection.at(1)).toEqual(currentDimension.relevantConfigs[0]);
202 expect(currentDimension.configCollection.at(2)).toEqual(currentDimension.relevantConfigs[1]);
203
204 // exceptional cases
205 expect(function(){testedConfigGrid[currentDimension.relocateFunctionName](currentDimension.irrelevantConfigs[1], 0);}).toThrow();
206 expect(function(){testedConfigGrid[currentDimension.relocateFunctionName](relevantConfigs[1], currentDimension.irrelevantConfigs[1]);}).toThrow();
207 expect(function(){testedConfigGrid[currentDimension.relocateFunctionName](currentDimension.relevantConfigs[1], -1);}).toThrow();
208 expect(function(){testedConfigGrid[currentDimension.relocateFunctionName](currentDimension.relevantConfigs[1], 4);}).toThrow();
209
210 // index - last
211 testedConfigGrid[currentDimension.relocateFunctionName](currentDimension.relevantConfigs[1], 3);
212 expect(currentDimension.configCollection.at(0)).toEqual(currentDimension.relevantConfigs[2]);
213 expect(currentDimension.configCollection.at(1)).toEqual(currentDimension.relevantConfigs[0]);
214 expect(currentDimension.configCollection.at(2)).toEqual(currentDimension.relevantConfigs[1]);
215
216 // clone of a config
217 var clone = new App.ContextModule.Config(currentDimension.relevantConfigs[1].serialize());
218 expect(function(){testedConfigGrid[currentDimension.relocateFunctionName](clone, 0);}).toThrow();
219 });
220 });
221
222 xit("works with selectedEntityConfigClientId and selectedViewConfigClientId", function() {
223 });
224
225 xit("performs combined actions: addEntityAndSelectIt, addViewAndSelectIt", function() {
226 });
227
228 xit("performs combined actions: removeEntityAndSelectNeighbour, removeViewAndSelectNeighbour", function() {
229 });
230
231 it("serializes and unserializes itself", function() {
232 var testedConfigGrid = new App.ContextModule.ConfigGrid();
233
234 var originalSerializedObject = {
235 entityConfigs: [
236 testEntityConfigs[1].serialize(),
237 testEntityConfigs[2].serialize(),
238 testEntityConfigs[0].serialize(),
239 ],
240 viewConfigs: [
241 testViewConfigs[0].serialize(),
242 testViewConfigs[1].serialize()
243 ],
244 };
245 testedConfigGrid.unserialize(originalSerializedObject);
246
247 expect(testedConfigGrid.entityConfigs.size()).toEqual(3);
248 expect(testedConfigGrid.viewConfigs.size()).toEqual(2);
249
250 expect(testedConfigGrid.entityConfigs.at(0).getClientId()).toEqual(testEntityConfigs[1].getClientId());
251 expect(testedConfigGrid.entityConfigs.at(1).getClientId()).toEqual(testEntityConfigs[2].getClientId());
252 expect(testedConfigGrid.entityConfigs.at(2).getClientId()).toEqual(testEntityConfigs[0].getClientId());
253
254 expect(testedConfigGrid.viewConfigs.at(0).getClientId()).toEqual(testViewConfigs[0].getClientId());
255 expect(testedConfigGrid.viewConfigs.at(1).getClientId()).toEqual(testViewConfigs[1].getClientId());
256
257 expect(testedConfigGrid.serialize()).toEqual(originalSerializedObject);
258
259 testedConfigGrid.relocateEntityConfig(testedConfigGrid.entityConfigs.at(2), 0);
260 testedConfigGrid.entityConfigs.remove(testedConfigGrid.entityConfigs.at(2));
261 testedConfigGrid.entityConfigs.add(testIrrelevantEntityConfigs[0]);
262
263 testedConfigGrid.viewConfigs.add(testViewConfigs[2]);
264 testedConfigGrid.relocateViewConfig(testedConfigGrid.viewConfigs.at(2), 0);
265 testedConfigGrid.relocateViewConfig(testedConfigGrid.viewConfigs.at(1), 3);
266 testedConfigGrid.viewConfigs.add(testIrrelevantViewConfigs[0]);
267
268 var newSerializedObject = {
269 entityConfigs: [
270 testEntityConfigs[0].serialize(),
271 testEntityConfigs[1].serialize(),
272 testIrrelevantEntityConfigs[0].serialize()
273 ],
274 viewConfigs: [
275 testViewConfigs[2].serialize(),
276 testViewConfigs[1].serialize(),
277 testViewConfigs[0].serialize(),
278 testIrrelevantViewConfigs[0].serialize()
279 ],
280 };
281 expect(testedConfigGrid.serialize()).toEqual(newSerializedObject);
282 });
283
284 it("unserializes itself from faulty serialized objects", function() {
285 var faultySerializedObjects = [
286 null,
287 undefined,
288 42,
289 "test",
290 {entityConfigs: 42},
291 {viewConfigs: [testViewConfigs[1].serialize()], "foo": "bar"},
292 ];
293
294 var testedConfigGrid = new App.ContextModule.ConfigGrid();
295
296 _.each(faultySerializedObjects, function(faultySerializedObject) {
297 expect(function() {
298 testedConfigGrid.unserialize(faultySerializedObject);
299 }).not.toThrow();
300 });
301 });
302
303 it("triggers events when real changes occur", function() {
304 var testedConfigGrid = new App.ContextModule.ConfigGrid();
305
306 _.each([
307 {
308 configCollection: testedConfigGrid.entityConfigs,
309 relocateFunctionName: "relocateEntityConfig",
310 relevantConfigs: testEntityConfigs,
311 irrelevantConfigs: testIrrelevantEntityConfigs,
312
313 changeLayoutEventName: "change_layout",
314 changeConfigEventPrefix: "change_entity:",
315 changeConfigNeighboursPrefix: "change_entity_neighbours:",
316 },
317 {
318 configCollection: testedConfigGrid.viewConfigs,
319 relocateFunctionName: "relocateViewConfig",
320 relevantConfigs: testViewConfigs,
321 irrelevantConfigs: testIrrelevantViewConfigs,
322
323 changeLayoutEventName: "change_layout",
324 changeConfigEventPrefix: "change_view:",
325 changeConfigNeighboursPrefix: "change_view_neighbours:",
326 }
327 ], function(currentDimension) {
328
329 var spyNames = [
330 "change",
331 "changeCollection",
332 "changeConfig0",
333 "changeConfig1",
334 "changeConfig2",
335 "changeConfigNeighbours0",
336 "changeConfigNeighbours1",
337 "changeConfigNeighbours2"
338 ];
339 var spy = jasmine.createSpyObj("listener", spyNames);
340
341 testedConfigGrid.on("change", spy.change, spy);
342 testedConfigGrid.on(currentDimension.changeLayoutEventName, spy.changeCollection, spy);
343 testedConfigGrid.on(currentDimension.changeConfigEventPrefix + currentDimension.relevantConfigs[0].getClientId(), spy.changeConfig0, spy);
344 testedConfigGrid.on(currentDimension.changeConfigEventPrefix + currentDimension.relevantConfigs[1].getClientId(), spy.changeConfig1, spy);
345 testedConfigGrid.on(currentDimension.changeConfigEventPrefix + currentDimension.relevantConfigs[2].getClientId(), spy.changeConfig2, spy);
346
347 testedConfigGrid.on(currentDimension.changeConfigNeighboursPrefix + currentDimension.relevantConfigs[0].getClientId(), spy.changeConfigNeighbours0, spy);
348 testedConfigGrid.on(currentDimension.changeConfigNeighboursPrefix + currentDimension.relevantConfigs[1].getClientId(), spy.changeConfigNeighbours1, spy);
349 testedConfigGrid.on(currentDimension.changeConfigNeighboursPrefix + currentDimension.relevantConfigs[2].getClientId(), spy.changeConfigNeighbours2, spy);
350
351 var expectSpyCallCount = function() {
352 _.each(arguments, function(arg, i) {
353 //console.log("___", i, "--->", spy[spyNames[i]].calls.count(), arg);
354 expect(spy[spyNames[i]].calls.count()).toEqual(arg);
355 });
356 for (var i = arguments.length; i < spyNames.length; i++) {
357 expect(spy[spyNames[i]].calls.count()).toEqual(0);
358 }
359 };
360 var resetSpyCallCount = function() {
361 _.each(spyNames, function(spyName) {
362 spy[spyName].calls.reset();
363 });
364 };
365 expectSpyCallCount(0);
366 resetSpyCallCount();
367
368 testedConfigGrid.unserialize(null);
369 resetSpyCallCount();
370
371 // Unserialize the whole grid
372 var defaultSerializedState = {
373 entityConfigs: [
374 testEntityConfigs[0].serialize(),
375 testEntityConfigs[1].serialize(),
376 testEntityConfigs[2].serialize(),
377 ],
378 viewConfigs: [
379 testViewConfigs[0].serialize(),
380 testViewConfigs[1].serialize(),
381 testViewConfigs[2].serialize(),
382 ],
383 };
384 testedConfigGrid.unserialize(defaultSerializedState);
385 expectSpyCallCount(1, 1);
386
387 resetSpyCallCount();
388 testedConfigGrid.unserialize(defaultSerializedState);
389 expectSpyCallCount(0, 0);
390
391 // Modify parameters
392 resetSpyCallCount();
393 expectSpyCallCount(0);
394
395 currentDimension.configCollection.at(1).updateParameter("my", 1);
396 expectSpyCallCount(1, 0,
397 0, 1, 0,
398 1, 0, 1);
399
400 resetSpyCallCount();
401 currentDimension.configCollection.at(2).planParameterUpdate("my", 1);
402 expectSpyCallCount(1, 0,
403 0, 0, 1,
404 0, 1, 0);
405
406 resetSpyCallCount();
407 currentDimension.configCollection.remove(currentDimension.configCollection.at(2));
408 expect(currentDimension.configCollection.size()).toEqual(2);
409 expectSpyCallCount(1, 1,
410 0, 0, 0,
411 0, 1, 0);
412
413 resetSpyCallCount();
414 testedConfigGrid.unserialize(defaultSerializedState);
415 expectSpyCallCount(1, 1,
416 0, 1, 0,
417 1, 1, 0);
418
419 resetSpyCallCount();
420
421 expect(function() {
422 testedConfigGrid.relocateEntityConfig(testEntityConfigs[2], 0);
423 }).toThrow();
424 expect(function() {
425 testedConfigGrid.relocateViewConfig(testViewConfigs[2], 0);
426 }).toThrow();
427
428 testedConfigGrid.relocateEntityConfig(testedConfigGrid.entityConfigs.at(1), 0);
429 testedConfigGrid.relocateViewConfig(testedConfigGrid.viewConfigs.at(1), 0);
430 expectSpyCallCount(2, 2,
431 0, 0, 0,
432 1, 1, 1);
433
434 resetSpyCallCount();
435 testedConfigGrid.unserialize(defaultSerializedState);
436 expectSpyCallCount(1, 1,
437 0, 0, 0,
438 1, 1, 1);
439
440 resetSpyCallCount();
441 currentDimension.configCollection.add(currentDimension.irrelevantConfigs[0], {at: 0});
442 expectSpyCallCount(1, 1,
443 0, 0, 0,
444 1, 0, 0);
445 });
446 });
447 });