Skip to content

Commit

Permalink
Create debounce.function.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
madsrasmussen committed Oct 2, 2024
1 parent a07f6d9 commit d8d1177
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 d8d1177

Please sign in to comment.