-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
259 lines (199 loc) · 6.1 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
const test = require('ava');
const StateRewind = require('./index');
test('simple-example', t => {
const state = new StateRewind;
t.false(state.canUndo());
t.false(state.canRedo());
state.set(5);
t.true(state.canUndo());
t.false(state.canRedo());
t.is(state.get(), 5);
state.set(10);
t.is(state.get(), 10);
state.set(17);
t.is(state.get(), 17);
state.undo();
t.true(state.canUndo());
t.true(state.canRedo());
t.is(state.get(), 10);
state.redo();
t.is(state.get(), 17);
state.undo();
state.undo();
t.is(state.get(), 5);
state.redo();
state.redo();
t.is(state.get(), 17);
t.deepEqual(state.getAll(), [5, 10, 17]);
});
test('exec-example', t => {
t.plan(14); // number of tests (to make sure the callbacks are run correctly in undo's redo's)
const state = new StateRewind;
state.exec({ text: 'hello' }, function () {
t.pass();
}, function () {
t.pass();
t.pass();
}); // exec auto calls the 1st pass
t.deepEqual(state.get(), { text: 'hello' }); // 2nd
state.undo(); // should trigger the 3nd and 4th passes (2 to make sure it's different to the forward call)
t.is(state.get(), undefined); // 5th
state.redo(); // should trigger the 6th pass
t.deepEqual(state.get(), { text: 'hello' }); // 7th
state.set({ text: 'goodbye' }, function () {
t.fail(); // should never be called
}, function () {
t.pass();
t.pass();
t.pass();
t.pass();
t.pass();
}); // shouldn't change pass count
t.deepEqual(state.get(), { text: 'goodbye' }); // 8th
state.undo(); // another 5 passes bringing it to the 13th, just to prove it's good ;D
t.deepEqual(state.get(), { text: 'hello' }); // 14th
});
test('squash-example', t => {
const state = new StateRewind;
// first change
state.set({
timestamp: '2020-04-04 11:52:00',
selector: '#el span',
type: 'text',
change: {
from: 'Hello',
to: 'World'
}
});
// now a series of 3 changes to the same selector and type (we'll then swuahs these down)
state.set({
timestamp: '2020-04-04 11:54:00',
selector: '.another strong',
type: 'text',
change: {
from: 'Start text',
to: 'First change'
}
});
state.set({
timestamp: '2020-04-04 11:56:00',
selector: '.another strong',
type: 'text',
change: {
from: 'First change',
to: '2nd change'
}
});
state.set({
timestamp: '2020-04-04 11:58:00',
selector: '.another strong',
type: 'text',
change: {
from: '2nd change',
to: 'Last change!!'
}
});
state.squash(function (prev, next) {
return prev.selector == next.selector && prev.type == next.type;
}, function (prev, next) {
next.change.from = prev.change.from; // keep the initial "from text" as we squash down to the latest "to text"
return next;
});
t.deepEqual(state.getAll(), [
{
timestamp: '2020-04-04 11:52:00',
selector: '#el span',
type: 'text',
change: {
from: 'Hello',
to: 'World'
}
},
{
timestamp: '2020-04-04 11:58:00',
selector: '.another strong',
type: 'text',
change: {
from: 'Start text', // here we want the original text as we squashed down with a callback to keep this
to: 'Last change!!'
}
}
]);
});
test('change-detection', t => {
t.plan(3);
const state = new StateRewind;
// listen for changes
state.onChange(function (value) {
t.pass();
});
// trigger 3 differnt types of changes
state.set('test');
state.undo();
state.redo();
});
test('remove-index-example', t => {
t.plan(7);
const state = new StateRewind;
t.deepEqual(state.getAll(), []);
state.set(111);
state.set(222, function () {}, function () {
t.pass();
});
state.set(333);
t.deepEqual(state.getAll(), [111, 222, 333]);
state.removeIndex(1);
t.deepEqual(state.getAll(), [111, 333]);
state.clear(); // remove all from history
t.deepEqual(state.getAll(), []);
t.false(state.canUndo());
t.false(state.canRedo());
});
test('load-state-example', t => {
t.plan(4);
const state = new StateRewind;
t.deepEqual(state.getAll(), []);
state.setDefaultForwardBackwardCallback(function (direction, change) {
if (change == 'a') {
t.pass();
} else {
t.is(change, 'b');
}
});
state.load(['a', 'b'], { exec: true });
t.deepEqual(state.getAll(), ['a', 'b']);
});
test('initial-state-example', t => {
let state;
// check initial states
state = new StateRewind;
t.is(state.get(), undefined);
t.deepEqual(state.getAll(), []);
// changed initial states
state = new StateRewind({
initialState: { text: "hello" },
});
t.deepEqual(state.get(), { text: "hello" });
t.deepEqual(state.getAll(), [{ text: "hello" }]);
// first change
state.set({ text: "ok" });
t.deepEqual(state.get(), { text: "ok" });
t.deepEqual(state.getAll(), [{ text: "hello" }, { text: "ok" }]);
// second change
state.set({ text: "this is interesting" });
t.deepEqual(state.get(), { text: "this is interesting" });
t.deepEqual(state.getAll(), [{ text: "hello" }, { text: "ok" }, { text: "this is interesting"}]);
// undo second change
state.undo();
t.deepEqual(state.get(), { text: "ok" });
t.deepEqual(state.getAll(), [{ text: "hello" }, { text: "ok" }]);
// undo first change
state.undo();
t.deepEqual(state.get(), { text: "hello" });
t.deepEqual(state.getAll(), [{ text: "hello" }]);
// attempt further rollback
t.false(state.canUndo());
state.undo();
t.deepEqual(state.get(), { text: "hello" });
t.deepEqual(state.getAll(), [{ text: "hello" }]);
});