test(web): add unit tests for debounce utility - #7359
Conversation
Covers the core debounce behaviour: - function is not called before the wait period elapses - function is called exactly once after the wait period - default wait of 300 ms is applied when no wait is specified - repeated calls within the wait window reset the timer and fire only once - the most recent argument set is forwarded to the underlying function - the debounced function can be called again after a previous firing - clear() cancels a pending invocation - clear() is safe to call with no pending invocation Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Signed-off-by: Goutham Annem <gouthemannem@gmail.com>
✅ Deploy Preview for pipecd-site canceled.
|
|
👋 Hi @Goutham-Annem, welcome to PipeCD and thanks for opening your first pull request! We’re really happy to have you here Before your PR gets merged, please check a few important things below. Helpful resources
DCO Sign-offAll commits must include a In case you forget to sign-off your commit(s), follow these steps: For the last commit: git commit --amend --signoff
git push --force-with-leaseFor multiple commits: git rebase --signoff origin/master
git push --force-with-leaseRun checks locallyBefore pushing updates, please run: make checkThis runs the same checks as CI and helps catch issues early. 💬 Need help?If anything is unclear, feel free to ask in this PR or join us on the CNCF Slack in the #pipecd channel. Thanks for contributing to PipeCD! ❤️ |
There was a problem hiding this comment.
🟡 Changes recommended
The new tests include a potential flakiness issue with pending timers and one assertion is too weak to reliably detect incorrect debounce behavior.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Adds Jest unit tests to improve coverage for the debounce utility in the web frontend, validating key debounce behaviors without introducing runtime application changes.
Changes:
- Add a new
debounce.test.tssuite covering timing, default wait, repeated calls, argument forwarding, re-invocation, andclear()behavior.
File summaries
| File | Description |
|---|---|
| web/src/utils/debounce.test.ts | Adds unit tests for debounce using Jest fake timers to validate debounce semantics and clear() behavior. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| beforeEach(() => { | ||
| jest.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.useRealTimers(); | ||
| }); |
| it("passes the most recent arguments to the underlying function", () => { | ||
| const fn = jest.fn(); | ||
| const debounced = debounce(fn, 300); | ||
|
|
||
| debounced("first"); | ||
| jest.advanceTimersByTime(100); | ||
| debounced("second"); | ||
| jest.advanceTimersByTime(300); | ||
|
|
||
| expect(fn).toHaveBeenCalledWith("second"); | ||
| }); |
What?
Add
web/src/utils/debounce.test.ts— unit tests for thedebounceutility.Why?
debounce.tshad no test coverage. The tests cover all observable behaviours of the function:clear()cancels a pending invocationclear()is safe to call with no pending invocationThe tests use
jest.useFakeTimers()to controlsetTimeoutwithout real delays, matching the pattern used in other utility test files (e.g.is-stage-running.test.ts,common.test.ts).Fixes
N/A — self-identified coverage gap.
Does this change affect how the app works for end users?
No — test file only.
Does this change require an update to the documentation?
No.