forked from vueuse/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
94 lines (82 loc) · 2.67 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
import { nextTick, reactive, ref } from 'vue-demi'
import { watchArray } from '.'
describe('watchArray', () => {
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])
watchArray(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])
watchArray(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])
watchArray(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])
watchArray(num, spy, { deep: true })
num.value.splice(1, 1, 5, 6, 7)
await nextTick()
expect(spy).toBeCalledTimes(1)
})
it('should work with reactive source', 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 = reactive([1, 2, 3])
watchArray(num, spy)
num.push(4)
await nextTick()
expect(spy).toBeCalledTimes(1)
})
it('should work with functional source', 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])
watchArray(() => num.value, spy)
num.value = [...num.value, 4]
await nextTick()
expect(spy).toBeCalledTimes(1)
})
})