comparison src/DML/MainVisBundle/Resources/assets/jasmine/marionette/[t]ContextModule.30-StateHistory.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.StateHistory", function() {
4
5 it("is promptly created", function() {
6 var testedStateHistory = new App.ContextModule.StateHistory();
7
8 expect(testedStateHistory).not.toBe(null);
9 expect(testedStateHistory.get("maxStackSize")).toEqual(50);
10 expect(testedStateHistory.get("currentSerializedState")).toEqual(undefined);
11 expect(testedStateHistory.get("compoundChangeDetector")).toEqual(undefined);
12 });
13
14 it("is promptly created with attributes", function() {
15 var testedStateHistory = new App.ContextModule.StateHistory({
16 maxStackSize: 40,
17 currentSerializedState: 42,
18 compoundChangeDetector: function() {}
19 });
20
21 expect(testedStateHistory).not.toBe(null);
22 expect(testedStateHistory.get("maxStackSize")).toEqual(40);
23 expect(testedStateHistory.get("currentSerializedState")).toEqual(42);
24 expect(typeof testedStateHistory.get("compoundChangeDetector")).toEqual("function");
25 });
26
27 it("undoes and redoes", function() {
28 var testedStateHistory = new App.ContextModule.StateHistory({
29 currentSerializedState: {"a": 42},
30 });
31 expect(testedStateHistory.canUndo()).toBe(false);
32 expect(testedStateHistory.canRedo()).toBe(false);
33
34 testedStateHistory.set("currentSerializedState", {"a": 42});
35 expect(testedStateHistory.get("currentSerializedState")).toEqual({"a": 42});
36 expect(testedStateHistory.canUndo()).toBe(false);
37 expect(testedStateHistory.canRedo()).toBe(false);
38 expect(function() {
39 testedStateHistory.undo();
40 }).toThrow();
41 expect(function() {
42 testedStateHistory.redo();
43 }).toThrow();
44
45 testedStateHistory.set("currentSerializedState", {"a": 43, "b": 44});
46 expect(testedStateHistory.get("currentSerializedState")).toEqual({"a": 43, "b": 44});
47 expect(testedStateHistory.canUndo()).toBe(true);
48 expect(testedStateHistory.canRedo()).toBe(false);
49
50 // The same object does not create a new state in the undo stack
51 testedStateHistory.set("currentSerializedState", {"a": 42});
52 expect(testedStateHistory.get("currentSerializedState")).toEqual({"a": 42});
53 expect(testedStateHistory.canUndo()).toBe(true);
54 expect(testedStateHistory.canRedo()).toBe(false);
55
56 testedStateHistory.undo();
57 expect(testedStateHistory.get("currentSerializedState")).toEqual({"a": 43, "b": 44});
58 expect(testedStateHistory.canUndo()).toBe(true);
59 expect(testedStateHistory.canRedo()).toBe(true);
60
61 testedStateHistory.undo();
62 expect(testedStateHistory.get("currentSerializedState")).toEqual({"a": 42});
63 expect(testedStateHistory.canUndo()).toBe(false);
64 expect(testedStateHistory.canRedo()).toBe(true);
65
66 expect(function() {
67 testedStateHistory.undo();
68 }).toThrow();
69
70 testedStateHistory.redo();
71 expect(testedStateHistory.get("currentSerializedState")).toEqual({"a": 43, "b": 44});
72 expect(testedStateHistory.canUndo()).toBe(true);
73 expect(testedStateHistory.canRedo()).toBe(true);
74
75 testedStateHistory.redo();
76 expect(testedStateHistory.get("currentSerializedState")).toEqual({"a": 42});
77 expect(testedStateHistory.canUndo()).toBe(true);
78 expect(testedStateHistory.canRedo()).toBe(false);
79
80 testedStateHistory.undo();
81 expect(testedStateHistory.get("currentSerializedState")).toEqual({"a": 43, "b": 44});
82
83 // In current implementation redo stack is wasted even
84 // if the new state is exactly the same as the first element there
85 testedStateHistory.set("currentSerializedState", {"a": 42});
86 expect(testedStateHistory.get("currentSerializedState")).toEqual({"a": 42});
87 expect(testedStateHistory.canUndo()).toBe(true);
88 expect(testedStateHistory.canRedo()).toBe(false);
89
90 });
91 it("resets", function() {
92 var testedStateHistory = new App.ContextModule.StateHistory({
93 currentSerializedState: {"a": 42},
94 });
95 testedStateHistory.set("currentSerializedState", {"a": 43, "b": 44});
96 testedStateHistory.set("currentSerializedState", {"a": 43, "b": 45});
97
98 testedStateHistory.undo();
99 expect(testedStateHistory.canUndo()).toBe(true);
100 expect(testedStateHistory.canRedo()).toBe(true);
101
102 testedStateHistory.reset();
103
104 expect(testedStateHistory.canUndo()).toBe(false);
105 expect(testedStateHistory.canRedo()).toBe(false);
106
107 });
108
109 it("trims undo and redo stacks", function() {
110 var testedStateHistory = new App.ContextModule.StateHistory({
111 maxStackSize: 10,
112 });
113 for (var i = 20; i >= 0; --i) {
114 testedStateHistory.set("currentSerializedState", {"x": i});
115 }
116 for (var i = 1; i <= 10; i++) {
117 testedStateHistory.undo();
118 expect(testedStateHistory.get("currentSerializedState")).toEqual({"x": i});
119 }
120 expect(testedStateHistory.canUndo()).toBe(false);
121 expect(testedStateHistory.canRedo()).toBe(true);
122 expect(testedStateHistory.get("currentSerializedState")).toEqual({"x": 10});
123
124 testedStateHistory.set("maxStackSize", 5);
125 for (var i = 9; i >= 5; --i) {
126 testedStateHistory.redo();
127 expect(testedStateHistory.get("currentSerializedState")).toEqual({"x": i});
128 }
129
130 expect(testedStateHistory.canUndo()).toBe(true);
131 expect(testedStateHistory.canRedo()).toBe(false);
132
133 testedStateHistory.set("maxStackSize", 1);
134 testedStateHistory.undo();
135 expect(testedStateHistory.canUndo()).toBe(false);
136 testedStateHistory.redo();
137 expect(testedStateHistory.canRedo()).toBe(false);
138
139 testedStateHistory.set("maxStackSize", 3);
140 testedStateHistory.set("currentSerializedState", {"x": 100});
141 testedStateHistory.set("currentSerializedState", {"x": 200});
142 testedStateHistory.set("currentSerializedState", {"x": 300});
143 testedStateHistory.undo();
144 testedStateHistory.undo();
145 testedStateHistory.undo();
146 expect(testedStateHistory.canUndo()).toBe(false);
147 });
148
149 it("triggers change:currentSerializedState when needed", function() {
150 var spyNames = [
151 "change",
152 "change:currentSerializedState",
153 ];
154 var spy = jasmine.createSpyObj("listener", spyNames);
155 var expectSpyCallCount = function() {
156 _.each(arguments, function(arg, i) {
157 //console.log("___", i, "--->", spy[spyNames[i]].calls.count(), arg);
158 expect(spy[spyNames[i]].calls.count()).toEqual(arg);
159 });
160 for (var i = arguments.length; i < spyNames.length; i++) {
161 expect(spy[spyNames[i]].calls.count()).toEqual(0);
162 }
163 };
164 var resetSpyCallCount = function() {
165 _.each(spyNames, function(spyName) {
166 spy[spyName].calls.reset();
167 });
168 };
169 var expectSpyCallCountAndReset = function() {
170 expectSpyCallCount.apply(null, arguments);
171 resetSpyCallCount();
172 };
173
174 var testedStateHistory = new App.ContextModule.StateHistory({
175 maxStackSize: 10,
176 });
177
178 _.each(spyNames, function(spyName) {
179 testedStateHistory.on(spyName, spy[spyName]);
180 });
181
182 testedStateHistory.set("currentSerializedState", {"test": true});
183 expectSpyCallCountAndReset(1, 1);
184
185 testedStateHistory.set("currentSerializedState", {"test": true});
186 expectSpyCallCountAndReset(0, 0);
187
188 testedStateHistory.set("currentSerializedState", {"test": false});
189 expectSpyCallCountAndReset(1, 1);
190
191 testedStateHistory.undo();
192 testedStateHistory.undo();
193 expectSpyCallCountAndReset(2, 2);
194
195 testedStateHistory.redo();
196 testedStateHistory.redo();
197 expectSpyCallCountAndReset(2, 2);
198
199 testedStateHistory.undo();
200 expectSpyCallCountAndReset(1, 1);
201
202 testedStateHistory.set("currentSerializedState", {"test": true});
203 expectSpyCallCountAndReset(0, 0);
204
205 testedStateHistory.reset();
206 expectSpyCallCountAndReset(1, 0);
207 testedStateHistory.reset();
208 expectSpyCallCountAndReset(0, 0);
209
210 });
211 });