1
1
import { StrictMode , Suspense } 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 } from 'jotai/react'
6
5
import { atom } from 'jotai/vanilla'
7
6
import { RESET , atomWithDefault } from 'jotai/vanilla/utils'
8
7
9
- const userEvent = {
10
- click : ( element : Element ) => act ( ( ) => userEventOrig . click ( element ) ) ,
11
- }
8
+ beforeEach ( ( ) => {
9
+ vi . useFakeTimers ( )
10
+ } )
11
+
12
+ afterEach ( ( ) => {
13
+ vi . useRealTimers ( )
14
+ } )
12
15
13
- it ( 'simple sync get default' , async ( ) => {
16
+ it ( 'simple sync get default' , ( ) => {
14
17
const count1Atom = atom ( 1 )
15
18
const count2Atom = atomWithDefault ( ( get ) => get ( count1Atom ) * 2 )
16
19
@@ -34,23 +37,22 @@ it('simple sync get default', async () => {
34
37
</ StrictMode > ,
35
38
)
36
39
37
- expect ( await screen . findByText ( 'count1: 1, count2: 2' ) ) . toBeInTheDocument ( )
40
+ expect ( screen . getByText ( 'count1: 1, count2: 2' ) ) . toBeInTheDocument ( )
38
41
39
- await userEvent . click ( screen . getByText ( 'button1' ) )
40
- expect ( await screen . findByText ( 'count1: 2, count2: 4' ) ) . toBeInTheDocument ( )
42
+ fireEvent . click ( screen . getByText ( 'button1' ) )
43
+ expect ( screen . getByText ( 'count1: 2, count2: 4' ) ) . toBeInTheDocument ( )
41
44
42
- await userEvent . click ( screen . getByText ( 'button2' ) )
43
- expect ( await screen . findByText ( 'count1: 2, count2: 5' ) ) . toBeInTheDocument ( )
45
+ fireEvent . click ( screen . getByText ( 'button2' ) )
46
+ expect ( screen . getByText ( 'count1: 2, count2: 5' ) ) . toBeInTheDocument ( )
44
47
45
- await userEvent . click ( screen . getByText ( 'button1' ) )
46
- expect ( await screen . findByText ( 'count1: 3, count2: 5' ) ) . toBeInTheDocument ( )
48
+ fireEvent . click ( screen . getByText ( 'button1' ) )
49
+ expect ( screen . getByText ( 'count1: 3, count2: 5' ) ) . toBeInTheDocument ( )
47
50
} )
48
51
49
52
it ( 'simple async get default' , async ( ) => {
50
53
const count1Atom = atom ( 1 )
51
- let resolve = ( ) => { }
52
54
const count2Atom = atomWithDefault ( async ( get ) => {
53
- await new Promise < void > ( ( r ) => ( resolve = r ) )
55
+ await new Promise < void > ( ( resolve ) => setTimeout ( resolve , 100 ) )
54
56
return get ( count1Atom ) * 2
55
57
} )
56
58
@@ -80,25 +82,25 @@ it('simple async get default', async () => {
80
82
)
81
83
} )
82
84
83
- expect ( await screen . findByText ( 'loading' ) ) . toBeInTheDocument ( )
84
- resolve ( )
85
- expect ( await screen . findByText ( 'count1: 1, count2: 2' ) ) . toBeInTheDocument ( )
85
+ expect ( screen . getByText ( 'loading' ) ) . toBeInTheDocument ( )
86
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 100 ) )
87
+ expect ( screen . getByText ( 'count1: 1, count2: 2' ) ) . toBeInTheDocument ( )
86
88
87
- await userEvent . click ( screen . getByText ( 'button1' ) )
88
- expect ( await screen . findByText ( 'loading' ) ) . toBeInTheDocument ( )
89
- resolve ( )
90
- expect ( await screen . findByText ( 'count1: 2, count2: 4' ) ) . toBeInTheDocument ( )
89
+ await act ( ( ) => fireEvent . click ( screen . getByText ( 'button1' ) ) )
90
+ expect ( screen . getByText ( 'loading' ) ) . toBeInTheDocument ( )
91
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 100 ) )
92
+ expect ( screen . getByText ( 'count1: 2, count2: 4' ) ) . toBeInTheDocument ( )
91
93
92
- await userEvent . click ( screen . getByText ( 'button2' ) )
93
- resolve ( )
94
- expect ( await screen . findByText ( 'count1: 2, count2: 5' ) ) . toBeInTheDocument ( )
94
+ await act ( ( ) => fireEvent . click ( screen . getByText ( 'button2' ) ) )
95
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 100 ) )
96
+ expect ( screen . getByText ( 'count1: 2, count2: 5' ) ) . toBeInTheDocument ( )
95
97
96
- await userEvent . click ( screen . getByText ( 'button1' ) )
97
- resolve ( )
98
- expect ( await screen . findByText ( 'count1: 3, count2: 5' ) ) . toBeInTheDocument ( )
98
+ await act ( ( ) => fireEvent . click ( screen . getByText ( 'button1' ) ) )
99
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 100 ) )
100
+ expect ( screen . getByText ( 'count1: 3, count2: 5' ) ) . toBeInTheDocument ( )
99
101
} )
100
102
101
- it ( 'refresh sync atoms to default values' , async ( ) => {
103
+ it ( 'refresh sync atoms to default values' , ( ) => {
102
104
const count1Atom = atom ( 1 )
103
105
const count2Atom = atomWithDefault ( ( get ) => get ( count1Atom ) * 2 )
104
106
@@ -123,29 +125,28 @@ it('refresh sync atoms to default values', async () => {
123
125
</ StrictMode > ,
124
126
)
125
127
126
- expect ( await screen . findByText ( 'count1: 1, count2: 2' ) ) . toBeInTheDocument ( )
128
+ expect ( screen . getByText ( 'count1: 1, count2: 2' ) ) . toBeInTheDocument ( )
127
129
128
- await userEvent . click ( screen . getByText ( 'button1' ) )
129
- expect ( await screen . findByText ( 'count1: 2, count2: 4' ) ) . toBeInTheDocument ( )
130
+ fireEvent . click ( screen . getByText ( 'button1' ) )
131
+ expect ( screen . getByText ( 'count1: 2, count2: 4' ) ) . toBeInTheDocument ( )
130
132
131
- await userEvent . click ( screen . getByText ( 'button2' ) )
132
- expect ( await screen . findByText ( 'count1: 2, count2: 5' ) ) . toBeInTheDocument ( )
133
+ fireEvent . click ( screen . getByText ( 'button2' ) )
134
+ expect ( screen . getByText ( 'count1: 2, count2: 5' ) ) . toBeInTheDocument ( )
133
135
134
- await userEvent . click ( screen . getByText ( 'button1' ) )
135
- expect ( await screen . findByText ( 'count1: 3, count2: 5' ) ) . toBeInTheDocument ( )
136
+ fireEvent . click ( screen . getByText ( 'button1' ) )
137
+ expect ( screen . getByText ( 'count1: 3, count2: 5' ) ) . toBeInTheDocument ( )
136
138
137
- await userEvent . click ( screen . getByText ( 'Refresh count2' ) )
138
- expect ( await screen . findByText ( 'count1: 3, count2: 6' ) ) . toBeInTheDocument ( )
139
+ fireEvent . click ( screen . getByText ( 'Refresh count2' ) )
140
+ expect ( screen . getByText ( 'count1: 3, count2: 6' ) ) . toBeInTheDocument ( )
139
141
140
- await userEvent . click ( screen . getByText ( 'button1' ) )
141
- expect ( await screen . findByText ( 'count1: 4, count2: 8' ) ) . toBeInTheDocument ( )
142
+ fireEvent . click ( screen . getByText ( 'button1' ) )
143
+ expect ( screen . getByText ( 'count1: 4, count2: 8' ) ) . toBeInTheDocument ( )
142
144
} )
143
145
144
146
it ( 'refresh async atoms to default values' , async ( ) => {
145
147
const count1Atom = atom ( 1 )
146
- let resolve = ( ) => { }
147
148
const count2Atom = atomWithDefault ( async ( get ) => {
148
- await new Promise < void > ( ( r ) => ( resolve = r ) )
149
+ await new Promise < void > ( ( reslove ) => setTimeout ( reslove , 100 ) )
149
150
return get ( count1Atom ) * 2
150
151
} )
151
152
@@ -176,45 +177,35 @@ it('refresh async atoms to default values', async () => {
176
177
)
177
178
} )
178
179
179
- expect ( await screen . findByText ( 'loading' ) ) . toBeInTheDocument ( )
180
- await waitFor ( ( ) => {
181
- resolve ( )
182
- expect ( screen . getByText ( 'count1: 1, count2: 2' ) ) . toBeInTheDocument ( )
183
- } )
180
+ expect ( screen . getByText ( 'loading' ) ) . toBeInTheDocument ( )
181
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 100 ) )
182
+ expect ( screen . getByText ( 'count1: 1, count2: 2' ) ) . toBeInTheDocument ( )
184
183
185
- await userEvent . click ( screen . getByText ( 'button1' ) )
186
- await screen . findByText ( 'loading' )
187
- await waitFor ( ( ) => {
188
- resolve ( )
189
- expect ( screen . getByText ( 'count1: 2, count2: 4' ) ) . toBeInTheDocument ( )
190
- } )
184
+ await act ( ( ) => fireEvent . click ( screen . getByText ( 'button1' ) ) )
185
+ expect ( screen . getByText ( 'loading' ) ) . toBeInTheDocument ( )
186
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 100 ) )
187
+ expect ( screen . getByText ( 'count1: 2, count2: 4' ) ) . toBeInTheDocument ( )
191
188
192
- await userEvent . click ( screen . getByText ( 'button2' ) )
193
- await waitFor ( ( ) => {
194
- resolve ( )
195
- expect ( screen . getByText ( 'count1: 2, count2: 5' ) ) . toBeInTheDocument ( )
196
- } )
189
+ await act ( ( ) => fireEvent . click ( screen . getByText ( 'button2' ) ) )
190
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 100 ) )
191
+ expect ( screen . getByText ( 'count1: 2, count2: 5' ) ) . toBeInTheDocument ( )
197
192
198
- await userEvent . click ( screen . getByText ( 'button1' ) )
199
- await waitFor ( ( ) => {
200
- resolve ( )
201
- expect ( screen . getByText ( 'count1: 3, count2: 5' ) ) . toBeInTheDocument ( )
202
- } )
193
+ await act ( ( ) => fireEvent . click ( screen . getByText ( 'button1' ) ) )
194
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 100 ) )
195
+ expect ( screen . getByText ( 'count1: 3, count2: 5' ) ) . toBeInTheDocument ( )
203
196
204
- await userEvent . click ( screen . getByText ( 'Refresh count2' ) )
205
- await waitFor ( ( ) => {
206
- resolve ( )
207
- expect ( screen . getByText ( 'count1: 3, count2: 6' ) ) . toBeInTheDocument ( )
208
- } )
197
+ await act ( ( ) => fireEvent . click ( screen . getByText ( 'Refresh count2' ) ) )
198
+ expect ( screen . getByText ( 'loading' ) ) . toBeInTheDocument ( )
199
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 100 ) )
200
+ expect ( screen . getByText ( 'count1: 3, count2: 6' ) ) . toBeInTheDocument ( )
209
201
210
- await userEvent . click ( screen . getByText ( 'button1' ) )
211
- await waitFor ( ( ) => {
212
- resolve ( )
213
- expect ( screen . getByText ( 'count1: 4, count2: 8' ) ) . toBeInTheDocument ( )
214
- } )
202
+ await act ( ( ) => fireEvent . click ( screen . getByText ( 'button1' ) ) )
203
+ expect ( screen . getByText ( 'loading' ) ) . toBeInTheDocument ( )
204
+ await act ( ( ) => vi . advanceTimersByTimeAsync ( 100 ) )
205
+ expect ( screen . getByText ( 'count1: 4, count2: 8' ) ) . toBeInTheDocument ( )
215
206
} )
216
207
217
- it ( 'can be set synchronously by passing value' , async ( ) => {
208
+ it ( 'can be set synchronously by passing value' , ( ) => {
218
209
const countAtom = atomWithDefault ( ( ) => 1 )
219
210
220
211
const Counter = ( ) => {
@@ -232,7 +223,6 @@ it('can be set synchronously by passing value', async () => {
232
223
233
224
expect ( screen . getByText ( 'count: 1' ) ) . toBeInTheDocument ( )
234
225
235
- await userEvent . click ( screen . getByRole ( 'button' , { name : 'Set to 10' } ) )
236
-
226
+ fireEvent . click ( screen . getByRole ( 'button' , { name : 'Set to 10' } ) )
237
227
expect ( screen . getByText ( 'count: 10' ) ) . toBeInTheDocument ( )
238
228
} )
0 commit comments