/
optimization.test.tsx
171 lines (148 loc) 路 3.91 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { useRef } from 'react'
import { fireEvent, render, waitFor } from '@testing-library/react'
import ReactDOM from 'react-dom'
import { atom, useAtom } from 'jotai'
import { getTestProvider } from './testUtils'
const Provider = getTestProvider()
// FIXME this is a hacky workaround temporarily
const IS_REACT18 = !!(ReactDOM as any).createRoot
it('only relevant render function called (#156)', async () => {
const count1Atom = atom(0)
const count2Atom = atom(0)
const Counter1 = () => {
const [count, setCount] = useAtom(count1Atom)
const renderCount = useRef(0)
++renderCount.current
return (
<>
<div>
count1: {count} ({renderCount.current})
</div>
<button onClick={() => setCount((c) => c + 1)}>button1</button>
</>
)
}
const Counter2 = () => {
const [count, setCount] = useAtom(count2Atom)
const renderCount = useRef(0)
++renderCount.current
return (
<>
<div>
count2: {count} ({renderCount.current})
</div>
<button onClick={() => setCount((c) => c + 1)}>button2</button>
</>
)
}
const { getByText } = render(
<Provider>
<Counter1 />
<Counter2 />
</Provider>
)
await waitFor(() => {
getByText('count1: 0 (1)')
getByText('count2: 0 (1)')
})
fireEvent.click(getByText('button1'))
await waitFor(() => {
if (IS_REACT18) {
getByText('count1: 1 (3)')
} else {
getByText('count1: 1 (2)')
}
getByText('count2: 0 (1)')
})
fireEvent.click(getByText('button2'))
await waitFor(() => {
if (IS_REACT18) {
getByText('count1: 1 (3)')
getByText('count2: 1 (3)')
} else {
getByText('count1: 1 (2)')
getByText('count2: 1 (2)')
}
})
})
it('only render once using atoms with write-only atom', async () => {
const count1Atom = atom(0)
const count2Atom = atom(0)
const incrementAtom = atom(null, (_get, set, _arg) => {
set(count1Atom, (c) => c + 1)
set(count2Atom, (c) => c + 1)
})
const Counter = () => {
const [count1] = useAtom(count1Atom)
const [count2] = useAtom(count2Atom)
const renderCount = useRef(0)
++renderCount.current
return (
<div>
count1: {count1}, count2: {count2} ({renderCount.current})
</div>
)
}
const Control = () => {
const [, increment] = useAtom(incrementAtom)
return <button onClick={increment}>button</button>
}
const { getByText, findByText } = render(
<Provider>
<Counter />
<Control />
</Provider>
)
await findByText('count1: 0, count2: 0 (1)')
fireEvent.click(getByText('button'))
if (IS_REACT18) {
await findByText('count1: 1, count2: 1 (3)')
} else {
await findByText('count1: 1, count2: 1 (2)')
}
fireEvent.click(getByText('button'))
if (IS_REACT18) {
await findByText('count1: 2, count2: 2 (4)')
} else {
await findByText('count1: 2, count2: 2 (3)')
}
})
it('useless re-renders with static atoms (#355)', async () => {
// check out https://codesandbox.io/s/m82r5 to see the expected re-renders
const countAtom = atom(0)
const unrelatedAtom = atom(0)
const Counter = () => {
const [count, setCount] = useAtom(countAtom)
useAtom(unrelatedAtom)
const renderCount = useRef(0)
++renderCount.current
return (
<>
<div>
<p>
count: {count} ({renderCount.current})
</p>
</div>
<button onClick={() => setCount((c) => c + 1)}>button</button>
</>
)
}
const { getByText, findByText } = render(
<Provider>
<Counter />
</Provider>
)
await findByText('count: 0 (1)')
fireEvent.click(getByText('button'))
if (IS_REACT18) {
await findByText('count: 1 (3)')
} else {
await findByText('count: 1 (2)')
}
fireEvent.click(getByText('button'))
if (IS_REACT18) {
await findByText('count: 2 (4)')
} else {
await findByText('count: 2 (3)')
}
})