-
Notifications
You must be signed in to change notification settings - Fork 502
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
Open
ssi02014
wants to merge
12
commits into
toss:main
Choose a base branch
from
ssi02014:test/debounce
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+83
−33
Open
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bd8c78f
test(debounce): add test case for flush function
ssi02014 6f4ead9
test(debounce): change to debouncedMs constant
ssi02014 043f891
test(debounce): add test cases for edge options
ssi02014 b101677
Merge branch 'main' into test/debounce
ssi02014 2c2561e
Merge branch 'main' into test/debounce
ssi02014 79e6765
Merge branch 'main' into test/debounce
ssi02014 b394472
Merge branch 'main' into test/debounce
ssi02014 01ffc92
Merge branch 'main' into test/debounce
ssi02014 6ead746
Merge branch 'main' into test/debounce
ssi02014 b77a16e
Merge branch 'main' into test/debounce
ssi02014 4bd3e87
Merge branch 'main' into test/debounce
manudeli f1a6789
Merge branch 'main' into test/debounce
ssi02014 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -3,110 +3,116 @@ import { debounce } from './debounce'; | |
| // adjust the import path as necessary | ||
| import { delay } from '../promise'; | ||
|
|
||
| const DEBOUNCE_MS = 50; | ||
|
|
||
| describe('debounce', () => { | ||
| it('should debounce function calls', async () => { | ||
| const func = vi.fn(); | ||
| const debounceMs = 50; | ||
| const debouncedFunc = debounce(func, debounceMs); | ||
| const debouncedFunc = debounce(func, DEBOUNCE_MS); | ||
|
|
||
| debouncedFunc(); | ||
| debouncedFunc(); | ||
| debouncedFunc(); | ||
|
|
||
| await delay(debounceMs * 2); | ||
| expect(func).not.toHaveBeenCalled(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code was added to more clearly test the basic debounce behavior. |
||
|
|
||
| await delay(DEBOUNCE_MS * 2); | ||
|
|
||
| expect(func).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should delay the function call by the specified wait time', async () => { | ||
| const func = vi.fn(); | ||
| const debounceMs = 50; | ||
| const debouncedFunc = debounce(func, debounceMs); | ||
| const debouncedFunc = debounce(func, DEBOUNCE_MS); | ||
|
|
||
| debouncedFunc(); | ||
| await delay(debounceMs / 2); | ||
| await delay(DEBOUNCE_MS / 2); | ||
| expect(func).not.toHaveBeenCalled(); | ||
|
|
||
| await delay(debounceMs / 2 + 1); | ||
| await delay(DEBOUNCE_MS / 2 + 1); | ||
| expect(func).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should reset the wait time if called again before wait time ends', async () => { | ||
| const func = vi.fn(); | ||
| const debounceMs = 50; | ||
| const debouncedFunc = debounce(func, debounceMs); | ||
| const debouncedFunc = debounce(func, DEBOUNCE_MS); | ||
|
|
||
| debouncedFunc(); | ||
| await delay(debounceMs / 2); | ||
| await delay(DEBOUNCE_MS / 2); | ||
| debouncedFunc(); | ||
| await delay(debounceMs / 2); | ||
| await delay(DEBOUNCE_MS / 2); | ||
| debouncedFunc(); | ||
| await delay(debounceMs / 2); | ||
| await delay(DEBOUNCE_MS / 2); | ||
| debouncedFunc(); | ||
|
|
||
| expect(func).not.toHaveBeenCalled(); | ||
|
|
||
| await delay(debounceMs + 1); | ||
| await delay(DEBOUNCE_MS + 1); | ||
| expect(func).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should cancel the debounced function call', async () => { | ||
| const func = vi.fn(); | ||
| const debounceMs = 50; | ||
| const debouncedFunc = debounce(func, debounceMs); | ||
| const debouncedFunc = debounce(func, DEBOUNCE_MS); | ||
|
|
||
| debouncedFunc(); | ||
| debouncedFunc.cancel(); | ||
| await delay(debounceMs); | ||
| await delay(DEBOUNCE_MS); | ||
|
|
||
| expect(func).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| 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); | ||
| }); | ||
|
Comment on lines
+65
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add test code for the |
||
|
|
||
| it('should work correctly if the debounced function is called after the wait time', async () => { | ||
| const func = vi.fn(); | ||
| const debounceMs = 50; | ||
| const debouncedFunc = debounce(func, debounceMs); | ||
| const debouncedFunc = debounce(func, DEBOUNCE_MS); | ||
|
|
||
| debouncedFunc(); | ||
| await delay(debounceMs + 1); | ||
| await delay(DEBOUNCE_MS + 1); | ||
| debouncedFunc(); | ||
| await delay(debounceMs + 1); | ||
| await delay(DEBOUNCE_MS + 1); | ||
|
|
||
| expect(func).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('should have no effect if we call cancel when the function is not executed', () => { | ||
| const func = vi.fn(); | ||
| const debounceMs = 50; | ||
| const debouncedFunc = debounce(func, debounceMs); | ||
| const debouncedFunc = debounce(func, DEBOUNCE_MS); | ||
|
|
||
| expect(() => debouncedFunc.cancel()).not.toThrow(); | ||
| }); | ||
|
|
||
| it('should call the function with correct arguments', async () => { | ||
| const func = vi.fn(); | ||
| const debounceMs = 50; | ||
| const debouncedFunc = debounce(func, debounceMs); | ||
| const debouncedFunc = debounce(func, DEBOUNCE_MS); | ||
|
|
||
| debouncedFunc('test', 123); | ||
|
|
||
| await delay(debounceMs * 2); | ||
| await delay(DEBOUNCE_MS * 2); | ||
|
|
||
| expect(func).toHaveBeenCalledTimes(1); | ||
| expect(func).toHaveBeenCalledWith('test', 123); | ||
| }); | ||
|
|
||
| it('should cancel the debounced function call if aborted via AbortSignal', async () => { | ||
| const func = vi.fn(); | ||
| const debounceMs = 50; | ||
| const controller = new AbortController(); | ||
| const signal = controller.signal; | ||
| const debouncedFunc = debounce(func, debounceMs, { signal }); | ||
| const debouncedFunc = debounce(func, DEBOUNCE_MS, { signal }); | ||
|
|
||
| debouncedFunc(); | ||
| controller.abort(); | ||
|
|
||
| await delay(debounceMs); | ||
| await delay(DEBOUNCE_MS); | ||
|
|
||
| expect(func).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
@@ -119,24 +125,22 @@ describe('debounce', () => { | |
|
|
||
| const func = vi.fn(); | ||
|
|
||
| const debounceMs = 50; | ||
| const debouncedFunc = debounce(func, debounceMs, { signal }); | ||
| const debouncedFunc = debounce(func, DEBOUNCE_MS, { signal }); | ||
|
|
||
| debouncedFunc(); | ||
|
|
||
| await delay(debounceMs); | ||
| await delay(DEBOUNCE_MS); | ||
|
|
||
| expect(func).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should not add multiple abort event listeners', async () => { | ||
| const func = vi.fn(); | ||
| const debounceMs = 100; | ||
| const controller = new AbortController(); | ||
| const signal = controller.signal; | ||
| const addEventListenerSpy = vi.spyOn(signal, 'addEventListener'); | ||
|
|
||
| const debouncedFunc = debounce(func, debounceMs, { signal }); | ||
| const debouncedFunc = debounce(func, DEBOUNCE_MS, { signal }); | ||
|
|
||
| debouncedFunc(); | ||
| debouncedFunc(); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.