-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathschedule-once-test.ts
334 lines (249 loc) · 8.44 KB
/
schedule-once-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
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
import Backburner from 'backburner';
QUnit.module('tests/schedule-once');
QUnit.test('when passed a function', function(assert) {
assert.expect(1);
let bb = new Backburner(['one']);
let functionWasCalled = false;
bb.run(() => {
bb.scheduleOnce('one', () => {
functionWasCalled = true;
});
});
assert.ok(functionWasCalled, 'function was called');
});
QUnit.test('when passed a target and method', function(assert) {
assert.expect(2);
let bb = new Backburner(['one']);
let functionWasCalled = false;
bb.run(() => {
bb.scheduleOnce('one', {zomg: 'hi'}, function() {
assert.equal(this.zomg, 'hi', 'the target was properly set');
functionWasCalled = true;
});
});
assert.ok(functionWasCalled, 'function was called');
});
QUnit.test('when passed a target and method name', function(assert) {
assert.expect(2);
let bb = new Backburner(['one']);
let functionWasCalled = false;
let targetObject = {
zomg: 'hi',
checkFunction() {
assert.equal(this.zomg, 'hi', 'the target was properly set');
functionWasCalled = true;
}
};
bb.run(() => bb.scheduleOnce('one', targetObject, 'checkFunction'));
assert.ok(functionWasCalled, 'function was called');
});
QUnit.test('throws when passed a null method', function(assert) {
assert.expect(1);
function onError(error) {
assert.equal('You attempted to schedule an action in a queue (deferErrors) for a method that doesn\'t exist', error.message);
}
let bb = new Backburner(['deferErrors'], {
onError
});
bb.run(() => bb.scheduleOnce('deferErrors', {zomg: 'hi'}, null));
});
QUnit.test('throws when passed an undefined method', function(assert) {
assert.expect(1);
function onError(error) {
assert.equal('You attempted to schedule an action in a queue (deferErrors) for a method that doesn\'t exist', error.message);
}
let bb = new Backburner(['deferErrors'], {
onError
});
bb.run(() => bb.scheduleOnce('deferErrors', {zomg: 'hi'}, undefined));
});
QUnit.test('throws when passed an method name that does not exists on the target', function(assert) {
assert.expect(1);
function onError(error) {
assert.equal('You attempted to schedule an action in a queue (deferErrors) for a method that doesn\'t exist', error.message);
}
let bb = new Backburner(['deferErrors'], {
onError
});
bb.run(() => bb.scheduleOnce('deferErrors', {zomg: 'hi'}, 'checkFunction'));
});
QUnit.test('when passed a target, method, and arguments', function(assert) {
assert.expect(5);
let bb = new Backburner(['one']);
let functionWasCalled = false;
bb.run(() => {
bb.scheduleOnce('one', {zomg: 'hi'}, function(a, b, c) {
assert.equal(this.zomg, 'hi', 'the target was properly set');
assert.equal(a, 1, 'the first arguments was passed in');
assert.equal(b, 2, 'the second arguments was passed in');
assert.equal(c, 3, 'the third arguments was passed in');
functionWasCalled = true;
}, 1, 2, 3);
});
assert.ok(functionWasCalled, 'function was called');
});
QUnit.test('when passed same function twice', function(assert) {
assert.expect(2);
let bb = new Backburner(['one']);
let i = 0;
let functionWasCalled = false;
function deferMethod() {
i++;
assert.equal(i, 1, 'Function should be called only once');
functionWasCalled = true;
}
bb.run(() => {
bb.scheduleOnce('one', deferMethod);
bb.scheduleOnce('one', deferMethod);
});
assert.ok(functionWasCalled, 'function was called only once');
});
QUnit.test('when passed same function twice with same target', function(assert) {
assert.expect(3);
let bb = new Backburner(['one']);
let i = 0;
let functionWasCalled = false;
function deferMethod() {
i++;
assert.equal(i, 1, 'Function should be called only once');
assert.equal(this['first'], 1, 'the target property was set');
functionWasCalled = true;
}
let argObj = {first: 1};
bb.run(() => {
bb.scheduleOnce('one', argObj, deferMethod);
bb.scheduleOnce('one', argObj, deferMethod);
});
assert.ok(functionWasCalled, 'function was called only once');
});
QUnit.test('when passed same function twice with different targets', function(assert) {
assert.expect(3);
let bb = new Backburner(['one']);
let i = 0;
function deferMethod() {
i++;
assert.equal(this['first'], 1, 'the target property was set');
}
bb.run(() => {
bb.scheduleOnce('one', {first: 1}, deferMethod);
bb.scheduleOnce('one', {first: 1}, deferMethod);
});
assert.equal(i, 2, 'function was called twice');
});
QUnit.test('when passed same function twice with same arguments and same target', function(assert) {
assert.expect(4);
let bb = new Backburner(['one']);
let i = 0;
function deferMethod(a, b) {
i++;
assert.equal(a, 1, 'First argument is set only one time');
assert.equal(b, 2, 'Second argument remains same');
assert.equal(this['first'], 1, 'the target property was set');
}
let argObj = {first: 1};
bb.run(() => {
bb.scheduleOnce('one', argObj, deferMethod, 1, 2);
bb.scheduleOnce('one', argObj, deferMethod, 1, 2);
});
assert.equal(i, 1, 'function was called once');
});
QUnit.test('when passed same function twice with same target and different arguments', function(assert) {
assert.expect(4);
let bb = new Backburner(['one']);
let i = 0;
function deferMethod(a, b) {
i++;
assert.equal(a, 3, 'First argument of only second call is set');
assert.equal(b, 2, 'Second argument remains same');
assert.equal(this['first'], 1, 'the target property was set');
}
let argObj = {first: 1};
bb.run(() => {
bb.scheduleOnce('one', argObj, deferMethod, 1, 2);
bb.scheduleOnce('one', argObj, deferMethod, 3, 2);
});
assert.equal(i, 1, 'function was called once');
});
QUnit.test('when passed same function twice with different target and different arguments', function(assert) {
assert.expect(7);
let bb = new Backburner(['one']);
let i = 0;
function deferMethod(a, b) {
i++;
if (i === 1) {
assert.equal(a, 1, 'First argument set during first call');
} else {
assert.equal(a, 3, 'First argument set during second call');
}
assert.equal(b, 2, 'Second argument remains same');
assert.equal(this['first'], 1, 'the target property was set');
}
let argObj = {first: 1};
bb.run(() => {
bb.scheduleOnce('one', {first: 1}, deferMethod, 1, 2);
bb.scheduleOnce('one', {first: 1}, deferMethod, 3, 2);
});
assert.equal(i, 2, 'function was called twice');
});
QUnit.test('when passed same function with same target after already triggering in current loop', function(assert) {
assert.expect(5);
let bb = new Backburner(['one', 'two']);
let i = 0;
function deferMethod(a) {
i++;
assert.equal(a, i, 'Correct argument is set');
assert.equal(this['first'], 1, 'the target property was set');
}
function scheduleMethod() {
bb.scheduleOnce('one', argObj, deferMethod, 2);
}
let argObj = {first: 1};
bb.run(() => {
bb.scheduleOnce('one', argObj, deferMethod, 1);
bb.scheduleOnce('two', argObj, scheduleMethod);
});
assert.equal(i, 2, 'function was called twice');
});
QUnit.test('when passed same function with same target after already triggering in current loop', function(assert) {
assert.expect(5);
let argObj = {first: 1};
let bb = new Backburner(['one', 'two'], { });
let i = 0;
function deferMethod(a) {
i++;
assert.equal(a, i, 'Correct argument is set');
assert.equal(this['first'], 1, 'the target property was set');
}
function scheduleMethod() {
bb.scheduleOnce('one', argObj, deferMethod, 2);
}
bb.run(() => {
bb.scheduleOnce('one', argObj, deferMethod, 1);
bb.scheduleOnce('two', argObj, scheduleMethod);
});
assert.equal(i, 2, 'function was called twice');
});
QUnit.test('onError', function(assert) {
assert.expect(1);
function onError(error) {
assert.equal('QUnit.test error', error.message);
}
let bb = new Backburner(['errors'], { onError });
bb.run(() => {
bb.scheduleOnce('errors', () => {
throw new Error('QUnit.test error');
});
});
});
QUnit.test('when [queueName, callback, string] args passed', function(assert) {
assert.expect(2);
let bb = new Backburner(['one']);
let functionWasCalled = false;
bb.run(() => {
bb.scheduleOnce('one', function(name) {
assert.equal(name, 'batman');
functionWasCalled = true;
}, 'batman', 100);
});
assert.ok(functionWasCalled, 'function was called');
});