forked from vueuse/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
102 lines (77 loc) · 2.8 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { ref } from 'vue-demi'
import { createFilterWrapper, debounceFilter, increaseWithUnit, throttleFilter } from '.'
describe('utils', () => {
it('increaseWithUnit', () => {
expect(increaseWithUnit(100, 1)).toEqual(101)
expect(increaseWithUnit('1px', 1)).toEqual('2px')
expect(increaseWithUnit('-1em', 1)).toEqual('0em')
expect(increaseWithUnit('1em', -1)).toEqual('0em')
expect(increaseWithUnit('1em', -5)).toEqual('-4em')
expect(increaseWithUnit('0.5vw', 1.5)).toEqual('2vw')
expect(increaseWithUnit('100 %', 10)).toEqual('110 %')
expect(increaseWithUnit('var(--cool)', -5)).toEqual('var(--cool)')
})
})
describe('filters', () => {
beforeEach(() => {
vitest.useFakeTimers()
})
it('should debounce', () => {
const debouncedFilterSpy = vitest.fn()
const filter = createFilterWrapper(debounceFilter(1000), debouncedFilterSpy)
setTimeout(filter, 200)
vitest.runAllTimers()
setTimeout(filter, 500)
vitest.advanceTimersByTime(500)
expect(debouncedFilterSpy).toHaveBeenCalledOnce()
})
it('should debounce twice', () => {
const debouncedFilterSpy = vitest.fn()
const filter = createFilterWrapper(debounceFilter(500), debouncedFilterSpy)
setTimeout(filter, 500)
vitest.advanceTimersByTime(500)
setTimeout(filter, 1000)
vitest.advanceTimersByTime(2000)
expect(debouncedFilterSpy).toHaveBeenCalledTimes(2)
})
it('should debounce with ref', () => {
const debouncedFilterSpy = vitest.fn()
const debounceTime = ref(0)
const filter = createFilterWrapper(debounceFilter(debounceTime), debouncedFilterSpy)
filter()
debounceTime.value = 500
filter()
setTimeout(filter, 200)
vitest.runAllTimers()
expect(debouncedFilterSpy).toHaveBeenCalledTimes(2)
})
it('should throttle', () => {
const debouncedFilterSpy = vitest.fn()
const filter = createFilterWrapper(throttleFilter(1000), debouncedFilterSpy)
setTimeout(filter, 500)
setTimeout(filter, 500)
setTimeout(filter, 500)
setTimeout(filter, 500)
vitest.runAllTimers()
expect(debouncedFilterSpy).toHaveBeenCalledTimes(2)
})
it('should throttle with ref', () => {
const debouncedFilterSpy = vitest.fn()
const throttle = ref(0)
const filter = createFilterWrapper(throttleFilter(throttle), debouncedFilterSpy)
filter()
throttle.value = 1000
setTimeout(filter, 300)
setTimeout(filter, 600)
setTimeout(filter, 900)
vitest.runAllTimers()
expect(debouncedFilterSpy).toHaveBeenCalledTimes(2)
})
it('should not duplicate single event', () => {
const debouncedFilterSpy = vitest.fn()
const filter = createFilterWrapper(throttleFilter(1000), debouncedFilterSpy)
setTimeout(filter, 500)
vitest.runAllTimers()
expect(debouncedFilterSpy).toHaveBeenCalledTimes(1)
})
})