-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebounce.spec.ts
376 lines (261 loc) · 9.18 KB
/
debounce.spec.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
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
import { debounce } from './debounce';
import { identity } from './identity';
describe('main functionality', () => {
it('should debounce a function', async () => {
const mockFn = vi.fn(identity());
const debouncer = debounce(mockFn, { waitMs: 32 });
expect([
debouncer.call('a'),
debouncer.call('b'),
debouncer.call('c'),
]).toStrictEqual([undefined, undefined, undefined]);
expect(mockFn).toHaveBeenCalledTimes(0);
await sleep(128);
expect(mockFn).toHaveBeenCalledTimes(1);
expect([
debouncer.call('d'),
debouncer.call('e'),
debouncer.call('f'),
]).toStrictEqual(['c', 'c', 'c']);
expect(mockFn).toHaveBeenCalledTimes(1);
await sleep(256);
expect(mockFn).toHaveBeenCalledTimes(2);
});
it('subsequent debounced calls return the last `func` result', async () => {
const debouncer = debounce(identity(), { waitMs: 32 });
debouncer.call('a');
await sleep(64);
expect(debouncer.call('b')).toBe('a');
await sleep(128);
expect(debouncer.call('c')).toBe('b');
});
it('should not immediately call `func` when `wait` is `0`', async () => {
const mockFn = vi.fn();
const debouncer = debounce(mockFn, {});
debouncer.call();
debouncer.call();
expect(mockFn).toHaveBeenCalledTimes(0);
await sleep(5);
expect(mockFn).toHaveBeenCalledTimes(1);
});
it('should apply default options', async () => {
const mockFn = vi.fn();
const debouncer = debounce(mockFn, { waitMs: 32 });
debouncer.call();
expect(mockFn).toHaveBeenCalledTimes(0);
await sleep(64);
expect(mockFn).toHaveBeenCalledTimes(1);
});
it('should support a `leading` option', async () => {
const leadingMockFn = vi.fn();
const bothMockFn = vi.fn();
const withLeading = debounce(leadingMockFn, {
waitMs: 32,
timing: 'leading',
});
withLeading.call();
expect(leadingMockFn).toHaveBeenCalledTimes(1);
const withLeadingAndTrailing = debounce(bothMockFn, {
waitMs: 32,
timing: 'both',
});
withLeadingAndTrailing.call();
withLeadingAndTrailing.call();
expect(bothMockFn).toHaveBeenCalledTimes(1);
await sleep(64);
expect(bothMockFn).toHaveBeenCalledTimes(2);
withLeading.call();
expect(leadingMockFn).toHaveBeenCalledTimes(2);
});
it('subsequent leading debounced calls return the last `func` result', async () => {
const debouncer = debounce(identity(), { waitMs: 32, timing: 'leading' });
expect([debouncer.call('a'), debouncer.call('b')]).toStrictEqual([
'a',
'a',
]);
await sleep(64);
expect([debouncer.call('c'), debouncer.call('d')]).toStrictEqual([
'c',
'c',
]);
});
it('should support a `trailing` option', async () => {
const mockFn = vi.fn();
const withTrailing = debounce(mockFn, { waitMs: 32, timing: 'trailing' });
withTrailing.call();
expect(mockFn).toHaveBeenCalledTimes(0);
await sleep(64);
expect(mockFn).toHaveBeenCalledTimes(1);
});
});
describe('optional param maxWaitMs', () => {
it('should support a `maxWait` option', async () => {
const mockFn = vi.fn(identity());
const debouncer = debounce(mockFn, { waitMs: 32, maxWaitMs: 64 });
debouncer.call('a');
debouncer.call('b');
expect(mockFn).toHaveBeenCalledTimes(0);
await sleep(128);
expect(mockFn).toHaveBeenCalledTimes(1);
debouncer.call('c');
debouncer.call('d');
expect(mockFn).toHaveBeenCalledTimes(1);
await sleep(256);
expect(mockFn).toHaveBeenCalledTimes(2);
});
it('should support `maxWait` in a tight loop', async () => {
const withMockFn = vi.fn();
const withoutMockFn = vi.fn();
const withMaxWait = debounce(withMockFn, { waitMs: 32, maxWaitMs: 128 });
const withoutMaxWait = debounce(withoutMockFn, { waitMs: 96 });
const start = Date.now();
while (Date.now() - start < 320) {
withMaxWait.call();
withoutMaxWait.call();
}
await sleep(1);
expect(withoutMockFn).toHaveBeenCalledTimes(0);
expect(withMockFn).not.toHaveBeenCalledTimes(0);
});
it('should queue a trailing call for subsequent debounced calls after `maxWait`', async () => {
const mockFn = vi.fn();
const debouncer = debounce(mockFn, { waitMs: 200, maxWaitMs: 200 });
debouncer.call();
setTimeout(() => {
debouncer.call();
}, 190);
setTimeout(() => {
debouncer.call();
}, 200);
setTimeout(() => {
debouncer.call();
}, 210);
await sleep(500);
expect(mockFn).toHaveBeenCalledTimes(2);
});
it('should cancel `maxDelayed` when `delayed` is invoked', async () => {
const mockFn = vi.fn();
const debouncer = debounce(mockFn, { waitMs: 32, maxWaitMs: 64 });
debouncer.call();
await sleep(128);
debouncer.call();
expect(mockFn).toHaveBeenCalledTimes(1);
await sleep(192);
expect(mockFn).toHaveBeenCalledTimes(2);
});
it('works like a leaky bucket when only maxWaitMs is set', async () => {
const mockFn = vi.fn();
const debouncer = debounce(mockFn, { maxWaitMs: 32 });
debouncer.call();
expect(mockFn).toHaveBeenCalledTimes(0);
await sleep(16);
// Without maxWaitMs this call would cause the actual invocation to be
// postponed for a full window.
debouncer.call();
expect(mockFn).toHaveBeenCalledTimes(0);
await sleep(17);
expect(mockFn).toHaveBeenCalledTimes(1);
});
});
describe('additional functionality', () => {
it('can cancel before the timer starts', async () => {
const debouncer = debounce(identity(), { waitMs: 32 });
expect(() => {
debouncer.cancel();
}).not.toThrow();
expect(debouncer.call('hello')).toBeUndefined();
await sleep(32);
expect(debouncer.call('world')).toBe('hello');
});
it('can cancel the timer', async () => {
const data = 'Hello, World!';
const mockFn = vi.fn(() => data);
const debouncer = debounce(mockFn, { waitMs: 32 });
expect(debouncer.call()).toBeUndefined();
expect(mockFn).toHaveBeenCalledTimes(0);
await sleep(1);
expect(debouncer.call()).toBeUndefined();
expect(mockFn).toHaveBeenCalledTimes(0);
debouncer.cancel();
await sleep(32);
expect(debouncer.call()).toBeUndefined();
expect(mockFn).toHaveBeenCalledTimes(0);
await sleep(32);
expect(debouncer.call()).toStrictEqual(data);
expect(mockFn).toHaveBeenCalledTimes(1);
});
it('can cancel after the timer ends', async () => {
const debouncer = debounce(identity(), { waitMs: 32 });
expect(debouncer.call('hello')).toBeUndefined();
await sleep(32);
expect(debouncer.call('world')).toBe('hello');
expect(() => {
debouncer.cancel();
}).not.toThrow();
});
it('can cancel maxWait timer', async () => {
const debouncer = debounce(identity(), { waitMs: 16, maxWaitMs: 32 });
expect(debouncer.call('hello')).toBeUndefined();
await sleep(1);
debouncer.cancel();
await sleep(32);
expect(debouncer.call('world')).toBeUndefined();
});
it('can return a cached value', () => {
const debouncer = debounce(identity(), { timing: 'leading', waitMs: 32 });
expect(debouncer.cachedValue).toBeUndefined();
expect(debouncer.call('hello')).toBe('hello');
expect(debouncer.cachedValue).toBe('hello');
});
it('can check for inflight timers (trailing)', async () => {
const debouncer = debounce(identity(), { waitMs: 32 });
expect(debouncer.isPending).toBe(false);
expect(debouncer.call('hello')).toBeUndefined();
expect(debouncer.isPending).toBe(true);
await sleep(1);
expect(debouncer.isPending).toBe(true);
await sleep(32);
expect(debouncer.isPending).toBe(false);
});
it('can check for inflight timers (leading)', async () => {
const debouncer = debounce(identity(), { timing: 'leading', waitMs: 32 });
expect(debouncer.isPending).toBe(false);
expect(debouncer.call('hello')).toBe('hello');
expect(debouncer.isPending).toBe(true);
await sleep(1);
expect(debouncer.isPending).toBe(true);
await sleep(32);
expect(debouncer.isPending).toBe(false);
});
it('can flush before a cool-down', async () => {
const debouncer = debounce(identity(), { waitMs: 32 });
expect(debouncer.flush()).toBeUndefined();
expect(debouncer.call('hello')).toBeUndefined();
await sleep(32);
expect(debouncer.call('world')).toBe('hello');
});
it('can flush during a cool-down', async () => {
const debouncer = debounce(identity(), { waitMs: 32 });
expect(debouncer.call('hello')).toBeUndefined();
await sleep(1);
expect(debouncer.call('world')).toBeUndefined();
await sleep(1);
expect(debouncer.flush()).toBe('world');
});
it('can flush after a cool-down', async () => {
const debouncer = debounce(identity(), { waitMs: 32 });
expect(debouncer.call('hello')).toBeUndefined();
await sleep(32);
expect(debouncer.flush()).toBe('hello');
});
});
describe('errors', () => {
it('prevents maxWaitMs to be less then waitMs', () => {
expect(() => debounce(identity(), { waitMs: 32, maxWaitMs: 16 })).toThrow();
});
});
async function sleep(ms: number): Promise<void> {
await new Promise((resolve) => {
setTimeout(resolve, ms);
});
}