Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ssi02014
Copy link
Contributor

@ssi02014 ssi02014 commented Jul 8, 2025

Improved the test code for the debounce function.

  • Add test code for the debounced.flush function.
  • Added test code for the edges option of the debounce function.

@vercel
Copy link

vercel bot commented Jul 8, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
es-toolkit Ready Ready Preview Comment Oct 24, 2025 4:23am

debouncedFunc();
debouncedFunc();

expect(func).not.toHaveBeenCalled();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code was added to more clearly test the basic debounce behavior.

// adjust the import path as necessary
import { delay } from '../promise';

const DEBOUNCE_MS = 50;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reduce code duplication, change debounceMs to a constant.

Comment on lines +65 to +73
it('should immediately invoke the delayed function when flush is called', async () => {
const func = vi.fn();
const debouncedFunc = debounce(func, DEBOUNCE_MS);

debouncedFunc();
debouncedFunc.flush();

expect(func).toHaveBeenCalledTimes(1);
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test code for the debounced.flush function.

@codecov-commenter
Copy link

codecov-commenter commented Jul 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.88%. Comparing base (750a9e3) to head (f1a6789).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1263   +/-   ##
=======================================
  Coverage   99.88%   99.88%           
=======================================
  Files         468      468           
  Lines        4453     4453           
  Branches     1311     1311           
=======================================
  Hits         4448     4448           
  Misses          5        5           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ssi02014 ssi02014 changed the title test(debounce): add test case for flush function test(debounce): improved the test code for the debounce function. Jul 8, 2025
* @param {number} debounceMs - The number of milliseconds to delay.
* @param {DebounceOptions} options - The options object
* @param {AbortSignal} options.signal - An optional AbortSignal to cancel the debounced function.
* @param {Array<'leading' | 'trailing'>} options.edges - An optional array specifying whether the function should be invoked on the leading edge, trailing edge, or both.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added missing comments.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's also an export missing for the DebounceOption type, I have a PR waiting for approval, but nobody reviewed it.

Copy link
Contributor Author

@ssi02014 ssi02014 Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type DebounceOptions = Parameters<typeof debounce<() => void>>[2];

@codecov-commenter It's a shame that the review is delayed 🥲
DebounceOptions can be approached in the same way as above

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but this is a ridiculous way to use it... hopefully we get proper responses on our PRs.

Comment on lines +106 to +149
it('should execute immediately on first call when edges is set to leading', async () => {
const func = vi.fn();
const debouncedFunc = debounce(func, DEBOUNCE_MS, { edges: ['leading'] });

debouncedFunc();

expect(func).toHaveBeenCalledTimes(1);

debouncedFunc();

await delay(DEBOUNCE_MS);

expect(func).toHaveBeenCalledTimes(1);
});

it('should execute immediately on last call when edges is set to trailing', async () => {
const func = vi.fn();
const debouncedFunc = debounce(func, DEBOUNCE_MS, { edges: ['trailing'] });

debouncedFunc();

expect(func).not.toHaveBeenCalled();

debouncedFunc();

await delay(DEBOUNCE_MS);

expect(func).toHaveBeenCalledTimes(1);
});

it('should execute immediately on both edges when edges is set to both', async () => {
const func = vi.fn();
const debouncedFunc = debounce(func, DEBOUNCE_MS, { edges: ['leading', 'trailing'] });

debouncedFunc();

expect(func).toHaveBeenCalledTimes(1);

debouncedFunc();

await delay(DEBOUNCE_MS);

expect(func).toHaveBeenCalledTimes(2);
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test code for the edges option of the debounce function.

@ssi02014 ssi02014 changed the title test(debounce): improved the test code for the debounce function. test(debounce): improved the test code for the debounce function Jul 10, 2025
@ssi02014
Copy link
Contributor Author

@dayongkr @raon0211 When will the review for this PR take place?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants