-
Couldn't load subscription status.
- Fork 500
test(debounce): improved the test code for the debounce function #1263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| debouncedFunc(); | ||
| debouncedFunc(); | ||
|
|
||
| expect(func).not.toHaveBeenCalled(); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
| 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); | ||
| }); |
There was a problem hiding this comment.
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 Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ 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:
|
| * @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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added missing comments.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
| 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); | ||
| }); |
There was a problem hiding this comment.
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.
Improved the test code for the debounce function.
debounced.flushfunction.edges option of the debouncefunction.