-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
watch.test.tsx
86 lines (71 loc) 路 1.87 KB
/
watch.test.tsx
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
import { watch } from 'valtio/utils'
import { proxy } from 'valtio/vanilla'
describe('watch', () => {
it('should re-run for individual proxy updates', async () => {
const reference = proxy({ value: 'Example' })
const callback = jest.fn()
watch((get) => {
get(reference)
callback()
})
expect(callback).toBeCalledTimes(1)
reference.value = 'Update'
await Promise.resolve()
expect(callback).toBeCalledTimes(2)
})
it('should re-run for multiple proxy updates', async () => {
const A = proxy({ value: 'A' })
const B = proxy({ value: 'B' })
const callback = jest.fn()
watch((get) => {
get(A)
get(B)
callback()
})
expect(callback).toBeCalledTimes(1)
A.value = 'B'
await Promise.resolve()
expect(callback).toBeCalledTimes(2)
B.value = 'C'
await Promise.resolve()
expect(callback).toBeCalledTimes(3)
})
it('should cleanup when state updates', async () => {
const reference = proxy({ value: 'Example' })
const callback = jest.fn()
watch((get) => {
get(reference)
return () => {
callback()
}
})
expect(callback).toBeCalledTimes(0)
reference.value = 'Update'
await Promise.resolve()
expect(callback).toBeCalledTimes(1)
})
it('should cleanup when stopped', () => {
const callback = jest.fn()
const stop = watch(() => callback)
expect(callback).toBeCalledTimes(0)
stop()
expect(callback).toBeCalledTimes(1)
})
it('should cleanup internal effects when stopped', () => {
const callback = jest.fn()
const stop = watch(() => {
watch(() => {
watch(() => {
watch(() => {
watch(() => () => {
callback()
})
})
})
})
})
expect(callback).toBeCalledTimes(0)
stop()
expect(callback).toBeCalledTimes(1)
})
})