1
- import * as React from 'react'
2
- import { render , waitForElementToBeRemoved , screen , waitFor } from '../'
1
+ let React , cleanup , render , screen , waitFor , waitForElementToBeRemoved
3
2
4
3
describe . each ( [
5
4
[ 'real timers' , ( ) => jest . useRealTimers ( ) ] ,
@@ -9,10 +8,25 @@ describe.each([
9
8
'it waits for the data to be loaded in a macrotask using %s' ,
10
9
( label , useTimers ) => {
11
10
beforeEach ( ( ) => {
11
+ jest . resetModules ( )
12
+ global . IS_REACT_ACT_ENVIRONMENT = true
13
+ process . env . RTL_SKIP_AUTO_CLEANUP = '0'
14
+
12
15
useTimers ( )
16
+
17
+ React = require ( 'react' )
18
+ ; ( {
19
+ cleanup,
20
+ render,
21
+ screen,
22
+ waitFor,
23
+ waitForElementToBeRemoved,
24
+ } = require ( '..' ) )
13
25
} )
14
26
15
- afterEach ( ( ) => {
27
+ afterEach ( async ( ) => {
28
+ await cleanup ( )
29
+ global . IS_REACT_ACT_ENVIRONMENT = false
16
30
jest . useRealTimers ( )
17
31
} )
18
32
@@ -83,10 +97,25 @@ describe.each([
83
97
'it waits for the data to be loaded in many microtask using %s' ,
84
98
( label , useTimers ) => {
85
99
beforeEach ( ( ) => {
100
+ jest . resetModules ( )
101
+ global . IS_REACT_ACT_ENVIRONMENT = true
102
+ process . env . RTL_SKIP_AUTO_CLEANUP = '0'
103
+
86
104
useTimers ( )
105
+
106
+ React = require ( 'react' )
107
+ ; ( {
108
+ cleanup,
109
+ render,
110
+ screen,
111
+ waitFor,
112
+ waitForElementToBeRemoved,
113
+ } = require ( '..' ) )
87
114
} )
88
115
89
- afterEach ( ( ) => {
116
+ afterEach ( async ( ) => {
117
+ await cleanup ( )
118
+ global . IS_REACT_ACT_ENVIRONMENT = false
90
119
jest . useRealTimers ( )
91
120
} )
92
121
@@ -167,10 +196,25 @@ describe.each([
167
196
'it waits for the data to be loaded in a microtask using %s' ,
168
197
( label , useTimers ) => {
169
198
beforeEach ( ( ) => {
199
+ jest . resetModules ( )
200
+ global . IS_REACT_ACT_ENVIRONMENT = true
201
+ process . env . RTL_SKIP_AUTO_CLEANUP = '0'
202
+
170
203
useTimers ( )
204
+
205
+ React = require ( 'react' )
206
+ ; ( {
207
+ cleanup,
208
+ render,
209
+ screen,
210
+ waitFor,
211
+ waitForElementToBeRemoved,
212
+ } = require ( '..' ) )
171
213
} )
172
214
173
- afterEach ( ( ) => {
215
+ afterEach ( async ( ) => {
216
+ await cleanup ( )
217
+ global . IS_REACT_ACT_ENVIRONMENT = false
174
218
jest . useRealTimers ( )
175
219
} )
176
220
@@ -218,3 +262,78 @@ describe.each([
218
262
} )
219
263
} ,
220
264
)
265
+
266
+ describe . each ( [
267
+ [ 'real timers' , ( ) => jest . useRealTimers ( ) ] ,
268
+ [ 'fake legacy timers' , ( ) => jest . useFakeTimers ( 'legacy' ) ] ,
269
+ [ 'fake modern timers' , ( ) => jest . useFakeTimers ( 'modern' ) ] ,
270
+ ] ) ( 'testing intermediate states using %s' , ( label , useTimers ) => {
271
+ beforeEach ( ( ) => {
272
+ jest . resetModules ( )
273
+ global . IS_REACT_ACT_ENVIRONMENT = false
274
+ process . env . RTL_SKIP_AUTO_CLEANUP = '0'
275
+
276
+ useTimers ( )
277
+
278
+ React = require ( 'react' )
279
+ ; ( { render, screen, waitFor, waitForElementToBeRemoved} = require ( '..' ) )
280
+ } )
281
+
282
+ afterEach ( async ( ) => {
283
+ await cleanup ( )
284
+ jest . useRealTimers ( )
285
+ global . IS_REACT_ACT_ENVIRONMENT = true
286
+ } )
287
+
288
+ const fetchAMessageInAMicrotask = ( ) =>
289
+ Promise . resolve ( {
290
+ status : 200 ,
291
+ json : ( ) => Promise . resolve ( { title : 'Hello World' } ) ,
292
+ } )
293
+
294
+ function ComponentWithMicrotaskLoader ( ) {
295
+ const [ fetchState , setFetchState ] = React . useState ( { fetching : true } )
296
+
297
+ React . useEffect ( ( ) => {
298
+ if ( fetchState . fetching ) {
299
+ fetchAMessageInAMicrotask ( ) . then ( res => {
300
+ return res . json ( ) . then ( data => {
301
+ setFetchState ( { todo : data . title , fetching : false } )
302
+ } )
303
+ } )
304
+ }
305
+ } , [ fetchState ] )
306
+
307
+ if ( fetchState . fetching ) {
308
+ return < p > Loading..</ p >
309
+ }
310
+
311
+ return (
312
+ < div data-testid = "message" > Loaded this message: { fetchState . todo } </ div >
313
+ )
314
+ }
315
+
316
+ test ( 'waitFor' , async ( ) => {
317
+ await render ( < ComponentWithMicrotaskLoader /> )
318
+
319
+ await waitFor ( ( ) => {
320
+ expect ( screen . getByText ( 'Loading..' ) ) . toBeInTheDocument ( )
321
+ } )
322
+
323
+ await waitFor ( ( ) => {
324
+ expect ( screen . getByText ( / L o a d e d t h i s m e s s a g e : / ) ) . toBeInTheDocument ( )
325
+ } )
326
+
327
+ expect ( screen . getByTestId ( 'message' ) ) . toHaveTextContent ( / H e l l o W o r l d / )
328
+ } )
329
+
330
+ test ( 'findBy' , async ( ) => {
331
+ await render ( < ComponentWithMicrotaskLoader /> )
332
+
333
+ await screen . findByText ( 'Loading..' )
334
+
335
+ await screen . findByText ( / L o a d e d t h i s m e s s a g e : / )
336
+
337
+ expect ( screen . getByTestId ( 'message' ) ) . toHaveTextContent ( / H e l l o W o r l d / )
338
+ } )
339
+ } )
0 commit comments