Skip to content
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

feat(useThrottleFn): add test cases #2649

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions packages/shared/useThrottleFn/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { promiseTimeout } from '@vueuse/shared'
import { useThrottleFn } from '../useThrottleFn'

describe('useThrottleFn', () => {
it('should be defined', () => {
expect(useThrottleFn).toBeDefined()
})

it('should work', async () => {
const callback = vi.fn()
const ms = 20
const run = useThrottleFn(callback, ms)
run()
run()
expect(callback).toHaveBeenCalledTimes(1)
await promiseTimeout(ms + 10)
run()
expect(callback).toHaveBeenCalledTimes(2)
})

it('should work with trailing', async () => {
const callback = vi.fn()
const ms = 20
const run = useThrottleFn(callback, ms, true)
run()
run()
expect(callback).toHaveBeenCalledTimes(1)
await promiseTimeout(ms + 10)
expect(callback).toHaveBeenCalledTimes(2)
})

it('should work with leading', async () => {
const callback = vi.fn()
const ms = 20
const run = useThrottleFn(callback, ms, false, false)
run()
run()
expect(callback).toHaveBeenCalledTimes(1)
await promiseTimeout(ms + 10)
run()
run()
run()
expect(callback).toHaveBeenCalledTimes(2)
await promiseTimeout(ms + 20)
run()
expect(callback).toHaveBeenCalledTimes(2)
})
})