Daniel@0
|
1 "use strict";
|
Daniel@0
|
2
|
Daniel@0
|
3 describe("ContextModule.AppContext", function() {
|
Daniel@0
|
4
|
Daniel@0
|
5 var serializedAppContexts = {
|
Daniel@0
|
6 "empty": {
|
Daniel@0
|
7 "stateHistory": {
|
Daniel@0
|
8 "currentSerializedState": {
|
Daniel@0
|
9 "musicCollectionGrid": {
|
Daniel@0
|
10 "entityConfigs": [],
|
Daniel@0
|
11 "viewConfigs": [],
|
Daniel@0
|
12 },
|
Daniel@0
|
13 "musicRecordingGrid": {
|
Daniel@0
|
14 "entityConfigs": [],
|
Daniel@0
|
15 "viewConfigs": [],
|
Daniel@0
|
16 }
|
Daniel@0
|
17 },
|
Daniel@0
|
18 "undoStack": [],
|
Daniel@0
|
19 "redoStack": []
|
Daniel@0
|
20 },
|
Daniel@0
|
21 "stateBookmarks": []
|
Daniel@0
|
22 },
|
Daniel@0
|
23
|
Daniel@0
|
24 "empty_faulty1": {
|
Daniel@0
|
25 },
|
Daniel@0
|
26 "empty_faulty2": 42,
|
Daniel@0
|
27 "empty_faulty3": null,
|
Daniel@0
|
28
|
Daniel@0
|
29 "empty_incomplete1": {
|
Daniel@0
|
30 "stateHistory": {
|
Daniel@0
|
31 "currentSerializedState": {
|
Daniel@0
|
32 "musicCollectionGrid": {
|
Daniel@0
|
33 "entityConfigs": [],
|
Daniel@0
|
34 "viewConfigs": [],
|
Daniel@0
|
35 },
|
Daniel@0
|
36 },
|
Daniel@0
|
37 "redoStack": []
|
Daniel@0
|
38 },
|
Daniel@0
|
39 "stateBookmarks": []
|
Daniel@0
|
40 },
|
Daniel@0
|
41 "empty_incomplete2": {
|
Daniel@0
|
42 "stateHistory": {
|
Daniel@0
|
43 "currentSerializedState": null,
|
Daniel@0
|
44 "undoStack": [],
|
Daniel@0
|
45 "redoStack": []
|
Daniel@0
|
46 },
|
Daniel@0
|
47 "stateBookmarks": []
|
Daniel@0
|
48 },
|
Daniel@0
|
49 };
|
Daniel@0
|
50
|
Daniel@0
|
51 it("is promptly created", function() {
|
Daniel@0
|
52 var testedAppContext = new App.ContextModule.AppContext();
|
Daniel@0
|
53
|
Daniel@0
|
54 expect(testedAppContext).not.toBe(null);
|
Daniel@0
|
55
|
Daniel@0
|
56 expect(testedAppContext.get("state")).not.toBe(null);
|
Daniel@0
|
57 expect(testedAppContext.get("state") instanceof App.ContextModule.State).toBe(true);
|
Daniel@0
|
58
|
Daniel@0
|
59 expect(testedAppContext.get("stateHistory")).not.toBe(null);
|
Daniel@0
|
60 expect(testedAppContext.get("stateHistory") instanceof App.ContextModule.StateHistory).toBe(true);
|
Daniel@0
|
61
|
Daniel@0
|
62 expect(testedAppContext.get("stateBookmarks")).not.toBe(null);
|
Daniel@0
|
63 expect(testedAppContext.get("stateBookmarks") instanceof App.ContextModule.StateBookmarkCollection).toBe(true);
|
Daniel@0
|
64
|
Daniel@0
|
65 });
|
Daniel@0
|
66
|
Daniel@0
|
67 it("serializes and unserializes itself", function() {
|
Daniel@0
|
68 var testedAppContext = new App.ContextModule.AppContext();
|
Daniel@0
|
69
|
Daniel@0
|
70 testedAppContext.unserialize(serializedAppContexts["empty"]);
|
Daniel@0
|
71 expect(testedAppContext.serialize()).toEqual(serializedAppContexts["empty"]);
|
Daniel@0
|
72
|
Daniel@0
|
73 testedAppContext.unserialize(serializedAppContexts["empty_faulty1"]);
|
Daniel@0
|
74 expect(testedAppContext.serialize()).toEqual(serializedAppContexts["empty"]);
|
Daniel@0
|
75
|
Daniel@0
|
76 testedAppContext.unserialize(serializedAppContexts["empty_faulty2"]);
|
Daniel@0
|
77 expect(testedAppContext.serialize()).toEqual(serializedAppContexts["empty"]);
|
Daniel@0
|
78
|
Daniel@0
|
79 testedAppContext.unserialize(serializedAppContexts["empty_faulty3"]);
|
Daniel@0
|
80 expect(testedAppContext.serialize()).toEqual(serializedAppContexts["empty"]);
|
Daniel@0
|
81
|
Daniel@0
|
82 testedAppContext.unserialize(serializedAppContexts["empty_incomplete1"]);
|
Daniel@0
|
83 expect(testedAppContext.serialize()).toEqual(serializedAppContexts["empty"]);
|
Daniel@0
|
84
|
Daniel@0
|
85 testedAppContext.unserialize(serializedAppContexts["empty_incomplete2"]);
|
Daniel@0
|
86 expect(testedAppContext.serialize()).toEqual(serializedAppContexts["empty"]);
|
Daniel@0
|
87 });
|
Daniel@0
|
88
|
Daniel@0
|
89 it("triggers change events when something changes", function() {
|
Daniel@0
|
90 var testedAppContext = new App.ContextModule.AppContext();
|
Daniel@0
|
91
|
Daniel@0
|
92 var spyNames = [
|
Daniel@0
|
93 "change",
|
Daniel@0
|
94 "changeState",
|
Daniel@0
|
95 "changeStateHistory",
|
Daniel@0
|
96 "changeStateBookmarks",
|
Daniel@0
|
97 ];
|
Daniel@0
|
98 var spy = jasmine.createSpyObj("listener", spyNames);
|
Daniel@0
|
99 var expectSpyCallCount = function() {
|
Daniel@0
|
100 _.each(arguments, function(arg, i) {
|
Daniel@0
|
101 //console.log("___", i, "--->", spy[spyNames[i]].calls.count(), arg);
|
Daniel@0
|
102 expect(spy[spyNames[i]].calls.count()).toEqual(arg);
|
Daniel@0
|
103 });
|
Daniel@0
|
104 for (var i = arguments.length; i < spyNames.length; i++) {
|
Daniel@0
|
105 //console.log("___", i, "--->", spy[spyNames[i]].calls.count(), 0);
|
Daniel@0
|
106 expect(spy[spyNames[i]].calls.count()).toEqual(0);
|
Daniel@0
|
107 }
|
Daniel@0
|
108 };
|
Daniel@0
|
109 var resetSpyCallCount = function() {
|
Daniel@0
|
110 _.each(spyNames, function(spyName) {
|
Daniel@0
|
111 spy[spyName].calls.reset();
|
Daniel@0
|
112 });
|
Daniel@0
|
113 };
|
Daniel@0
|
114 var expectSpyCallCountAndReset = function() {
|
Daniel@0
|
115 expectSpyCallCount.apply(null, arguments);
|
Daniel@0
|
116 resetSpyCallCount();
|
Daniel@0
|
117 };
|
Daniel@0
|
118
|
Daniel@0
|
119 testedAppContext.on("change", spy["change"]);
|
Daniel@0
|
120 testedAppContext.get("state").on("change", spy["changeState"]);
|
Daniel@0
|
121 testedAppContext.get("stateHistory").on("change", spy["changeStateHistory"]);
|
Daniel@0
|
122 testedAppContext.get("stateBookmarks").on("change", spy["changeStateBookmarks"]);
|
Daniel@0
|
123
|
Daniel@0
|
124 expectSpyCallCountAndReset(0);
|
Daniel@0
|
125
|
Daniel@0
|
126 testedAppContext.unserialize(serializedAppContexts["empty"]);
|
Daniel@0
|
127 expectSpyCallCountAndReset(0);
|
Daniel@0
|
128
|
Daniel@0
|
129 testedAppContext.unserialize(serializedAppContexts["empty_faulty1"]);
|
Daniel@0
|
130 expectSpyCallCountAndReset(0);
|
Daniel@0
|
131 expect(testedAppContext.get("stateHistory").canUndo()).toEqual(false);
|
Daniel@0
|
132 expect(testedAppContext.get("stateHistory").canRedo()).toEqual(false);
|
Daniel@0
|
133
|
Daniel@0
|
134 testedAppContext.get("state").get("musicCollectionGrid").entityConfigs.add(new App.ContextModule.Config({
|
Daniel@0
|
135 "parameters": {
|
Daniel@0
|
136 "x": 2,
|
Daniel@0
|
137 "y": 3,
|
Daniel@0
|
138 }
|
Daniel@0
|
139 }));
|
Daniel@0
|
140 expectSpyCallCountAndReset(1, 1, 1);
|
Daniel@0
|
141 testedAppContext.get("state").get("musicCollectionGrid").entityConfigs.at(0).planParameterUpdate("x", 10);
|
Daniel@0
|
142 expectSpyCallCountAndReset(1, 1, 1);
|
Daniel@0
|
143 testedAppContext.get("state").get("musicCollectionGrid").entityConfigs.at(0).applyPlannedParameterUpdates();
|
Daniel@0
|
144 expectSpyCallCountAndReset(1, 1, 1);
|
Daniel@0
|
145 testedAppContext.get("state").get("musicCollectionGrid").entityConfigs.at(0).planParameterUpdate("x", 10);
|
Daniel@0
|
146 expectSpyCallCountAndReset(0, 0, 0);
|
Daniel@0
|
147 expect(testedAppContext.get("state").get("musicCollectionGrid").entityConfigs.at(0).getParameterValue("x")).toEqual(10);
|
Daniel@0
|
148 expect(testedAppContext.get("state").get("musicCollectionGrid").entityConfigs.at(0).getPlannedParameterValue("x")).toEqual(10);
|
Daniel@0
|
149 expect(testedAppContext.get("state").get("musicCollectionGrid").entityConfigs.at(0).isPlannedToUpdate("x")).toEqual(false);
|
Daniel@0
|
150
|
Daniel@0
|
151 testedAppContext.get("stateHistory").undo();
|
Daniel@0
|
152 expectSpyCallCountAndReset(1, 1, 1);
|
Daniel@0
|
153 expect(testedAppContext.get("stateHistory").canUndo()).toEqual(true);
|
Daniel@0
|
154 expect(testedAppContext.get("stateHistory").canRedo()).toEqual(true);
|
Daniel@0
|
155 expect(testedAppContext.get("state").get("musicCollectionGrid").entityConfigs.at(0).getParameterValue("x")).toEqual(2);
|
Daniel@0
|
156 expect(testedAppContext.get("state").get("musicCollectionGrid").entityConfigs.at(0).getPlannedParameterValue("x")).toEqual(10);
|
Daniel@0
|
157 expect(testedAppContext.get("state").get("musicCollectionGrid").entityConfigs.at(0).isPlannedToUpdate("x")).toEqual(true);
|
Daniel@0
|
158
|
Daniel@0
|
159 testedAppContext.get("stateHistory").undo();
|
Daniel@0
|
160 testedAppContext.get("stateHistory").undo();
|
Daniel@0
|
161 expectSpyCallCountAndReset(2, 2, 2);
|
Daniel@0
|
162 expect(testedAppContext.get("stateHistory").canUndo()).toEqual(false);
|
Daniel@0
|
163 expect(testedAppContext.get("stateHistory").canRedo()).toEqual(true);
|
Daniel@0
|
164 expect(testedAppContext.get("state").get("musicCollectionGrid").entityConfigs.size()).toEqual(0);
|
Daniel@0
|
165
|
Daniel@0
|
166 testedAppContext.get("stateHistory").redo();
|
Daniel@0
|
167 expectSpyCallCountAndReset(1, 1, 1);
|
Daniel@0
|
168 expect(testedAppContext.get("stateHistory").canUndo()).toEqual(true);
|
Daniel@0
|
169 expect(testedAppContext.get("stateHistory").canRedo()).toEqual(true);
|
Daniel@0
|
170
|
Daniel@0
|
171 testedAppContext.unserialize(serializedAppContexts["empty"]);
|
Daniel@0
|
172 expectSpyCallCountAndReset(1, 1, 1);
|
Daniel@0
|
173 expect(testedAppContext.get("stateHistory").canUndo()).toEqual(false);
|
Daniel@0
|
174 expect(testedAppContext.get("stateHistory").canRedo()).toEqual(false);
|
Daniel@0
|
175 });
|
Daniel@0
|
176 });
|