-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathcancel-test.ts
229 lines (172 loc) · 5.75 KB
/
cancel-test.ts
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
import Backburner from 'backburner';
QUnit.module('tests/cancel');
QUnit.test('scheduleOnce', function(assert) {
assert.expect(3);
let bb = new Backburner(['one']);
let functionWasCalled = false;
bb.run(() => {
let timer = bb.scheduleOnce('one', () => functionWasCalled = true);
assert.ok(timer, 'Timer object was returned');
assert.ok(bb.cancel(timer), 'Cancel returned true');
assert.ok(!functionWasCalled, 'function was not called');
});
});
QUnit.test('later', function(assert) {
assert.expect(5);
let done = assert.async();
let called = false;
let bb = new Backburner(['one'], {
onBegin() {
called = true;
}
});
let functionWasCalled = false;
let timer = bb.later(() => functionWasCalled = true);
assert.ok(timer, 'Timer object was returned');
assert.ok(bb.cancel(timer), 'Cancel returned true');
assert.ok(!called, 'onBegin was not called');
setTimeout(() => {
assert.ok(!functionWasCalled, 'function was not called');
assert.ok(!called, 'onBegin was not called');
done();
}, 0);
});
QUnit.test('later with multiple pending', function(assert) {
assert.expect(7);
let done = assert.async();
let called = false;
let bb = new Backburner(['one'], {
onBegin() {
called = true;
}
});
let function1WasCalled = false;
let function2WasCalled = false;
let timer1 = bb.later(() => function1WasCalled = true);
let timer2 = bb.later(() => function2WasCalled = true);
assert.ok(timer1, 'Timer object 2 was returned');
assert.ok(bb.cancel(timer1), 'Cancel for timer 1 returned true');
assert.ok(timer2, 'Timer object 2 was returned');
assert.ok(!called, 'onBegin was not called');
setTimeout(() => {
assert.ok(!function1WasCalled, 'function 1 was not called');
assert.ok(function2WasCalled, 'function 2 was called');
assert.ok(called, 'onBegin was called');
done();
}, 10);
});
QUnit.test('later and creating a new later', function(assert) {
assert.expect(7);
let done = assert.async();
let called = false;
let bb = new Backburner(['one'], {
onBegin() {
called = true;
}
});
let function1WasCalled = false;
let function2WasCalled = false;
let timer1 = bb.later(() => function1WasCalled = true, 0);
assert.ok(timer1, 'Timer object 2 was returned');
assert.ok(bb.cancel(timer1), 'Cancel for timer 1 returned true');
let timer2 = bb.later(() => function2WasCalled = true, 1);
assert.ok(timer2, 'Timer object 2 was returned');
assert.ok(!called, 'onBegin was not called');
setTimeout(() => {
assert.ok(!function1WasCalled, 'function 1 was not called');
assert.ok(function2WasCalled, 'function 2 was called');
assert.ok(called, 'onBegin was called');
done();
}, 50);
});
QUnit.test('cancelTimers', function(assert) {
assert.expect(8);
let done = assert.async();
let bb = new Backburner(['one']);
let laterWasCalled = false;
let debounceWasCalled = false;
let throttleWasCalled = false;
let timer1 = bb.later(() => laterWasCalled = true, 0);
let timer2 = bb.debounce(() => debounceWasCalled = true, 0);
let timer3 = bb.throttle(() => throttleWasCalled = true, 0, false);
assert.ok(timer1, 'Timer object was returned');
assert.ok(timer2, 'Timer object was returned');
assert.ok(timer3, 'Timer object was returned');
assert.ok(bb.hasTimers(), 'bb has scheduled timer');
bb.cancelTimers();
setTimeout(function() {
assert.ok(!bb.hasTimers(), 'bb has no scheduled timer');
assert.ok(!laterWasCalled, 'later function was not called');
assert.ok(!debounceWasCalled, 'debounce function was not called');
assert.ok(!throttleWasCalled, 'throttle function was not called');
done();
}, 100);
});
QUnit.test('cancel during flush', function(assert) {
assert.expect(1);
let bb = new Backburner(['one']);
let functionWasCalled = false;
bb.run(() => {
let timer1 = bb.scheduleOnce('one', () => bb.cancel(timer2));
let timer2 = bb.scheduleOnce('one', () => functionWasCalled = true);
});
assert.ok(!functionWasCalled, 'function was not called');
});
QUnit.test('with target', function(assert) {
assert.expect(3);
let obj = {
___FOO___: 1
};
let bb = new Backburner(['action']);
let wasCalled = 0;
function fn() {
wasCalled++;
}
bb.run(() => {
let timer = bb.scheduleOnce('action', obj, fn);
assert.equal(wasCalled, 0);
bb.cancel(timer);
bb.scheduleOnce('action', obj, fn);
assert.equal(wasCalled, 0);
});
assert.equal(wasCalled, 1);
});
QUnit.test('no target', function(assert) {
assert.expect(3);
let bb = new Backburner(['action']);
let wasCalled = 0;
function fn() {
wasCalled++;
}
bb.run(() => {
let timer = bb.scheduleOnce('action', fn);
assert.equal(wasCalled, 0);
bb.cancel(timer);
bb.scheduleOnce('action', fn);
assert.equal(wasCalled, 0);
});
assert.equal(wasCalled, 1);
});
QUnit.test('cancel always returns boolean', function(assert) {
let bb = new Backburner(['one']);
bb.run(function() {
let timer1 = bb.schedule('one', null, function() {});
assert.equal(bb.cancel(timer1), true);
assert.equal(bb.cancel(timer1), false);
assert.equal(bb.cancel(timer1), false);
let timer2 = bb.later(function() {}, 10);
assert.equal(bb.cancel(timer2), true);
assert.equal(bb.cancel(timer2), false);
assert.equal(bb.cancel(timer2), false);
let timer3 = bb.debounce(function() {}, 10);
assert.equal(bb.cancel(timer3), true);
assert.equal(bb.cancel(timer3), false);
assert.equal(bb.cancel(timer3), false);
assert.equal(bb.cancel(undefined), false);
assert.equal(bb.cancel(null), false);
assert.equal(bb.cancel({}), false);
assert.equal(bb.cancel([]), false);
assert.equal(bb.cancel(42), false);
assert.equal(bb.cancel('42'), false);
});
});