-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2404 from umbraco/v15.1/chore/debounce-test
Chore: Add unit tests for the debounce function
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/packages/core/utils/debounce/debounce.function.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { expect } from '@open-wc/testing'; | ||
import { debounce } from './debounce.function.js'; | ||
|
||
describe('debounce', () => { | ||
it('should call the function only once after the timeout', async () => { | ||
let count = 0; | ||
const debounced = debounce(() => count++, 100); | ||
|
||
debounced(); | ||
debounced(); | ||
debounced(); | ||
debounced(); | ||
debounced(); | ||
|
||
expect(count).to.equal(0); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 200)); | ||
|
||
expect(count).to.equal(1); | ||
}); | ||
|
||
it('should call the function with the latest arguments', async () => { | ||
let count = 0; | ||
const debounced = debounce((value: number) => (count = value), 100); | ||
|
||
debounced(1); | ||
debounced(2); | ||
debounced(3); | ||
debounced(4); | ||
debounced(5); | ||
|
||
expect(count).to.equal(0); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 200)); | ||
|
||
expect(count).to.equal(5); | ||
}); | ||
}); |