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
@@ -27,31 +34,9 @@ it('useAtomValue basic test', async () => {
27
34
</ StrictMode > ,
28
35
)
29
36
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 ( )
37
+ expect ( screen . getByText ( 'count: 0' ) ) . toBeInTheDocument ( )
38
+ fireEvent . click ( screen . getByText ( 'dispatch' ) )
39
+ expect ( screen . getByText ( 'count: 1' ) ) . toBeInTheDocument ( )
55
40
} )
56
41
57
42
class ErrorBoundary extends Component <
@@ -96,7 +81,7 @@ it('useAtomValue with error throwing atom', async () => {
96
81
</ StrictMode > ,
97
82
)
98
83
99
- expect ( await screen . findByText ( 'error: fail' ) ) . toBeInTheDocument ( )
84
+ expect ( screen . getByText ( 'error: fail' ) ) . toBeInTheDocument ( )
100
85
} )
101
86
102
87
it ( 'useAtomValue with atom returning object' , async ( ) => {
@@ -118,5 +103,33 @@ it('useAtomValue with atom returning object', async () => {
118
103
</ StrictMode > ,
119
104
)
120
105
121
- expect ( await screen . findByText ( 'obj: 1,2' ) ) . toBeInTheDocument ( )
106
+ expect ( screen . getByText ( 'obj: 1,2' ) ) . toBeInTheDocument ( )
107
+ } )
108
+
109
+ it ( 'useAtomValue with async atom (promise)' , async ( ) => {
110
+ const asyncAtom = atom ( async ( ) => {
111
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) )
112
+ return 42
113
+ } )
114
+
115
+ const AsyncComponent = ( ) => {
116
+ const value = useAtomValue ( asyncAtom )
117
+
118
+ return < div > value: { value } </ div >
119
+ }
120
+
121
+ await act ( ( ) =>
122
+ render (
123
+ < StrictMode >
124
+ < Suspense fallback = { < div > loading</ div > } >
125
+ < AsyncComponent />
126
+ </ Suspense >
127
+ </ StrictMode > ,
128
+ ) ,
129
+ )
130
+
131
+ expect ( screen . getByText ( 'loading' ) ) . toBeInTheDocument ( )
132
+
133
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 10 ) )
134
+ expect ( screen . getByText ( 'value: 42' ) ) . toBeInTheDocument ( )
122
135
} )
0 commit comments