forked from vueuse/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
34 lines (28 loc) · 918 Bytes
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { nextTick, ref } from 'vue-demi'
import { promiseTimeout } from '../utils'
import { throttledWatch, watchThrottled } from '.'
describe('watchThrottled', () => {
it('should export module', () => {
expect(watchThrottled).toBeDefined()
expect(throttledWatch).toBeDefined()
})
it('should work', async () => {
const num = ref(0)
const cb = vi.fn()
watchThrottled(num, cb, { throttle: 100 })
num.value = 1
await nextTick()
expect(cb).toHaveBeenCalledWith(1, 0, expect.anything())
num.value = 2
await promiseTimeout(50)
expect(cb).toHaveBeenCalledTimes(1)
num.value = 3
await promiseTimeout(50)
expect(cb).toHaveBeenCalledTimes(2)
expect(cb).toHaveBeenCalledWith(3, 2, expect.anything())
num.value = 4
await promiseTimeout(100)
expect(cb).toHaveBeenCalledTimes(3)
expect(cb).toHaveBeenCalledWith(4, 3, expect.anything())
})
})