deno.land / x / jotai@v1.8.4 / tests / valtio / atomWithProxy.test.tsx

atomWithProxy.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { StrictMode, Suspense } from 'react'import { fireEvent, render } from '@testing-library/react'import { proxy, snapshot } from 'valtio/vanilla'import { atom, useAtom } from 'jotai'import { atomWithProxy } from 'jotai/valtio'import { getTestProvider } from '../testUtils'
const Provider = getTestProvider()
it('count state', async () => { const proxyState = proxy({ count: 0 }) const stateAtom = atomWithProxy(proxyState) ++proxyState.count
const Counter = () => { const [state, setState] = useAtom(stateAtom)
return ( <> count: {state.count} <button onClick={() => setState((prev) => ({ ...prev, count: prev.count + 1 })) }> button </button> </> ) }
const { findByText, getByText } = render( <StrictMode> <Provider> <Counter /> </Provider> </StrictMode> )
await findByText('count: 1')
fireEvent.click(getByText('button')) await findByText('count: 2') expect(proxyState.count).toBe(2)
++proxyState.count await findByText('count: 3') expect(proxyState.count).toBe(3)})
it('nested count state', async () => { const proxyState = proxy({ nested: { count: 0 }, other: {} }) const otherSnap = snapshot(proxyState.other) const stateAtom = atomWithProxy(proxyState) const Counter = () => { const [state, setState] = useAtom(stateAtom)
return ( <> count: {state.nested.count} <button onClick={() => setState((prev) => ({ ...prev, nested: { ...prev.nested, count: prev.nested.count + 1 }, })) }> button </button> </> ) }
const { findByText, getByText } = render( <StrictMode> <Provider> <Counter /> </Provider> </StrictMode> )
await findByText('count: 0')
fireEvent.click(getByText('button')) await findByText('count: 1') expect(proxyState.nested.count).toBe(1) expect(otherSnap === snapshot(proxyState.other)).toBe(true)
++proxyState.nested.count await findByText('count: 2') expect(proxyState.nested.count).toBe(2) expect(otherSnap === snapshot(proxyState.other)).toBe(true)})
it('state with a promise', async () => { const getAsyncStatus = (status: string) => new Promise<string>((resolve) => { setTimeout(() => { resolve(status) }, 500) })
const proxyState = proxy({ status: getAsyncStatus('done'), }) const stateAtom = atomWithProxy(proxyState)
const Status = () => { const [state, setState] = useAtom(stateAtom) return ( <> <span> status:{' '} {state.status as any /* LIMITATION: we don't yet use resolved type */} </span> <button onClick={() => setState((prev) => ({ ...prev, status: getAsyncStatus('modified'), })) }> button </button> </> ) }
const { findByText, getByText } = render( <StrictMode> <Provider> <Suspense fallback="loading"> <Status /> </Suspense> </Provider> </StrictMode> )
await findByText('loading') await findByText('status: done') fireEvent.click(getByText('button')) await findByText('loading') await findByText('status: modified')})
it('synchronous atomWithProxy and regular atom ', async () => { const proxyState: { elements: Record<string, string> } = proxy({ elements: {}, })
const stateAtom = atomWithProxy(proxyState, { sync: true }) const selectedElementIdAtom = atom('')
const createElementAtom = atom(null, (_, set) => { const id = '123' set(selectedElementIdAtom, id) proxyState.elements[id] = `element` })
const Elements = () => { const [state] = useAtom(stateAtom) const [selected] = useAtom(selectedElementIdAtom) const [, create] = useAtom(createElementAtom)
return ( <> <span> selected element:{' '} {selected === '' ? 'none' : state.elements[selected]} </span> <button onClick={() => { create() }}> create and select element </button> </> ) }
const { findByText, getByText } = render( <StrictMode> <Provider> <Elements /> </Provider> </StrictMode> )
await findByText('selected element: none') fireEvent.click(getByText('create and select element')) getByText('selected element: element') // synchronous})
it('array.length state', async () => { const proxyState = proxy({ array: [0, 0] }) const stateAtom = atomWithProxy(proxyState)
const Counter = () => { const [state, setState] = useAtom(stateAtom)
return ( <> array0: {state.array[0]} <button onClick={() => setState({ array: [1] })}>button</button> </> ) }
const { findByText, getByText } = render( <StrictMode> <Provider> <Counter /> </Provider> </StrictMode> )
await findByText('array0: 0') fireEvent.click(getByText('button'))
await findByText('array0: 1') expect(proxyState.array.length).toBe(1)})
jotai

Version Info

Tagged at
a year ago