Skip to content

Commit

Permalink
Merge pull request #2404 from umbraco/v15.1/chore/debounce-test
Browse files Browse the repository at this point in the history
Chore: Add unit tests for the debounce function
  • Loading branch information
nielslyngsoe authored Oct 4, 2024
2 parents 574a178 + d8d1177 commit f12472e
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/packages/core/utils/debounce/debounce.function.test.ts
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);
});
});

0 comments on commit f12472e

Please sign in to comment.