1
1
import { Component , StrictMode , Suspense } from 'react'
2
2
import type { ReactNode } from 'react'
3
- import { act , render , screen } from '@testing-library/react'
4
- import userEvent from '@testing-library/user-event'
5
- import { expect , it } from 'vitest'
3
+ import { act , fireEvent , render , screen } from '@testing-library/react'
4
+ import { afterEach , beforeEach , expect , it , vi } from 'vitest'
6
5
import { useAtomValue , useSetAtom } from 'jotai/react'
7
6
import { atom } from 'jotai/vanilla'
8
7
8
+ beforeEach ( ( ) => {
9
+ vi . useFakeTimers ( )
10
+ } )
11
+
12
+ afterEach ( ( ) => {
13
+ vi . useRealTimers ( )
14
+ } )
15
+
9
16
it ( 'useAtomValue basic test' , async ( ) => {
10
17
const countAtom = atom ( 0 )
11
18
@@ -21,37 +28,11 @@ it('useAtomValue basic test', async () => {
21
28
)
22
29
}
23
30
24
- render (
25
- < StrictMode >
26
- < Counter />
27
- </ StrictMode > ,
28
- )
31
+ render ( < Counter /> )
29
32
30
- expect ( await screen . findByText ( 'count: 0' ) ) . toBeInTheDocument ( )
31
- await userEvent . click ( screen . getByText ( 'dispatch' ) )
32
- expect ( await screen . findByText ( 'count: 1' ) ) . toBeInTheDocument ( )
33
- } )
34
-
35
- it ( 'useAtomValue with async atom (promise)' , async ( ) => {
36
- const asyncAtom = atom ( async ( ) => 42 )
37
-
38
- const AsyncComponent = ( ) => {
39
- const value = useAtomValue ( asyncAtom )
40
-
41
- return < div > value: { value } </ div >
42
- }
43
-
44
- await act ( async ( ) => {
45
- render (
46
- < StrictMode >
47
- < Suspense fallback = "loading" >
48
- < AsyncComponent />
49
- </ Suspense >
50
- </ StrictMode > ,
51
- )
52
- } )
53
-
54
- expect ( await screen . findByText ( 'value: 42' ) ) . toBeInTheDocument ( )
33
+ expect ( screen . getByText ( 'count: 0' ) ) . toBeInTheDocument ( )
34
+ fireEvent . click ( screen . getByText ( 'dispatch' ) )
35
+ expect ( screen . getByText ( 'count: 1' ) ) . toBeInTheDocument ( )
55
36
} )
56
37
57
38
class ErrorBoundary extends Component <
@@ -96,7 +77,7 @@ it('useAtomValue with error throwing atom', async () => {
96
77
</ StrictMode > ,
97
78
)
98
79
99
- expect ( await screen . findByText ( 'error: fail' ) ) . toBeInTheDocument ( )
80
+ expect ( screen . getByText ( 'error: fail' ) ) . toBeInTheDocument ( )
100
81
} )
101
82
102
83
it ( 'useAtomValue with atom returning object' , async ( ) => {
@@ -118,5 +99,31 @@ it('useAtomValue with atom returning object', async () => {
118
99
</ StrictMode > ,
119
100
)
120
101
121
- expect ( await screen . findByText ( 'obj: 1,2' ) ) . toBeInTheDocument ( )
102
+ expect ( screen . getByText ( 'obj: 1,2' ) ) . toBeInTheDocument ( )
103
+ } )
104
+
105
+ it ( 'useAtomValue with async atom (promise)' , async ( ) => {
106
+ const asyncAtom = atom ( async ( ) => {
107
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) )
108
+ return 42
109
+ } )
110
+
111
+ const AsyncComponent = ( ) => {
112
+ const value = useAtomValue ( asyncAtom )
113
+
114
+ return < div > value: { value } </ div >
115
+ }
116
+
117
+ await act ( ( ) =>
118
+ render (
119
+ < Suspense fallback = { < div > loading</ div > } >
120
+ < AsyncComponent />
121
+ </ Suspense > ,
122
+ ) ,
123
+ )
124
+
125
+ expect ( screen . getByText ( 'loading' ) ) . toBeInTheDocument ( )
126
+
127
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 10 ) )
128
+ expect ( screen . getByText ( 'value: 42' ) ) . toBeInTheDocument ( )
122
129
} )
0 commit comments