1
1
import { StrictMode , Suspense , useState } from 'react'
2
- import { act , render , screen , waitFor } from '@testing-library/react'
3
- import userEventOrig from '@testing-library/user-event'
4
- import { expect , it } from 'vitest'
2
+ import { act , fireEvent , render , screen } from '@testing-library/react'
3
+ import { afterEach , beforeEach , expect , it , vi } from 'vitest'
5
4
import { useAtom , useSetAtom } from 'jotai/react'
6
5
import { atom } from 'jotai/vanilla'
7
6
import type { SetStateAction , WritableAtom } from 'jotai/vanilla'
8
7
import { atomFamily } from 'jotai/vanilla/utils'
9
8
10
- const userEvent = {
11
- click : ( element : Element ) => act ( ( ) => userEventOrig . click ( element ) ) ,
12
- }
9
+ beforeEach ( ( ) => {
10
+ vi . useFakeTimers ( )
11
+ } )
12
+
13
+ afterEach ( ( ) => {
14
+ vi . useRealTimers ( )
15
+ } )
13
16
14
- it ( 'new atomFamily impl' , async ( ) => {
17
+ it ( 'new atomFamily impl' , ( ) => {
15
18
const myFamily = atomFamily ( ( param : string ) => atom ( param ) )
16
19
17
20
const Displayer = ( { index } : { index : string } ) => {
@@ -21,31 +24,33 @@ it('new atomFamily impl', async () => {
21
24
22
25
render (
23
26
< StrictMode >
24
- < Displayer index = { 'a' } />
27
+ < Displayer index = "a" />
25
28
</ StrictMode > ,
26
29
)
27
30
28
- expect ( await screen . findByText ( 'count: a' ) ) . toBeInTheDocument ( )
31
+ expect ( screen . getByText ( 'count: a' ) ) . toBeInTheDocument ( )
29
32
} )
30
33
31
- it ( 'primitive atomFamily returns same reference for same parameters' , async ( ) => {
34
+ it ( 'primitive atomFamily returns same reference for same parameters' , ( ) => {
32
35
const myFamily = atomFamily ( ( num : number ) => atom ( { num } ) )
36
+
33
37
expect ( myFamily ( 0 ) ) . toEqual ( myFamily ( 0 ) )
34
38
expect ( myFamily ( 0 ) ) . not . toEqual ( myFamily ( 1 ) )
35
39
expect ( myFamily ( 1 ) ) . not . toEqual ( myFamily ( 0 ) )
36
40
} )
37
41
38
- it ( 'read-only derived atomFamily returns same reference for same parameters' , async ( ) => {
42
+ it ( 'read-only derived atomFamily returns same reference for same parameters' , ( ) => {
39
43
const arrayAtom = atom ( [ 0 ] )
40
44
const myFamily = atomFamily ( ( num : number ) =>
41
45
atom ( ( get ) => get ( arrayAtom ) [ num ] as number ) ,
42
46
)
47
+
43
48
expect ( myFamily ( 0 ) ) . toEqual ( myFamily ( 0 ) )
44
49
expect ( myFamily ( 0 ) ) . not . toEqual ( myFamily ( 1 ) )
45
50
expect ( myFamily ( 1 ) ) . not . toEqual ( myFamily ( 0 ) )
46
51
} )
47
52
48
- it ( 'removed atom creates a new reference' , async ( ) => {
53
+ it ( 'removed atom creates a new reference' , ( ) => {
49
54
const bigAtom = atom ( [ 0 ] )
50
55
const myFamily = atomFamily ( ( num : number ) =>
51
56
atom ( ( get ) => get ( bigAtom ) [ num ] as number ) ,
@@ -66,7 +71,7 @@ it('removed atom creates a new reference', async () => {
66
71
expect ( myFamily ( 0 ) ) . toEqual ( newReference )
67
72
} )
68
73
69
- it ( 'primitive atomFamily initialized with props' , async ( ) => {
74
+ it ( 'primitive atomFamily initialized with props' , ( ) => {
70
75
const myFamily = atomFamily ( ( param : number ) => atom ( param ) )
71
76
72
77
const Displayer = ( { index } : { index : number } ) => {
@@ -96,19 +101,19 @@ it('primitive atomFamily initialized with props', async () => {
96
101
</ StrictMode > ,
97
102
)
98
103
99
- expect ( await screen . findByText ( 'count: 1' ) ) . toBeInTheDocument ( )
104
+ expect ( screen . getByText ( 'count: 1' ) ) . toBeInTheDocument ( )
100
105
101
- await userEvent . click ( screen . getByText ( 'button' ) )
102
- expect ( await screen . findByText ( 'count: 11' ) ) . toBeInTheDocument ( )
106
+ fireEvent . click ( screen . getByText ( 'button' ) )
107
+ expect ( screen . getByText ( 'count: 11' ) ) . toBeInTheDocument ( )
103
108
104
- await userEvent . click ( screen . getByText ( 'increment' ) )
105
- expect ( await screen . findByText ( 'count: 2' ) ) . toBeInTheDocument ( )
109
+ fireEvent . click ( screen . getByText ( 'increment' ) )
110
+ expect ( screen . getByText ( 'count: 2' ) ) . toBeInTheDocument ( )
106
111
107
- await userEvent . click ( screen . getByText ( 'button' ) )
108
- expect ( await screen . findByText ( 'count: 12' ) ) . toBeInTheDocument ( )
112
+ fireEvent . click ( screen . getByText ( 'button' ) )
113
+ expect ( screen . getByText ( 'count: 12' ) ) . toBeInTheDocument ( )
109
114
} )
110
115
111
- it ( 'derived atomFamily functionality as usual' , async ( ) => {
116
+ it ( 'derived atomFamily functionality as usual' , ( ) => {
112
117
const arrayAtom = atom ( [ 0 , 0 , 0 ] )
113
118
114
119
const myFamily = atomFamily ( ( param : number ) =>
@@ -173,35 +178,27 @@ it('derived atomFamily functionality as usual', async () => {
173
178
</ StrictMode > ,
174
179
)
175
180
176
- await waitFor ( ( ) => {
177
- expect ( screen . getByText ( 'index: 0, count: 0' ) ) . toBeInTheDocument ( )
178
- expect ( screen . getByText ( 'index: 1, count: 0' ) ) . toBeInTheDocument ( )
179
- expect ( screen . getByText ( 'index: 2, count: 0' ) ) . toBeInTheDocument ( )
180
- } )
181
+ expect ( screen . getByText ( 'index: 0, count: 0' ) ) . toBeInTheDocument ( )
182
+ expect ( screen . getByText ( 'index: 1, count: 0' ) ) . toBeInTheDocument ( )
183
+ expect ( screen . getByText ( 'index: 2, count: 0' ) ) . toBeInTheDocument ( )
181
184
182
- await userEvent . click ( screen . getByText ( 'increment #1' ) )
183
- await waitFor ( ( ) => {
184
- expect ( screen . getByText ( 'index: 0, count: 0' ) ) . toBeInTheDocument ( )
185
- expect ( screen . getByText ( 'index: 1, count: 1' ) ) . toBeInTheDocument ( )
186
- expect ( screen . getByText ( 'index: 2, count: 0' ) ) . toBeInTheDocument ( )
187
- } )
185
+ fireEvent . click ( screen . getByText ( 'increment #1' ) )
186
+ expect ( screen . getByText ( 'index: 0, count: 0' ) ) . toBeInTheDocument ( )
187
+ expect ( screen . getByText ( 'index: 1, count: 1' ) ) . toBeInTheDocument ( )
188
+ expect ( screen . getByText ( 'index: 2, count: 0' ) ) . toBeInTheDocument ( )
188
189
189
- await userEvent . click ( screen . getByText ( 'increment #0' ) )
190
- await waitFor ( ( ) => {
191
- expect ( screen . getByText ( 'index: 0, count: 1' ) ) . toBeInTheDocument ( )
192
- expect ( screen . getByText ( 'index: 1, count: 1' ) ) . toBeInTheDocument ( )
193
- expect ( screen . getByText ( 'index: 2, count: 0' ) ) . toBeInTheDocument ( )
194
- } )
190
+ fireEvent . click ( screen . getByText ( 'increment #0' ) )
191
+ expect ( screen . getByText ( 'index: 0, count: 1' ) ) . toBeInTheDocument ( )
192
+ expect ( screen . getByText ( 'index: 1, count: 1' ) ) . toBeInTheDocument ( )
193
+ expect ( screen . getByText ( 'index: 2, count: 0' ) ) . toBeInTheDocument ( )
195
194
196
- await userEvent . click ( screen . getByText ( 'increment #2' ) )
197
- await waitFor ( ( ) => {
198
- expect ( screen . getByText ( 'index: 0, count: 1' ) ) . toBeInTheDocument ( )
199
- expect ( screen . getByText ( 'index: 1, count: 1' ) ) . toBeInTheDocument ( )
200
- expect ( screen . getByText ( 'index: 2, count: 1' ) ) . toBeInTheDocument ( )
201
- } )
195
+ fireEvent . click ( screen . getByText ( 'increment #2' ) )
196
+ expect ( screen . getByText ( 'index: 0, count: 1' ) ) . toBeInTheDocument ( )
197
+ expect ( screen . getByText ( 'index: 1, count: 1' ) ) . toBeInTheDocument ( )
198
+ expect ( screen . getByText ( 'index: 2, count: 1' ) ) . toBeInTheDocument ( )
202
199
} )
203
200
204
- it ( 'custom equality function work' , async ( ) => {
201
+ it ( 'custom equality function work' , ( ) => {
205
202
const bigAtom = atom ( [ 0 ] )
206
203
207
204
const badFamily = atomFamily ( ( num : { index : number } ) =>
@@ -223,10 +220,9 @@ it('custom equality function work', async () => {
223
220
224
221
it ( 'a derived atom from an async atomFamily (#351)' , async ( ) => {
225
222
const countAtom = atom ( 1 )
226
- const resolve : ( ( ) => void ) [ ] = [ ]
227
223
const getAsyncAtom = atomFamily ( ( n : number ) =>
228
224
atom ( async ( ) => {
229
- await new Promise < void > ( ( r ) => resolve . push ( r ) )
225
+ await new Promise < void > ( ( resolve ) => setTimeout ( resolve , 100 ) )
230
226
return n + 10
231
227
} ) ,
232
228
)
@@ -243,29 +239,31 @@ it('a derived atom from an async atomFamily (#351)', async () => {
243
239
)
244
240
}
245
241
246
- await act ( async ( ) => {
242
+ await act ( ( ) =>
247
243
render (
248
244
< StrictMode >
249
245
< Suspense fallback = "loading" >
250
246
< Counter />
251
247
</ Suspense >
252
248
</ StrictMode > ,
253
- )
254
- } )
255
-
256
- expect ( await screen . findByText ( 'loading' ) ) . toBeInTheDocument ( )
257
- resolve . splice ( 0 ) . forEach ( ( fn ) => fn ( ) )
258
- expect ( await screen . findByText ( 'derived: 11' ) ) . toBeInTheDocument ( )
259
-
260
- await userEvent . click ( screen . getByText ( 'button' ) )
261
- expect ( await screen . findByText ( 'loading' ) ) . toBeInTheDocument ( )
262
- resolve . splice ( 0 ) . forEach ( ( fn ) => fn ( ) )
263
- expect ( await screen . findByText ( 'derived: 12' ) ) . toBeInTheDocument ( )
249
+ ) ,
250
+ )
264
251
265
- await userEvent . click ( screen . getByText ( 'button' ) )
266
- expect ( await screen . findByText ( 'loading' ) ) . toBeInTheDocument ( )
267
- resolve . splice ( 0 ) . forEach ( ( fn ) => fn ( ) )
268
- expect ( await screen . findByText ( 'derived: 13' ) ) . toBeInTheDocument ( )
252
+ expect ( screen . getByText ( 'loading' ) ) . toBeInTheDocument ( )
253
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 100 ) )
254
+ expect ( screen . getByText ( 'derived: 11' ) ) . toBeInTheDocument ( )
255
+
256
+ await act ( ( ) => fireEvent . click ( screen . getByText ( 'button' ) ) )
257
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 0 ) )
258
+ expect ( screen . getByText ( 'loading' ) ) . toBeInTheDocument ( )
259
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 100 ) )
260
+ expect ( screen . getByText ( 'derived: 12' ) ) . toBeInTheDocument ( )
261
+
262
+ await act ( ( ) => fireEvent . click ( screen . getByText ( 'button' ) ) )
263
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 0 ) )
264
+ expect ( screen . getByText ( 'loading' ) ) . toBeInTheDocument ( )
265
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 100 ) )
266
+ expect ( screen . getByText ( 'derived: 13' ) ) . toBeInTheDocument ( )
269
267
} )
270
268
271
269
it ( 'setShouldRemove with custom equality function' , async ( ) => {
0 commit comments