invoker.spec.js
4.61 KB
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
/**
* @author NHN Ent. FE Development Team <dl_javascript@nhn.com>
* @fileoverview Test cases of "src/js/invoker.js"
*/
import { Promise } from '../src/js/util';
import Invoker from '../src/js/invoker';
import Command from '../src/js/interface/command';
describe('Invoker', () => {
let invoker, cmd;
beforeEach(() => {
invoker = new Invoker();
cmd = new Command({
execute: jasmine.createSpy().and.returnValue(Promise.resolve()),
undo: jasmine.createSpy().and.returnValue(Promise.resolve()),
});
});
it('"redo()" should call "command.execute" again', (done) => {
invoker
.execute(cmd)
.then(() => invoker.undo())
.then(() => {
cmd.execute.calls.reset();
return invoker.redo();
})
.then(() => {
expect(cmd.execute).toHaveBeenCalled();
done();
});
});
it('should call the "command.executeCallback" after invoke', (done) => {
const spyCallback = jasmine.createSpy();
cmd.setExecuteCallback(spyCallback);
invoker.execute(cmd).then(() => {
expect(spyCallback).toHaveBeenCalled();
done();
});
});
it('should call the "command.undoCallback" after undo', (done) => {
const spyCallback = jasmine.createSpy();
cmd.setUndoCallback(spyCallback);
invoker
.execute(cmd)
.then(() => invoker.undo())
.then(() => {
expect(spyCallback).toHaveBeenCalled();
done();
});
});
describe('invoker.customEvents', () => {
let spyEvents;
beforeEach(() => {
spyEvents = {
undoStackChanged: jasmine.createSpy(),
redoStackChanged: jasmine.createSpy(),
};
});
it(
'"invoke()" should fire a event - ' + ' "pushUndoStack" (when redoStack is empty before)"',
(done) => {
invoker.on(spyEvents);
invoker.execute(cmd).then(() => {
expect(spyEvents.undoStackChanged).toHaveBeenCalledWith(1);
expect(spyEvents.redoStackChanged).not.toHaveBeenCalled();
done();
});
}
);
it(
'"invoke()" should fire events - ' +
' "pushUndoStack", "clearRedoStack" (when redoStack is not empty before)',
(done) => {
invoker.pushRedoStack({});
invoker.on(spyEvents);
invoker.execute(cmd).then(() => {
expect(spyEvents.undoStackChanged).toHaveBeenCalledWith(1);
expect(spyEvents.redoStackChanged).toHaveBeenCalledWith(0);
done();
});
}
);
it(
'"undo()" should fire a event - ' + ' "pushRedoStack" (when undoStack is not empty after)',
(done) => {
invoker
.execute(cmd)
.then(() => invoker.execute(cmd))
.then(() => {
invoker.on(spyEvents);
return invoker.undo();
})
.then(() => {
expect(spyEvents.undoStackChanged).not.toHaveBeenCalled();
expect(spyEvents.redoStackChanged).toHaveBeenCalledWith(1);
done();
});
}
);
it(
'"undo()" should fire events - ' +
' "pushRedoStack", "emptyUndoStack" (when undoStack is empty after)',
(done) => {
invoker
.execute(cmd)
.then(() => {
invoker.on(spyEvents);
return invoker.undo();
})
.then(() => {
expect(spyEvents.redoStackChanged).toHaveBeenCalledWith(1);
expect(spyEvents.undoStackChanged).toHaveBeenCalledWith(0);
done();
});
}
);
it(
'"redo()" should fire a event - ' + ' "pushUndoStack" (when redoStack is not empty after)',
(done) => {
invoker
.execute(cmd)
.then(() => invoker.execute(cmd))
.then(() => invoker.undo())
.then(() => invoker.undo())
.then(() => {
invoker.on(spyEvents);
return invoker.redo();
})
.then(() => {
expect(spyEvents.undoStackChanged).toHaveBeenCalledWith(1);
expect(spyEvents.redoStackChanged).not.toHaveBeenCalled();
done();
});
}
);
it(
'"redo()" should fire events - ' +
' "pushUndoStack", "emptyRedoStack" (when undoStack is empty after)',
(done) => {
invoker
.execute(cmd)
.then(() => invoker.undo())
.then(() => {
invoker.on(spyEvents);
return invoker.redo(cmd);
})
.then(() => {
expect(spyEvents.undoStackChanged).toHaveBeenCalledWith(1);
expect(spyEvents.redoStackChanged).toHaveBeenCalledWith(0);
done();
});
}
);
});
});