-
Notifications
You must be signed in to change notification settings - Fork 215
/
undo-manager-spec.js
643 lines (503 loc) · 21.8 KB
/
undo-manager-spec.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
var Montage = require("montage").Montage,
Set = require("montage/collections/set"),
UndoManager = require("montage/core/undo-manager").UndoManager,
Promise = require("montage/core/promise").Promise,
WAITS_FOR_TIMEOUT = 2500;
var Roster = Montage.specialize( {
constructor: {
value: function () {
this._members = [];
}
},
initWithUndoManager: {
value: function (undoManager) {
this._undoManager = undoManager;
return this;
}
},
_undoManager: {
value: null
},
undoManager: {
get: function () {
return this._undoManager;
}
},
_members: {
value: null
},
members: {
get: function () {
return this._members;
}
},
//NOTE these APIs return the add promise to expose more testing surface area, this is not a demand of the undo system
addMember: {
value: function (member, position) {
position = position || this.members.length;
this._members.splice(position, 0, member);
return this.undoManager.register("Add " + member, Promise.resolve([this.removeMember, this, member]));
}
},
removeMember: {
value: function (member) {
var index = this._members.indexOf(member);
if (index !== -1) {
this._members.splice(index, 1);
var foo = this.undoManager.register("Remove " + member, Promise.resolve([this.addMember, this, member, index]));
foo.memberName = member;
return foo;
} else {
return Promise.resolve();
}
}
},
removeAllMembers: {
value: function () {
this.undoManager.openBatch("Remove All Members");
while (this._members.length) {
this.removeMember(this._members[this._members.length - 1]);
}
this.undoManager.closeBatch();
}
},
replaceAllMembers: {
value: function () {
this.undoManager.openBatch("Replace All Members");
this.removeAllMembers();
this.addMember("Replacement")
this.undoManager.closeBatch();
}
},
testableAddMember: {
value: function (member) {
var deferredAdd = Promise.defer();
this.members.add(member);
this.undoManager.register("Add Member", deferredAdd.promise);
return deferredAdd;
}
}
});
var Text = Montage.specialize({
initWithUndoManager: {
value: function (undoManager) {
this.undoManager = undoManager;
return this;
}
},
text: {
value: "abc"
},
del: {
value: function (isSlow) {
var self = this;
var promise = isSlow ? Promise.delay(20) : Promise.resolve();
promise = promise.then(function () {
var c = self.text.charAt(self.text.length - 1);
self.text = self.text.substring(0, self.text.length - 1);
return [self.add, self, c, isSlow];
});
return self.undoManager.register("Delete character", promise);
}
},
add: {
value: function (c, isSlow) {
var self = this;
var promise = isSlow ? Promise.delay(20) : Promise.resolve();
promise = promise.then(function () {
self.text += c;
return [self.del, self, isSlow];
});
return self.undoManager.register("Add character", promise);
}
}
});
describe('core/undo-manager-spec', function () {
var undoManager, roster;
beforeEach(function () {
undoManager = new UndoManager();
roster = new Roster().initWithUndoManager(undoManager);
});
describe("initially", function () {
it("must not have anything to undo", function () {
expect(undoManager.canUndo).toBe(false);
});
it("must not have anything to redo", function () {
expect(undoManager.canRedo).toBe(false);
});
it("must not be undoing", function () {
expect(undoManager.isUndoing).toBe(false);
});
it("must not be redoing", function () {
expect(undoManager.isRedoing).toBe(false);
});
});
describe("max undo count functionality", function () {
it("must not accept new undo entries when the max undo count is 0", function () {
undoManager.maxUndoCount = 0;
roster.addMember("Alice");
expect(undoManager.canUndo).toBe(false);
});
it("must remove extra undo operations when the max undo count is reduced beyond the current count", function () {
var aliceAddition = roster.addMember("Alice"),
bobAddition = roster.addMember("Bob");
expect(undoManager.undoCount).toBe(2);
return Promise.all([aliceAddition, bobAddition]).then(function () {
undoManager.maxUndoCount = 1;
expect(undoManager.canUndo).toBe(true);
expect(undoManager.undoCount).toBe(1);
expect(undoManager.undoLabel).toBe("Add Bob");
});
});
it("should not remove undo operations when the max undo count is changed above the current count", function () {
roster.addMember("Alice");
roster.addMember("Bob");
expect(undoManager.undoCount).toBe(2);
undoManager.maxUndoCount = 2;
expect(undoManager.undoCount).toBe(2);
undoManager.maxUndoCount = 10;
expect(undoManager.undoCount).toBe(2);
});
});
describe("registering operations", function () {
it("must not register operations when registration is disabled", function () {
undoManager.registrationEnabled = false;
roster.addMember("Alice");
expect(undoManager.undoCount).toBe(0);
});
it("should add to the undo stack when not undoing or redoing", function () {
roster.addMember("Alice");
expect(undoManager.undoCount).toBe(1);
expect(undoManager.redoCount).toBe(0);
});
it("should add to the redo stack when undoing", function () {
roster.addMember("Alice");
return undoManager.undo().then(function () {
expect(undoManager.undoCount).toBe(0);
expect(undoManager.redoCount).toBe(1);
});
});
it("should add to the undo stack when redoing", function () {
roster.addMember("Alice");
var undoThenRedoPromise = undoManager.undo().then(function () {
return undoManager.redo();
});
return undoThenRedoPromise.then(function () {
expect(undoManager.undoCount).toBe(1);
expect(undoManager.redoCount).toBe(0);
});
});
it("must reject adding non-promises", function () {
expect(function () {
undoManager.register("Something", function () {});
}).toThrow();
});
it("must reject if no undo function was given", function () {
return undoManager.register("Something", Promise.resolve([]))
.then(function () {
throw new Error("should be rejected");
}, function (error) {
expect(error.message).toBe("Need undo function for 'Something' operation, not: undefined");
});
});
});
describe("performing an undo", function () {
it("should not alter the initial label if no label is provided when resolving the operationPromise", function () {
var deferredAdditionUndo = roster.testableAddMember("Alice"),
entry = undoManager._promiseOperationMap.get(deferredAdditionUndo.promise);
expect(entry.label).toBe("Add Member");
deferredAdditionUndo.resolve([Function.noop, window, "Alice"]);
deferredAdditionUndo.promise.then(function () {
expect(entry.label).toBe("Add Member");
});
});
it("should invoke the undo function with the expected parameters", function () {
var deferredAdditionUndo = roster.testableAddMember("Alice"),
spyObject = {
removeMember: function (member) {
expect(this).toBe(spyObject);
expect(member).toBe("Alice");
}
};
spyOn(spyObject, "removeMember").andCallThrough();
deferredAdditionUndo.resolve(["Test Label", spyObject.removeMember, spyObject, "Alice"]);
return undoManager.undo().then(function () {
expect(spyObject.removeMember).toHaveBeenCalled();
});
});
it("should properly invoke the undo function even if no label was provided when resolving the operationPromise", function () {
var deferredAdditionUndo = roster.testableAddMember("Alice"),
spyObject = {
removeMember: function (member) {
expect(this).toBe(spyObject);
expect(member).toBe("Alice");
}
};
spyOn(spyObject, "removeMember").andCallThrough();
deferredAdditionUndo.resolve([spyObject.removeMember, spyObject, "Alice"]);
return undoManager.undo().then(function () {
expect(spyObject.removeMember).toHaveBeenCalled();
});
});
it("should invoke the redo function with the expected parameters", function () {
var deferredAdditionUndo = roster.testableAddMember("Alice"),
spyObject = {
removeMember: function (member) {
undoManager.register("Spy: Add Member", Promise.resolve(["Spy Resolved Add Member", this.addMember, this, member]));
},
addMember: function (member) {
expect(this).toBe(spyObject);
expect(member).toBe("Alice");
}
};
deferredAdditionUndo.resolve(["Test Label", spyObject.removeMember, spyObject, "Alice"]);
spyOn(spyObject, "addMember").andCallThrough();
return undoManager.undo().then(function () {
return undoManager.redo();
}).then(function () {
expect(spyObject.addMember).toHaveBeenCalled();
});
});
it("should invoke multiple undos newest to oldest", function () {
roster.addMember("Alice");
roster.addMember("Bob");
var bobUndo = undoManager.undo();
var aliceUndo = undoManager.undo();
return Promise.all([bobUndo, aliceUndo]).then(function () {
expect(roster.members.length).toBe(0);
});
});
it("should invoke multiple undos newest to oldest with interleaving undoable operations", function () {
roster.addMember("Alice");
roster.addMember("Bob");
var bobUndo = undoManager.undo();
roster.addMember("Carol");
var carolUndo = undoManager.undo();
return Promise.all([bobUndo, carolUndo]).then(function () {
expect(roster.members.length).toBe(1);
expect(roster.members.has("Alice")).toBe(true);
});
});
it("should not be affected by multiple no-op undos", function () {
roster.addMember("Alice");
return Promise.all([undoManager.undo(), undoManager.undo(), undoManager.undo()]).then(function () {
expect(roster.members.length).toBe(0);
return undoManager.redo();
}).then(function () {
expect(roster.members.length).toBe(1);
expect(roster.members.has("Alice")).toBe(true);
});
});
it("should not be affected by multiple no-op redos", function () {
roster.addMember("Alice");
return undoManager.undo().then(function () {
expect(roster.members.length).toBe(0);
return Promise.all([undoManager.redo(), undoManager.redo(), undoManager.redo()])
.then(function () {
expect(roster.members.length).toBe(1);
expect(roster.members.has("Alice")).toBe(true);
return undoManager.undo();
});
}).then(function () {
expect(roster.members.length).toBe(0);
});
});
it("should correctly reject an exceptional undo operation", function () {
var error = new Error("Undo Operation Exception");
var deferredAdditionUndo = roster.testableAddMember("Alice"),
spyObject = {
removeMember: function (member) {
throw error;
}
};
spyOn(spyObject, "removeMember").andCallThrough();
deferredAdditionUndo.resolve(["Test Label", spyObject.removeMember, spyObject, "Alice"]);
return undoManager.undo().fail(function (failure) {
expect(spyObject.removeMember).toHaveBeenCalled();
expect(failure).toBe(error);
}).timeout(WAITS_FOR_TIMEOUT);
});
it("should report the expected undoLabel while in the middle of undoing", function () {
var deferredAdditionUndo = roster.testableAddMember("Alice"),
spyObject = {
removeMember: function (member) {
expect(undoManager.undoLabel).toBe("Test Label");
}
};
spyOn(spyObject, "removeMember").andCallThrough();
deferredAdditionUndo.resolve(["Test Label", spyObject.removeMember, spyObject, "Alice"]);
return undoManager.undo().then(function (success) {
expect(spyObject.removeMember).toHaveBeenCalled();
}).timeout(WAITS_FOR_TIMEOUT);
});
it("maintains order when an operation is a long running async operation", function () {
var text = new Text().initWithUndoManager(undoManager);
text.del(true);
text.del(false);
undoManager.undo().done();
return undoManager.undo().then(function () {
expect(text.text).toBe("abc");
});
});
});
describe("batch operations", function () {
beforeEach(function() {
roster._members = ["Alice", "Bob", "Carol"];
});
describe("registration", function () {
it("should consider multiple undo registrations within a single batch as a single undo operation", function () {
roster.removeAllMembers();
expect(undoManager.undoCount).toBe(1);
expect(undoManager.undoLabel).toBe("Remove All Members");
});
});
describe("undoing", function () {
it("should perform all undo operations within the batch operation", function () {
roster.removeAllMembers();
return undoManager.undo().then(function () {
expect(roster.members.length).toBe(3);
expect(roster.members[0]).toBe('Alice');
expect(roster.members[1]).toBe('Bob');
expect(roster.members[2]).toBe('Carol');
}).timeout(WAITS_FOR_TIMEOUT);
});
it("should perform the undos in the oposite order to the registrations", function () {
undoManager.openBatch("Ordered removal");
roster.removeMember('Carol');
roster.removeMember('Bob');
undoManager.closeBatch();
return undoManager.undo().then(function () {
expect(roster.members.length).toBe(3);
expect(roster.members[0]).toBe('Alice');
expect(roster.members[1]).toBe('Bob');
expect(roster.members[2]).toBe('Carol');
}).timeout(WAITS_FOR_TIMEOUT);
});
});
describe("redoing", function () {
it("should perform all redo operations within the batch operation", function () {
roster.removeAllMembers();
return undoManager.undo().then(function () {
return undoManager.redo();
}).then(function () {
expect(roster.members.length).toBe(0);
}).timeout(WAITS_FOR_TIMEOUT);
});
it("should perform all redo operations within a batch itself", function () {
roster.removeAllMembers();
return undoManager.undo().then(function () {
return undoManager.redo();
}).then(function () {
expect(undoManager.undoCount).toBe(1);
expect(undoManager.redoCount).toBe(0);
}).timeout(WAITS_FOR_TIMEOUT);
});
});
it("maintains order when an operation is a long running async operation", function () {
var text = new Text().initWithUndoManager(undoManager);
undoManager.openBatch("Delete 2 characters");
text.del(false);
text.del(true);
undoManager.closeBatch();
return undoManager.undo().then(function () {
expect(text.text).toBe("abc");
});
});
});
describe("nested batch operations", function () {
beforeEach(function() {
roster._members = ["Alice", "Bob", "Carol"];
});
describe("registration", function () {
it("should consider multiple undo batch registrations within a single parent batch as a single undo operation", function () {
roster.replaceAllMembers();
expect(undoManager.undoCount).toBe(1);
expect(undoManager.undoLabel).toBe("Replace All Members");
});
});
describe("undoing", function () {
it("should perform all undo batch operations within the parent batch operation", function () {
roster.replaceAllMembers();
return undoManager.undo().then(function () {
expect(roster.members.length).toBe(3);
expect(roster.members[0]).toBe('Alice');
expect(roster.members[1]).toBe('Bob');
expect(roster.members[2]).toBe('Carol');
}).timeout(WAITS_FOR_TIMEOUT);
});
});
describe("redoing", function () {
it("should perform all batch redo operations within the parent batch operation", function () {
roster.replaceAllMembers();
return undoManager.undo().then(function () {
return undoManager.redo();
}).then(function () {
expect(roster.members.length).toBe(1);
expect(roster.members[0]).toBe('Replacement');
}).timeout(WAITS_FOR_TIMEOUT);
});
it("should perform all redo operations within a batch itself", function () {
roster.replaceAllMembers();
return undoManager.undo().then(function () {
return undoManager.redo();
}).then(function () {
expect(undoManager.undoCount).toBe(1);
expect(undoManager.redoCount).toBe(0);
}).timeout(WAITS_FOR_TIMEOUT);
});
});
});
describe("event", function () {
var listener;
beforeEach(function () {
listener = {
handleOperationRegistered: function () {},
handleUndo: function () {},
handleRedo: function () {}
};
undoManager.addEventListener("operationRegistered", listener, false);
undoManager.addEventListener("undo", listener, false);
undoManager.addEventListener("redo", listener, false);
});
it("operationRegistered is dispatched when an operation is added", function () {
spyOn(listener, "handleOperationRegistered");
return roster.addMember("Alice")
.then(function () {
expect(listener.handleOperationRegistered).toHaveBeenCalled();
});
});
it("operationRegistered is not dispatched when an operation is added as a result of an undo", function () {
return roster.addMember("Alice")
.then(function () {
spyOn(listener, "handleOperationRegistered");
return undoManager.undo();
})
.then(function () {
expect(listener.handleOperationRegistered).not.toHaveBeenCalled();
});
});
it("undo is dispatched when an undo is performed", function () {
return roster.addMember("Alice")
.then(function () {
spyOn(listener, "handleUndo");
return undoManager.undo();
})
.then(function () {
expect(listener.handleUndo).toHaveBeenCalled();
});
});
it("redo is dispatched when a redo is performed", function () {
return roster.addMember("Alice")
.then(function () {
return undoManager.undo();
})
.then(function () {
spyOn(listener, "handleRedo");
return undoManager.redo();
})
.then(function () {
expect(listener.handleRedo).toHaveBeenCalled();
});
});
});
});