-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlodash.debounce-test.js
More file actions
54 lines (44 loc) · 1.75 KB
/
lodash.debounce-test.js
File metadata and controls
54 lines (44 loc) · 1.75 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
'use strict';
const assert = require('node:assert/strict');
(async () => {
const debounce = require('lodash.debounce');
assert.equal(typeof debounce, 'function');
// Test that debounce delays execution
let callCount = 0;
const debounced = debounce(() => { callCount++; }, 50);
debounced();
debounced();
debounced();
// Should not have been called yet since we're within the wait period
assert.equal(callCount, 0, 'should not call immediately');
// Wait for the debounce timer to expire
await new Promise((resolve) => setTimeout(resolve, 100));
assert.equal(callCount, 1, 'should have been called exactly once after delay');
// Test cancel
let cancelCount = 0;
const cancelable = debounce(() => { cancelCount++; }, 50);
cancelable();
assert.equal(typeof cancelable.cancel, 'function');
cancelable.cancel();
await new Promise((resolve) => setTimeout(resolve, 100));
assert.equal(cancelCount, 0, 'cancelled debounce should not fire');
// Test flush
let flushCount = 0;
const flushable = debounce(() => { flushCount++; }, 200);
flushable();
assert.equal(typeof flushable.flush, 'function');
flushable.flush();
assert.equal(flushCount, 1, 'flush should invoke the debounced function immediately');
// Test with leading option
let leadingCount = 0;
const leading = debounce(() => { leadingCount++; }, 50, { leading: true, trailing: false });
leading();
assert.equal(leadingCount, 1, 'leading debounce should fire on first call');
leading();
assert.equal(leadingCount, 1, 'second call within wait should not fire again');
console.log('lodash.debounce-test:ok');
})().catch((err) => {
console.error('lodash.debounce-test:fail');
console.error(err && err.stack ? err.stack : String(err));
process.exit(1);
});