Skip to content

Commit

Permalink
feat(useThrottleFn): add test cases (#2649)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfred-Skyblue committed Jan 12, 2023
1 parent 6cd5f44 commit 85ab146
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions packages/shared/useThrottleFn/index.test.ts
@@ -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)
})
})

0 comments on commit 85ab146

Please sign in to comment.