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