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

abortableAtom.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
import { StrictMode, Suspense, version as reactVersion, useState } from 'react'import { fireEvent, render, waitFor } from '@testing-library/react'import { atom, useAtomValue, useSetAtom } from 'jotai'import { abortableAtom } from 'jotai/utils'import { getTestProvider, itSkipIfVersionedWrite } from '../testUtils'
const Provider = getTestProvider()
// Doesn't work with 16.8.6 maybe because of some React bugs?// Curious, though. Feel free to dig into it if someone gets interested.const describeExceptFor1686 = reactVersion !== '16.8.6' ? describe : describe.skip
describeExceptFor1686('abortable atom test', () => { itSkipIfVersionedWrite('can abort with signal.aborted', async () => { const countAtom = atom(0) let abortedCount = 0 const derivedAtom = abortableAtom(async (get, { signal }) => { const count = get(countAtom) await new Promise((r) => setTimeout(r, 100)) if (signal.aborted) { ++abortedCount } return count })
const Component = () => { const count = useAtomValue(derivedAtom) return <div>count: {count}</div> }
const Controls = () => { const setCount = useSetAtom(countAtom) return ( <> <button onClick={() => setCount((c) => c + 1)}>button</button> </> ) }
const { findByText, getByText } = render( <StrictMode> <Provider> <Suspense fallback="loading"> <Component /> <Controls /> </Suspense> </Provider> </StrictMode> )
await findByText('loading') await findByText('count: 0') expect(abortedCount).toBe(0)
fireEvent.click(getByText('button')) fireEvent.click(getByText('button')) await findByText('count: 2') expect(abortedCount).toBe(1)
fireEvent.click(getByText('button')) await findByText('count: 3') expect(abortedCount).toBe(1) })
itSkipIfVersionedWrite('can abort with event listener', async () => { const countAtom = atom(0) let abortedCount = 0 const derivedAtom = abortableAtom(async (get, { signal }) => { const count = get(countAtom) const callback = () => { ++abortedCount } signal.addEventListener('abort', callback) await new Promise((r) => setTimeout(r, 100)) signal.removeEventListener('abort', callback) return count })
const Component = () => { const count = useAtomValue(derivedAtom) return <div>count: {count}</div> }
const Controls = () => { const setCount = useSetAtom(countAtom) return ( <> <button onClick={() => setCount((c) => c + 1)}>button</button> </> ) }
const { findByText, getByText } = render( <StrictMode> <Provider> <Suspense fallback="loading"> <Component /> <Controls /> </Suspense> </Provider> </StrictMode> )
await findByText('loading') await findByText('count: 0') expect(abortedCount).toBe(0)
fireEvent.click(getByText('button')) fireEvent.click(getByText('button')) await findByText('count: 2') expect(abortedCount).toBe(1)
fireEvent.click(getByText('button')) await findByText('count: 3') expect(abortedCount).toBe(1) })
itSkipIfVersionedWrite('can abort on unmount', async () => { const countAtom = atom(0) let abortedCount = 0 const derivedAtom = abortableAtom(async (get, { signal }) => { const count = get(countAtom) await new Promise((r) => setTimeout(r, 100)) if (signal.aborted) { ++abortedCount } return count })
const Component = () => { const count = useAtomValue(derivedAtom) return <div>count: {count}</div> }
const Parent = () => { const setCount = useSetAtom(countAtom) const [show, setShow] = useState(true) return ( <> {show ? <Component /> : 'hidden'} <button onClick={() => setCount((c) => c + 1)}>button</button> <button onClick={() => setShow((x) => !x)}>toggle</button> </> ) }
const { findByText, getByText } = render( <StrictMode> <Provider> <Suspense fallback="loading"> <Parent /> </Suspense> </Provider> </StrictMode> )
await findByText('loading') await findByText('count: 0') expect(abortedCount).toBe(0)
fireEvent.click(getByText('button')) fireEvent.click(getByText('toggle')) await findByText('hidden') await waitFor(() => { expect(abortedCount).toBe(1) }) })
it('throws aborted error (like fetch)', async () => { const countAtom = atom(0) const derivedAtom = abortableAtom(async (get, { signal }) => { const count = get(countAtom) await new Promise((r) => setTimeout(r, 100)) if (signal.aborted) { throw new Error('aborted') } return count })
const Component = () => { const count = useAtomValue(derivedAtom) return <div>count: {count}</div> }
const Controls = () => { const setCount = useSetAtom(countAtom) return ( <> <button onClick={() => setCount((c) => c + 1)}>button</button> </> ) }
const { findByText, getByText } = render( <StrictMode> <Provider> <Suspense fallback="loading"> <Component /> <Controls /> </Suspense> </Provider> </StrictMode> )
await findByText('loading') await findByText('count: 0')
fireEvent.click(getByText('button')) fireEvent.click(getByText('button')) await findByText('count: 2')
fireEvent.click(getByText('button')) await findByText('count: 3') })})
jotai

Version Info

Tagged at
a year ago