Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

test(watchDebounced): add tests to solve debounce max wait issue #4473

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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions 27 packages/shared/watchDebounced/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,31 @@ describe('watchDebounced', () => {
expect(cb).toHaveBeenCalledTimes(2)
expect(cb).toHaveBeenCalledWith(4, 2, expect.anything())
})

it.fails('should work with constant changes over multiple maxWaits', async () => {
vi.useFakeTimers()
const num = ref(0)
const cb = vi.fn()

const constantUpdateOverTime = async (ms: number) => {
for (let i = 0; i < ms; i++) {
num.value += 1
await vi.advanceTimersByTimeAsync(1)
}
}
watchDebounced(num, cb, { debounce: 10, maxWait: 50 })
expect(cb).toHaveBeenCalledTimes(0)
await constantUpdateOverTime(49)
expect(cb).toHaveBeenCalledTimes(0)
await constantUpdateOverTime(1)
expect(cb).toHaveBeenCalledTimes(1)
await constantUpdateOverTime(50)
expect(cb).toHaveBeenCalledTimes(2)
await constantUpdateOverTime(50)

expect(cb).toHaveBeenCalledTimes(3)
expect(cb.mock.calls[0][0]).toBe(50) // or 51
expect(cb.mock.calls[1][0]).toBe(100) // or 101
expect(cb.mock.calls[2][0]).toBe(150) // or 151
})
})
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.