2017-09-27 02:33:08 +08:00
|
|
|
import UndoStack from '../src/undo-stack';
|
2016-05-25 02:48:33 +08:00
|
|
|
|
2017-09-27 02:33:08 +08:00
|
|
|
describe('UndoStack', function UndoStackSpecs() {
|
2016-05-25 02:48:33 +08:00
|
|
|
beforeEach(() => {
|
2016-10-18 08:59:33 +08:00
|
|
|
this.undoManager = new UndoStack();
|
2016-05-25 02:48:33 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
advanceClock(500);
|
2017-09-27 02:33:08 +08:00
|
|
|
});
|
2016-05-25 02:48:33 +08:00
|
|
|
|
2017-09-27 02:33:08 +08:00
|
|
|
describe('undo', () => {
|
|
|
|
it('can restore history items, and returns null when none are available', () => {
|
|
|
|
this.undoManager.save('A');
|
|
|
|
this.undoManager.save('B');
|
|
|
|
this.undoManager.save('C');
|
|
|
|
expect(this.undoManager.current()).toBe('C');
|
|
|
|
expect(this.undoManager.undo()).toBe('B');
|
|
|
|
expect(this.undoManager.current()).toBe('B');
|
|
|
|
expect(this.undoManager.undo()).toBe('A');
|
|
|
|
expect(this.undoManager.current()).toBe('A');
|
|
|
|
expect(this.undoManager.undo()).toBe(null);
|
|
|
|
expect(this.undoManager.current()).toBe('A');
|
2016-05-25 02:48:33 +08:00
|
|
|
});
|
|
|
|
|
2017-09-27 02:33:08 +08:00
|
|
|
it('limits the undo stack to the MAX_HISTORY_SIZE', () => {
|
|
|
|
this.undoManager._MAX_STACK_SIZE = 3;
|
|
|
|
this.undoManager.save('A');
|
|
|
|
this.undoManager.save('B');
|
|
|
|
this.undoManager.save('C');
|
|
|
|
this.undoManager.save('D');
|
|
|
|
expect(this.undoManager.current()).toBe('D');
|
|
|
|
expect(this.undoManager.undo()).toBe('C');
|
|
|
|
expect(this.undoManager.undo()).toBe('B');
|
|
|
|
expect(this.undoManager.undo()).toBe(null);
|
|
|
|
expect(this.undoManager.current()).toBe('B');
|
2016-05-25 02:48:33 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-09-27 02:33:08 +08:00
|
|
|
describe('undo followed by redo', () => {
|
|
|
|
it('can restore previously undone history items', () => {
|
|
|
|
this.undoManager.save('A');
|
|
|
|
this.undoManager.save('B');
|
|
|
|
this.undoManager.save('C');
|
|
|
|
expect(this.undoManager.current()).toBe('C');
|
|
|
|
expect(this.undoManager.undo()).toBe('B');
|
|
|
|
expect(this.undoManager.current()).toBe('B');
|
|
|
|
expect(this.undoManager.redo()).toBe('C');
|
|
|
|
expect(this.undoManager.current()).toBe('C');
|
2016-05-25 02:48:33 +08:00
|
|
|
});
|
|
|
|
|
2017-09-27 02:33:08 +08:00
|
|
|
it('cannot be used after pushing additional items', () => {
|
|
|
|
this.undoManager.save('A');
|
|
|
|
this.undoManager.save('B');
|
|
|
|
this.undoManager.save('C');
|
|
|
|
expect(this.undoManager.current()).toBe('C');
|
|
|
|
expect(this.undoManager.undo()).toBe('B');
|
|
|
|
this.undoManager.save('D');
|
|
|
|
expect(this.undoManager.redo()).toBe(null);
|
|
|
|
expect(this.undoManager.current()).toBe('D');
|
2016-05-25 02:48:33 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|