-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
optimization.test.tsx
116 lines (95 loc) 路 2.95 KB
/
optimization.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { useState } from 'react'
import { fireEvent, render, waitFor } from '@testing-library/react'
import { expect, it, vi } from 'vitest'
import { proxy, useSnapshot } from 'valtio'
it('regression: useSnapshot renders should not fail consistency check with extra render (nested useSnapshot)', async () => {
const obj = proxy({ childCount: 0, parentCount: 0 })
const childRenderFn = vi.fn()
const Child = () => {
const snap = useSnapshot(obj)
childRenderFn(snap.childCount)
return (
<>
<div>childCount: {snap.childCount}</div>
<button onClick={() => ++obj.childCount}>childButton</button>
</>
)
}
const parentRenderFn = vi.fn()
const Parent = () => {
const snap = useSnapshot(obj)
parentRenderFn(snap.parentCount)
return (
<>
<div>parentCount: {snap.parentCount}</div>
<button onClick={() => ++obj.parentCount}>parentButton</button>
<Child />
</>
)
}
const { getByText } = render(<Parent />)
await waitFor(() => {
getByText('childCount: 0')
getByText('parentCount: 0')
})
expect(childRenderFn).toBeCalledTimes(1)
expect(childRenderFn).lastCalledWith(0)
expect(parentRenderFn).toBeCalledTimes(1)
expect(parentRenderFn).lastCalledWith(0)
obj.parentCount += 1
await waitFor(() => {
getByText('childCount: 0')
getByText('parentCount: 1')
})
expect(childRenderFn).toBeCalledTimes(2)
expect(childRenderFn).lastCalledWith(0)
expect(parentRenderFn).toBeCalledTimes(2)
expect(parentRenderFn).lastCalledWith(1)
})
it('regression: useSnapshot renders should not fail consistency check with extra render', async () => {
const obj = proxy({ childCount: 0, anotherValue: 0 })
const childRenderFn = vi.fn()
const Child = () => {
const snap = useSnapshot(obj)
childRenderFn(snap.childCount)
return (
<>
<div>childCount: {snap.childCount}</div>
<button onClick={() => ++obj.childCount}>childButton</button>
</>
)
}
const parentRenderFn = vi.fn()
const Parent = () => {
const [parentCount, setParentCount] = useState(0)
parentRenderFn(parentCount)
return (
<>
<div>parentCount: {parentCount}</div>
<button onClick={() => setParentCount((v) => v + 1)}>
parentButton
</button>
<Child />
</>
)
}
const { getByText } = render(<Parent />)
await waitFor(() => {
getByText('childCount: 0')
getByText('parentCount: 0')
})
expect(childRenderFn).toBeCalledTimes(1)
expect(childRenderFn).lastCalledWith(0)
expect(parentRenderFn).toBeCalledTimes(1)
expect(parentRenderFn).lastCalledWith(0)
obj.anotherValue += 1
fireEvent.click(getByText('parentButton'))
await waitFor(() => {
getByText('childCount: 0')
getByText('parentCount: 1')
})
expect(childRenderFn).toBeCalledTimes(2)
expect(childRenderFn).lastCalledWith(0)
expect(parentRenderFn).toBeCalledTimes(2)
expect(parentRenderFn).lastCalledWith(1)
})