forked from vueuse/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
64 lines (56 loc) · 1.85 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
import { nextTick, ref } from 'vue-demi'
import { watchListChanges } from '.'
describe('watchListChanges', () => {
it('should work when two lists are different', async () => {
const spy = vitest.fn((newList, oldList, added, removed) => {
expect(newList).toEqual([1, 1, 4])
expect(oldList).toEqual([1, 2, 3])
expect(added).toEqual([1, 4])
expect(removed).toEqual([2, 3])
})
const num = ref([1, 2, 3])
watchListChanges(num, spy)
num.value = [1, 1, 4]
await nextTick()
expect(spy).toBeCalledTimes(1)
})
it('should work when two lists are identical', async () => {
const spy = vitest.fn((newList, oldList, added, removed) => {
expect(newList).toEqual([1, 2, 3])
expect(oldList).toEqual([1, 2, 3])
expect(added).toEqual([])
expect(removed).toEqual([])
})
const num = ref([1, 2, 3])
watchListChanges(num, spy)
num.value = [1, 2, 3]
await nextTick()
expect(spy).toBeCalledTimes(1)
})
it('should work with list push', async () => {
const spy = vitest.fn((newList, oldList, added, removed) => {
expect(newList).toEqual([1, 2, 3, 4])
expect(oldList).toEqual([1, 2, 3])
expect(added).toEqual([4])
expect(removed).toEqual([])
})
const num = ref([1, 2, 3])
watchListChanges(num, spy, { deep: true })
num.value.push(4)
await nextTick()
expect(spy).toBeCalledTimes(1)
})
it('should work with list splice', async () => {
const spy = vitest.fn((newList, oldList, added, removed) => {
expect(newList).toEqual([1, 5, 6, 7, 3])
expect(oldList).toEqual([1, 2, 3])
expect(added).toEqual([5, 6, 7])
expect(removed).toEqual([2])
})
const num = ref([1, 2, 3])
watchListChanges(num, spy, { deep: true })
num.value.splice(1, 1, 5, 6, 7)
await nextTick()
expect(spy).toBeCalledTimes(1)
})
})