Skip to content

Commit f12472e

Browse files
authored
Merge pull request #2404 from umbraco/v15.1/chore/debounce-test
Chore: Add unit tests for the debounce function
2 parents 574a178 + d8d1177 commit f12472e

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { expect } from '@open-wc/testing';
2+
import { debounce } from './debounce.function.js';
3+
4+
describe('debounce', () => {
5+
it('should call the function only once after the timeout', async () => {
6+
let count = 0;
7+
const debounced = debounce(() => count++, 100);
8+
9+
debounced();
10+
debounced();
11+
debounced();
12+
debounced();
13+
debounced();
14+
15+
expect(count).to.equal(0);
16+
17+
await new Promise((resolve) => setTimeout(resolve, 200));
18+
19+
expect(count).to.equal(1);
20+
});
21+
22+
it('should call the function with the latest arguments', async () => {
23+
let count = 0;
24+
const debounced = debounce((value: number) => (count = value), 100);
25+
26+
debounced(1);
27+
debounced(2);
28+
debounced(3);
29+
debounced(4);
30+
debounced(5);
31+
32+
expect(count).to.equal(0);
33+
34+
await new Promise((resolve) => setTimeout(resolve, 200));
35+
36+
expect(count).to.equal(5);
37+
});
38+
});

0 commit comments

Comments
 (0)