-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
test.js
463 lines (323 loc) · 11.9 KB
/
test.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
const test = require('node:test');
const assert = require('node:assert');
const sinon = require('sinon');
const debounce = require('./index.js');
// TODO: Use Node.js test timer mocking.
test('housekeeping', async () => {
assert.strictEqual(typeof debounce, 'function', 'debounce should be a function');
});
test('catch issue #3 - Debounced function executing early?', async t => {
let clock;
await t.test('should debounce with fast timeout', async () => {
clock = sinon.useFakeTimers();
const callback = sinon.spy();
const fn = debounce(callback, 100);
setTimeout(fn, 100);
setTimeout(fn, 150);
setTimeout(fn, 200);
setTimeout(fn, 250);
clock.tick(350);
assert.strictEqual(callback.callCount, 1, 'Callback should be triggered once');
clock.restore();
});
});
test('forcing execution', async t => {
let clock;
await t.test('should not execute prior to timeout', async () => {
clock = sinon.useFakeTimers();
const callback = sinon.spy();
const fn = debounce(callback, 100);
setTimeout(fn, 100);
setTimeout(fn, 150);
clock.tick(175);
assert.strictEqual(callback.callCount, 0, 'Callback should not be called yet');
clock.restore();
});
await t.test('should execute prior to timeout when flushed', async () => {
clock = sinon.useFakeTimers();
const callback = sinon.spy();
const fn = debounce(callback, 100);
setTimeout(fn, 100);
setTimeout(fn, 150);
clock.tick(175);
fn.flush();
assert.strictEqual(callback.callCount, 1, 'Callback should have been called');
clock.restore();
});
await t.test('should not execute again after timeout when flushed before the timeout', async () => {
clock = sinon.useFakeTimers();
const callback = sinon.spy();
const fn = debounce(callback, 100);
setTimeout(fn, 100);
setTimeout(fn, 150);
clock.tick(175);
fn.flush();
assert.strictEqual(callback.callCount, 1, 'Callback should have been called once');
clock.tick(225);
assert.strictEqual(callback.callCount, 1, 'Callback should not be called again after timeout');
clock.restore();
});
await t.test('should not execute on a timer after being flushed', async () => {
clock = sinon.useFakeTimers();
const callback = sinon.spy();
const fn = debounce(callback, 100);
setTimeout(fn, 100);
setTimeout(fn, 150);
clock.tick(175);
fn.flush();
assert.strictEqual(callback.callCount, 1, 'Callback should have been called once');
setTimeout(fn, 250);
clock.tick(400);
assert.strictEqual(callback.callCount, 2, 'Callback should be called again after new timeout');
clock.restore();
});
await t.test('should not execute when flushed if nothing was scheduled', async () => {
const callback = sinon.spy();
const fn = debounce(callback, 100);
fn.flush();
assert.strictEqual(callback.callCount, 0, 'Callback should not be called when flushed without scheduling');
});
await t.test('should execute with correct args when called again from within timeout', async () => {
clock = sinon.useFakeTimers();
const callback = sinon.spy(n => {
--n;
if (n > 0) {
fn(n);
}
});
const fn = debounce(callback, 100);
fn(3);
clock.tick(125);
clock.tick(250);
clock.tick(375);
assert.strictEqual(callback.callCount, 3, 'Callback should be called three times');
assert.deepStrictEqual(callback.args[0], [3], 'First call args should match');
assert.deepStrictEqual(callback.args[1], [2], 'Second call args should match');
assert.deepStrictEqual(callback.args[2], [1], 'Third call args should match');
clock.restore();
});
});
test('context check in debounced function', async t => {
await t.test('should throw an error if debounced method is called with different contexts of the same class', async () => {
function MyClass() {}
MyClass.prototype.debounced = debounce(() => {});
const instance1 = new MyClass();
const instance2 = new MyClass();
instance1.debounced();
assert.throws(() => {
instance2.debounced();
}, {
message: 'Debounced method called with different contexts of the same prototype.',
}, 'An error should have been thrown');
});
await t.test('should not throw an error if debounced method is called with different contexts of different classes', async () => {
function MyClass1() {}
function MyClass2() {}
const debouncedFunction = debounce(() => {});
MyClass1.prototype.debounced = debouncedFunction;
MyClass2.prototype.debounced = debouncedFunction;
const instance1 = new MyClass1();
const instance2 = new MyClass2();
instance1.debounced();
assert.doesNotThrow(() => {
instance2.debounced();
}, {
message: 'Debounced method called with different contexts of the same prototype.',
}, 'An error should not have been thrown');
});
});
test('immediate execution', async t => {
let clock;
await t.test('should execute immediately when immediate is true', async () => {
clock = sinon.useFakeTimers();
const callback = sinon.spy();
const fn = debounce(callback, 100, true);
fn();
assert.strictEqual(callback.callCount, 1, 'Callback should be triggered immediately');
clock.tick(100);
assert.strictEqual(callback.callCount, 1, 'Callback should not be triggered again after wait time');
clock.restore();
});
await t.test('should execute immediately when immediate is in options object', async () => {
clock = sinon.useFakeTimers();
const callback = sinon.spy();
const fn = debounce(callback, 100, {immediate: true});
fn();
assert.strictEqual(callback.callCount, 1, 'Callback should be triggered immediately');
clock.tick(100);
assert.strictEqual(callback.callCount, 1, 'Callback should not be triggered again after wait time');
clock.restore();
});
await t.test('should not execute immediately when immediate is false', async () => {
clock = sinon.useFakeTimers();
const callback = sinon.spy();
const fn = debounce(callback, 100, {immediate: false});
fn();
clock.tick(50);
assert.strictEqual(callback.callCount, 0, 'Callback should not be triggered immediately');
clock.tick(50);
assert.strictEqual(callback.callCount, 1, 'Callback should be triggered after wait time');
clock.restore();
});
});
test('debounce edge cases', async t => {
const clock = sinon.useFakeTimers();
await t.test('zero wait time', async () => {
const callback = sinon.spy();
const fn = debounce(callback, 0);
fn();
clock.tick(1);
assert.strictEqual(callback.callCount, 1, 'Callback should be triggered immediately for zero wait time');
});
await t.test('negative wait time error handling', async () => {
assert.throws(() => {
debounce(() => {}, -100);
}, RangeError, 'Debounce should throw RangeError for negative wait time');
});
await t.test('repeated rapid calls', async () => {
const callback = sinon.spy();
const fn = debounce(callback, 100);
fn();
fn();
fn();
clock.tick(100);
assert.strictEqual(callback.callCount, 1, 'Callback should be called only once after rapid calls');
});
await t.test('single call', async () => {
const callback = sinon.spy();
const fn = debounce(callback, 100);
fn();
clock.tick(100);
assert.strictEqual(callback.callCount, 1, 'Callback should be called once for a single call');
});
await t.test('long wait time', async () => {
const callback = sinon.spy();
const fn = debounce(callback, 10_000);
fn();
clock.tick(10_000);
assert.strictEqual(callback.callCount, 1, 'Callback should be called once after long wait time');
});
await t.test('function arguments preservation', async () => {
const callback = sinon.spy();
const fn = debounce(callback, 100);
fn('test', 123);
clock.tick(100);
assert.deepStrictEqual(callback.args[0], ['test', 123], 'Arguments should be preserved and passed correctly');
});
await t.test('context preservation', async () => {
const callback = sinon.spy();
const context = {a: 1};
// Bind the context to the debounced function
const fn = debounce(callback.bind(context), 100);
fn();
clock.tick(100);
assert.strictEqual(callback.firstCall.thisValue, context, 'Context should be preserved');
});
await t.test('clear method', async () => {
const callback = sinon.spy();
const fn = debounce(callback, 100);
fn();
fn.clear();
clock.tick(100);
assert.strictEqual(callback.callCount, 0, 'Clear method should cancel scheduled execution');
});
await t.test('non-function parameter error handling', async () => {
assert.throws(() => {
debounce(123, 100);
}, TypeError, 'Debounce should throw TypeError if first parameter is not a function');
});
clock.restore();
});
test('multiple independent instances', async () => {
const clock = sinon.useFakeTimers();
const callback1 = sinon.spy();
const callback2 = sinon.spy();
const fn1 = debounce(callback1, 100);
const fn2 = debounce(callback2, 200);
fn1();
fn2();
clock.tick(100);
assert.strictEqual(callback1.callCount, 1, 'First callback should be called once');
assert.strictEqual(callback2.callCount, 0, 'Second callback should not be called yet');
clock.tick(100);
assert.strictEqual(callback2.callCount, 1, 'Second callback should be called once');
clock.restore();
});
test('execution after timeout with multiple calls', async () => {
const clock = sinon.useFakeTimers();
const callback = sinon.spy();
const fn = debounce(callback, 100);
fn();
fn();
fn();
clock.tick(300);
assert.strictEqual(callback.callCount, 1, 'Callback should be executed only once after timeout');
clock.restore();
});
test('debounce method cancelled before execution', async () => {
const clock = sinon.useFakeTimers();
const callback = sinon.spy();
const fn = debounce(callback, 100);
fn();
fn.clear();
clock.tick(100);
assert.strictEqual(callback.callCount, 0, 'Callback should not be executed after being cancelled');
clock.restore();
});
test('non-standard function calls', async () => {
const clock = sinon.useFakeTimers();
const callback = sinon.spy();
const fn = debounce(callback, 100);
const context = {a: 1};
fn.call(context);
fn.apply(context);
clock.tick(100);
assert.strictEqual(callback.callCount, 1, 'Callback should handle call and apply methods correctly');
clock.restore();
});
test('no calls made', async () => {
const clock = sinon.useFakeTimers();
const callback = sinon.spy();
debounce(callback, 100);
clock.tick(100);
assert.strictEqual(callback.callCount, 0, 'Callback should not be executed if debounce function is not called');
clock.restore();
});
test('calling flush method without any scheduled execution', async () => {
const callback = sinon.spy();
const fn = debounce(callback, 100);
fn.flush();
assert.strictEqual(callback.callCount, 0, 'Callback should not be executed if flush is called without any scheduled execution');
});
test('calling the trigger function should run it immediately', async () => {
const clock = sinon.useFakeTimers();
const callback = sinon.spy();
const fn = debounce(callback, 100);
fn();
fn.trigger();
assert.strictEqual(callback.callCount, 1, 'Callback should be called once when using trigger method');
clock.tick(100);
assert.strictEqual(callback.callCount, 1, 'Callback should stay at one call after timeout');
clock.restore();
});
test('calling the trigger should not affect future function calls', async () => {
const clock = sinon.useFakeTimers();
const callback = sinon.spy();
const fn = debounce(callback, 100);
fn();
fn.trigger();
fn();
assert.strictEqual(callback.callCount, 1, 'Callback should be called once when using trigger method');
clock.tick(100);
assert.strictEqual(callback.callCount, 2, 'Callback should total two calls after timeout');
clock.restore();
});
test('should correctly handle debounce state transitions', async () => {
const callback = sinon.spy();
const fn = debounce(callback, 100);
assert.strictEqual(fn.isPending, false, 'isPending should be false for first execution');
fn();
assert.strictEqual(fn.isPending, true, 'isPending should be true within debounce delay');
fn.trigger();
assert.strictEqual(fn.isPending, false, 'isPending should be false after debounce triggered');
});