deno.land / x / jotai@v1.8.4 / tests / utils / useAtomCallback.test.tsx

useAtomCallback.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { StrictMode, useCallback, useEffect, useState } from 'react'import { fireEvent, render, waitFor } from '@testing-library/react'import { atom, useAtom } from 'jotai'import { useAtomCallback } from 'jotai/utils'import { StrictModeUnlessVersionedWrite, getTestProvider } from '../testUtils'
const Provider = getTestProvider()
it('useAtomCallback with get', async () => { const countAtom = atom(0)
const Counter = () => { const [count, setCount] = useAtom(countAtom) return ( <> <div>atom count: {count}</div> <button onClick={() => setCount((c) => c + 1)}>dispatch</button> </> ) }
const Monitor = () => { const [count, setCount] = useState(0) const readCount = useAtomCallback( useCallback((get) => { const currentCount = get(countAtom) setCount(currentCount) return currentCount }, []) ) useEffect(() => { const timer = setInterval(async () => { await readCount() }, 10) return () => { clearInterval(timer) } }, [readCount]) return ( <> <div>state count: {count}</div> </> ) }
const { findByText, getByText } = render( <StrictMode> <Provider> <Counter /> <Monitor /> </Provider> </StrictMode> )
await findByText('atom count: 0') fireEvent.click(getByText('dispatch')) await waitFor(() => { getByText('atom count: 1') getByText('state count: 1') })})
it('useAtomCallback with set and update', async () => { const countAtom = atom(0) const changeableAtom = atom(0) const Counter = () => { const [count, setCount] = useAtom(countAtom) return ( <> <div>count: {count}</div> <button onClick={() => setCount((c) => c + 1)}>dispatch</button> </> ) }
const Monitor = () => { const [changeableCount] = useAtom(changeableAtom) const changeCount = useAtomCallback( useCallback((get, set) => { const currentCount = get(countAtom) set(changeableAtom, currentCount) return currentCount }, []) ) useEffect(() => { const timer = setInterval(async () => { await changeCount() }, 10) return () => { clearInterval(timer) } }, [changeCount]) return ( <> <div>changeable count: {changeableCount}</div> </> ) }
const { findByText, getByText } = render( <StrictMode> <Provider> <Counter /> <Monitor /> </Provider> </StrictMode> )
await findByText('count: 0') fireEvent.click(getByText('dispatch')) await waitFor(() => { getByText('count: 1') getByText('changeable count: 1') })})
it('useAtomCallback with set and update and arg', async () => { const countAtom = atom(0)
const App = () => { const [count] = useAtom(countAtom) const setCount = useAtomCallback( useCallback((_get, set, arg: number) => { set(countAtom, arg) return arg }, []) )
return ( <div> <p>count: {count}</p> <button onClick={() => setCount(42)}>dispatch</button> </div> ) }
const { findByText, getByText } = render( <StrictMode> <Provider> <App /> </Provider> </StrictMode> )
await findByText('count: 0') fireEvent.click(getByText('dispatch')) await waitFor(() => { getByText('count: 42') })})
it('useAtomCallback with sync atom (#1100)', async () => { const countAtom = atom(0)
const Counter = () => { const [count, setCount] = useAtom(countAtom) const readCount = useAtomCallback(useCallback((get) => get(countAtom), [])) useEffect(() => { const promiseOrValue = readCount() if (typeof promiseOrValue !== 'number') { throw new Error('should return number') } }, [readCount]) return ( <> <div>atom count: {count}</div> <button onClick={() => setCount((c) => c + 1)}>dispatch</button> </> ) }
const { findByText, getByText } = render( <StrictModeUnlessVersionedWrite> <Provider> <Counter /> </Provider> </StrictModeUnlessVersionedWrite> )
await findByText('atom count: 0')
fireEvent.click(getByText('dispatch')) await findByText('atom count: 1')})
jotai

Version Info

Tagged at
a year ago