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 , vi } 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 } from 'jotai/react'
6
5
import { atom } from 'jotai/vanilla'
7
6
8
- const userEvent = {
9
- click : ( element : Element ) => act ( ( ) => userEventOrig . click ( element ) ) ,
10
- }
7
+ beforeEach ( ( ) => {
8
+ vi . useFakeTimers ( )
9
+ } )
10
+
11
+ afterEach ( ( ) => {
12
+ vi . useRealTimers ( )
13
+ } )
11
14
12
- it ( 'one atom, one effect' , async ( ) => {
15
+ it ( 'one atom, one effect' , ( ) => {
13
16
const countAtom = atom ( 1 )
14
17
const onMountFn = vi . fn ( ( ) => { } )
15
18
countAtom . onMount = onMountFn
@@ -24,21 +27,17 @@ it('one atom, one effect', async () => {
24
27
)
25
28
}
26
29
27
- render (
28
- < >
29
- < Counter />
30
- </ > ,
31
- )
30
+ render ( < Counter /> )
32
31
33
- expect ( await screen . findByText ( 'count: 1' ) ) . toBeInTheDocument ( )
32
+ expect ( screen . getByText ( 'count: 1' ) ) . toBeInTheDocument ( )
34
33
expect ( onMountFn ) . toHaveBeenCalledTimes ( 1 )
35
34
36
- await userEvent . click ( screen . getByText ( 'button' ) )
37
- expect ( await screen . findByText ( 'count: 2' ) ) . toBeInTheDocument ( )
35
+ fireEvent . click ( screen . getByText ( 'button' ) )
36
+ expect ( screen . getByText ( 'count: 2' ) ) . toBeInTheDocument ( )
38
37
expect ( onMountFn ) . toHaveBeenCalledTimes ( 1 )
39
38
} )
40
39
41
- it ( 'two atoms, one each' , async ( ) => {
40
+ it ( 'two atoms, one each' , ( ) => {
42
41
const countAtom = atom ( 1 )
43
42
const countAtom2 = atom ( 1 )
44
43
const onMountFn = vi . fn ( ( ) => { } )
@@ -65,30 +64,23 @@ it('two atoms, one each', async () => {
65
64
)
66
65
}
67
66
68
- render (
69
- < >
70
- < Counter />
71
- </ > ,
72
- )
67
+ render ( < Counter /> )
68
+
69
+ expect ( screen . getByText ( 'count: 1' ) ) . toBeInTheDocument ( )
70
+ expect ( screen . getByText ( 'count2: 1' ) ) . toBeInTheDocument ( )
73
71
74
- await waitFor ( ( ) => {
75
- expect ( screen . getByText ( 'count: 1' ) ) . toBeInTheDocument ( )
76
- expect ( screen . getByText ( 'count2: 1' ) ) . toBeInTheDocument ( )
77
- } )
78
72
expect ( onMountFn ) . toHaveBeenCalledTimes ( 1 )
79
73
expect ( onMountFn2 ) . toHaveBeenCalledTimes ( 1 )
80
74
81
- await userEvent . click ( screen . getByText ( 'button' ) )
82
- await waitFor ( ( ) => {
83
- expect ( screen . getByText ( 'count: 2' ) ) . toBeInTheDocument ( )
84
- expect ( screen . getByText ( 'count2: 2' ) ) . toBeInTheDocument ( )
85
- } )
75
+ fireEvent . click ( screen . getByText ( 'button' ) )
76
+ expect ( screen . getByText ( 'count: 2' ) ) . toBeInTheDocument ( )
77
+ expect ( screen . getByText ( 'count2: 2' ) ) . toBeInTheDocument ( )
86
78
87
79
expect ( onMountFn ) . toHaveBeenCalledTimes ( 1 )
88
80
expect ( onMountFn2 ) . toHaveBeenCalledTimes ( 1 )
89
81
} )
90
82
91
- it ( 'one derived atom, one onMount' , async ( ) => {
83
+ it ( 'one derived atom, one onMount' , ( ) => {
92
84
const countAtom = atom ( 1 )
93
85
const countAtom2 = atom ( ( get ) => get ( countAtom ) )
94
86
const onMountFn = vi . fn ( ( ) => { } )
@@ -103,17 +95,14 @@ it('one derived atom, one onMount', async () => {
103
95
)
104
96
}
105
97
106
- render (
107
- < >
108
- < Counter />
109
- </ > ,
110
- )
98
+ render ( < Counter /> )
99
+
100
+ expect ( screen . getByText ( 'count: 1' ) ) . toBeInTheDocument ( )
111
101
112
- expect ( await screen . findByText ( 'count: 1' ) ) . toBeInTheDocument ( )
113
102
expect ( onMountFn ) . toHaveBeenCalledTimes ( 1 )
114
103
} )
115
104
116
- it ( 'mount/unmount test' , async ( ) => {
105
+ it ( 'mount/unmount test' , ( ) => {
117
106
const countAtom = atom ( 1 )
118
107
119
108
const onUnMountFn = vi . fn ( )
@@ -139,22 +128,18 @@ it('mount/unmount test', async () => {
139
128
)
140
129
}
141
130
142
- render (
143
- < >
144
- < Display />
145
- </ > ,
146
- )
131
+ render ( < Display /> )
147
132
148
133
expect ( onMountFn ) . toHaveBeenCalledTimes ( 1 )
149
134
expect ( onUnMountFn ) . toHaveBeenCalledTimes ( 0 )
150
135
151
- await userEvent . click ( screen . getByText ( 'button' ) )
136
+ fireEvent . click ( screen . getByText ( 'button' ) )
152
137
153
138
expect ( onMountFn ) . toHaveBeenCalledTimes ( 1 )
154
139
expect ( onUnMountFn ) . toHaveBeenCalledTimes ( 1 )
155
140
} )
156
141
157
- it ( 'one derived atom, one onMount for the derived one, and one for the regular atom + onUnMount' , async ( ) => {
142
+ it ( 'one derived atom, one onMount for the derived one, and one for the regular atom + onUnMount' , ( ) => {
158
143
const countAtom = atom ( 1 )
159
144
const derivedAtom = atom (
160
145
( get ) => get ( countAtom ) ,
@@ -189,26 +174,22 @@ it('one derived atom, one onMount for the derived one, and one for the regular a
189
174
)
190
175
}
191
176
192
- render (
193
- < >
194
- < Display />
195
- </ > ,
196
- )
177
+ render ( < Display /> )
197
178
198
179
expect ( derivedOnMountFn ) . toHaveBeenCalledTimes ( 1 )
199
180
expect ( derivedOnUnMountFn ) . toHaveBeenCalledTimes ( 0 )
200
181
expect ( onMountFn ) . toHaveBeenCalledTimes ( 1 )
201
182
expect ( onUnMountFn ) . toHaveBeenCalledTimes ( 0 )
202
183
203
- await userEvent . click ( screen . getByText ( 'button' ) )
184
+ fireEvent . click ( screen . getByText ( 'button' ) )
204
185
205
186
expect ( derivedOnMountFn ) . toHaveBeenCalledTimes ( 1 )
206
187
expect ( derivedOnUnMountFn ) . toHaveBeenCalledTimes ( 1 )
207
188
expect ( onMountFn ) . toHaveBeenCalledTimes ( 1 )
208
189
expect ( onUnMountFn ) . toHaveBeenCalledTimes ( 1 )
209
190
} )
210
191
211
- it ( 'mount/unMount order' , async ( ) => {
192
+ it ( 'mount/unMount order' , ( ) => {
212
193
const committed : number [ ] = [ 0 , 0 ]
213
194
const countAtom = atom ( 1 )
214
195
const derivedAtom = atom (
@@ -273,29 +254,22 @@ it('mount/unMount order', async () => {
273
254
274
255
expect ( committed ) . toEqual ( [ 0 , 0 ] )
275
256
276
- await userEvent . click ( screen . getByText ( 'button' ) )
277
- await waitFor ( ( ) => {
278
- expect ( committed ) . toEqual ( [ 1 , 0 ] )
279
- } )
257
+ fireEvent . click ( screen . getByText ( 'button' ) )
258
+ expect ( committed ) . toEqual ( [ 1 , 0 ] )
280
259
281
- await userEvent . click ( screen . getByText ( 'derived atom' ) )
282
- await waitFor ( ( ) => {
283
- expect ( committed ) . toEqual ( [ 1 , 1 ] )
284
- } )
260
+ fireEvent . click ( screen . getByText ( 'derived atom' ) )
261
+ expect ( committed ) . toEqual ( [ 1 , 1 ] )
285
262
286
- await userEvent . click ( screen . getByText ( 'derived atom' ) )
287
- await waitFor ( ( ) => {
288
- expect ( committed ) . toEqual ( [ 1 , 0 ] )
289
- } )
263
+ fireEvent . click ( screen . getByText ( 'derived atom' ) )
264
+ expect ( committed ) . toEqual ( [ 1 , 0 ] )
290
265
291
- await userEvent . click ( screen . getByText ( 'button' ) )
292
- await waitFor ( ( ) => {
293
- expect ( committed ) . toEqual ( [ 0 , 0 ] )
294
- } )
266
+ fireEvent . click ( screen . getByText ( 'button' ) )
267
+ expect ( committed ) . toEqual ( [ 0 , 0 ] )
295
268
} )
296
269
297
270
it ( 'mount/unmount test with async atom' , async ( ) => {
298
271
let resolve = ( ) => { }
272
+
299
273
const countAtom = atom (
300
274
async ( ) => {
301
275
await new Promise < void > ( ( r ) => ( resolve = r ) )
@@ -327,28 +301,28 @@ it('mount/unmount test with async atom', async () => {
327
301
)
328
302
}
329
303
330
- await act ( async ( ) => {
304
+ await act ( ( ) =>
331
305
render (
332
- < >
333
- < Suspense fallback = "loading" >
334
- < Display />
335
- </ Suspense >
336
- </ > ,
337
- )
338
- } )
306
+ < Suspense fallback = "loading" >
307
+ < Display />
308
+ </ Suspense > ,
309
+ ) ,
310
+ )
311
+
312
+ expect ( screen . getByText ( 'loading' ) ) . toBeInTheDocument ( )
313
+
314
+ await act ( async ( ) => resolve ( ) )
339
315
340
- expect ( await screen . findByText ( 'loading' ) ) . toBeInTheDocument ( )
341
- resolve ( )
342
- await screen . findByText ( 'count: 0' )
316
+ expect ( screen . getByText ( 'count: 0' ) ) . toBeInTheDocument ( )
343
317
expect ( onMountFn ) . toHaveBeenCalledTimes ( 1 )
344
318
expect ( onUnMountFn ) . toHaveBeenCalledTimes ( 0 )
345
319
346
- await userEvent . click ( screen . getByText ( 'button' ) )
320
+ fireEvent . click ( screen . getByText ( 'button' ) )
347
321
expect ( onMountFn ) . toHaveBeenCalledTimes ( 1 )
348
322
expect ( onUnMountFn ) . toHaveBeenCalledTimes ( 1 )
349
323
} )
350
324
351
- it ( 'subscription usage test' , async ( ) => {
325
+ it ( 'subscription usage test' , ( ) => {
352
326
const store = {
353
327
count : 10 ,
354
328
listeners : new Set < ( ) => void > ( ) ,
@@ -393,32 +367,30 @@ it('subscription usage test', async () => {
393
367
</ StrictMode > ,
394
368
)
395
369
396
- expect ( await screen . findByText ( 'count: 10' ) ) . toBeInTheDocument ( )
370
+ expect ( screen . getByText ( 'count: 10' ) ) . toBeInTheDocument ( )
397
371
398
- act ( ( ) => {
399
- store . inc ( )
400
- } )
401
- expect ( await screen . findByText ( 'count: 11' ) ) . toBeInTheDocument ( )
372
+ act ( ( ) => store . inc ( ) )
402
373
403
- await userEvent . click ( screen . getByText ( 'button' ) )
404
- expect ( await screen . findByText ( 'N/A' ) ) . toBeInTheDocument ( )
374
+ expect ( screen . getByText ( 'count: 11' ) ) . toBeInTheDocument ( )
405
375
406
- await userEvent . click ( screen . getByText ( 'button' ) )
407
- expect ( await screen . findByText ( 'count: 11 ') ) . toBeInTheDocument ( )
376
+ fireEvent . click ( screen . getByText ( 'button' ) )
377
+ expect ( screen . getByText ( 'N/A ') ) . toBeInTheDocument ( )
408
378
409
- await userEvent . click ( screen . getByText ( 'button' ) )
410
- expect ( await screen . findByText ( 'N/A ') ) . toBeInTheDocument ( )
379
+ fireEvent . click ( screen . getByText ( 'button' ) )
380
+ expect ( screen . getByText ( 'count: 11 ') ) . toBeInTheDocument ( )
411
381
412
- act ( ( ) => {
413
- store . inc ( )
414
- } )
415
- expect ( await screen . findByText ( 'N/A' ) ) . toBeInTheDocument ( )
382
+ fireEvent . click ( screen . getByText ( 'button' ) )
383
+ expect ( screen . getByText ( 'N/A' ) ) . toBeInTheDocument ( )
416
384
417
- await userEvent . click ( screen . getByText ( 'button' ) )
418
- expect ( await screen . findByText ( 'count: 12' ) ) . toBeInTheDocument ( )
385
+ act ( ( ) => store . inc ( ) )
386
+
387
+ expect ( screen . getByText ( 'N/A' ) ) . toBeInTheDocument ( )
388
+
389
+ fireEvent . click ( screen . getByText ( 'button' ) )
390
+ expect ( screen . getByText ( 'count: 12' ) ) . toBeInTheDocument ( )
419
391
} )
420
392
421
- it ( 'subscription in base atom test' , async ( ) => {
393
+ it ( 'subscription in base atom test' , ( ) => {
422
394
const store = {
423
395
count : 10 ,
424
396
listeners : new Set < ( ) => void > ( ) ,
@@ -460,13 +432,13 @@ it('subscription in base atom test', async () => {
460
432
</ StrictMode > ,
461
433
)
462
434
463
- expect ( await screen . findByText ( 'count: 10' ) ) . toBeInTheDocument ( )
435
+ expect ( screen . getByText ( 'count: 10' ) ) . toBeInTheDocument ( )
464
436
465
- await userEvent . click ( screen . getByText ( 'button' ) )
466
- expect ( await screen . findByText ( 'count: 11' ) ) . toBeInTheDocument ( )
437
+ fireEvent . click ( screen . getByText ( 'button' ) )
438
+ expect ( screen . getByText ( 'count: 11' ) ) . toBeInTheDocument ( )
467
439
468
- await userEvent . click ( screen . getByText ( 'button' ) )
469
- expect ( await screen . findByText ( 'count: 12' ) ) . toBeInTheDocument ( )
440
+ fireEvent . click ( screen . getByText ( 'button' ) )
441
+ expect ( screen . getByText ( 'count: 12' ) ) . toBeInTheDocument ( )
470
442
} )
471
443
472
444
it ( 'create atom with onMount in async get' , async ( ) => {
@@ -508,24 +480,26 @@ it('create atom with onMount in async get', async () => {
508
480
)
509
481
}
510
482
511
- await act ( async ( ) => {
483
+ await act ( ( ) =>
512
484
render (
513
485
< StrictMode >
514
486
< Suspense fallback = "loading" >
515
487
< Counter />
516
488
</ Suspense >
517
489
</ StrictMode > ,
518
- )
519
- } )
490
+ ) ,
491
+ )
520
492
521
493
// FIXME this is not working
522
- //await screen.findByText('count: 1')
494
+ // await screen.findByText('count: 1')
523
495
524
- expect ( await screen . findByText ( 'count: 10' ) ) . toBeInTheDocument ( )
496
+ expect ( screen . getByText ( 'count: 10' ) ) . toBeInTheDocument ( )
525
497
526
- await userEvent . click ( screen . getByText ( 'button' ) )
527
- expect ( await screen . findByText ( 'count: 11' ) ) . toBeInTheDocument ( )
498
+ await act ( ( ) => fireEvent . click ( screen . getByText ( 'button' ) ) )
499
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 100 ) )
500
+ expect ( screen . getByText ( 'count: 11' ) ) . toBeInTheDocument ( )
528
501
529
- await userEvent . click ( screen . getByText ( 'button' ) )
530
- expect ( await screen . findByText ( 'count: 12' ) ) . toBeInTheDocument ( )
502
+ await act ( ( ) => fireEvent . click ( screen . getByText ( 'button' ) ) )
503
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 100 ) )
504
+ expect ( screen . getByText ( 'count: 12' ) ) . toBeInTheDocument ( )
531
505
} )
0 commit comments