Skip to content

Commit

Permalink
test(watchDebounced):watchDebounced ut statements 100% branches 100% …
Browse files Browse the repository at this point in the history
…functions 100% lines 100%
  • Loading branch information
sun0day committed Oct 20, 2022
1 parent caaa56a commit c41def6
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions packages/shared/watchDebounced/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { nextTick, ref } from 'vue-demi'
import { promiseTimeout } from '../utils'
import { debouncedWatch, watchDebounced } from '.'

describe('watchDebounced', () => {
it('should export module', () => {
expect(watchDebounced).toBeDefined()
expect(debouncedWatch).toBeDefined()
})

it('should work', async () => {
const num = ref(0)
const cb = vi.fn()
watchDebounced(num, cb, { debounce: 100, maxWait: 150 })

num.value = 1
await nextTick()
expect(cb).toHaveBeenCalledTimes(0)

num.value = 2
await promiseTimeout(50)
expect(cb).toHaveBeenCalledTimes(0)

await promiseTimeout(50)
expect(cb).toHaveBeenCalledWith(2, 1, expect.anything())

num.value = 4
await promiseTimeout(80)
expect(cb).toHaveBeenCalledTimes(1)

num.value = 5
await promiseTimeout(70)
expect(cb).toHaveBeenCalledTimes(2)
expect(cb).toHaveBeenCalledWith(4, 2, expect.anything())
})
})

0 comments on commit c41def6

Please sign in to comment.