1- import * as React from 'react'
2- import { render , waitForElementToBeRemoved , screen , waitFor } from '../'
1+ let React , cleanup , render , screen , waitFor , waitForElementToBeRemoved
32
43describe . each ( [
54 [ 'real timers' , ( ) => jest . useRealTimers ( ) ] ,
@@ -9,10 +8,25 @@ describe.each([
98 'it waits for the data to be loaded in a macrotask using %s' ,
109 ( label , useTimers ) => {
1110 beforeEach ( ( ) => {
11+ jest . resetModules ( )
12+ global . IS_REACT_ACT_ENVIRONMENT = true
13+ process . env . RTL_SKIP_AUTO_CLEANUP = '0'
14+
1215 useTimers ( )
16+
17+ React = require ( 'react' )
18+ ; ( {
19+ cleanup,
20+ render,
21+ screen,
22+ waitFor,
23+ waitForElementToBeRemoved,
24+ } = require ( '..' ) )
1325 } )
1426
15- afterEach ( ( ) => {
27+ afterEach ( async ( ) => {
28+ await cleanup ( )
29+ global . IS_REACT_ACT_ENVIRONMENT = false
1630 jest . useRealTimers ( )
1731 } )
1832
@@ -83,10 +97,25 @@ describe.each([
8397 'it waits for the data to be loaded in many microtask using %s' ,
8498 ( label , useTimers ) => {
8599 beforeEach ( ( ) => {
100+ jest . resetModules ( )
101+ global . IS_REACT_ACT_ENVIRONMENT = true
102+ process . env . RTL_SKIP_AUTO_CLEANUP = '0'
103+
86104 useTimers ( )
105+
106+ React = require ( 'react' )
107+ ; ( {
108+ cleanup,
109+ render,
110+ screen,
111+ waitFor,
112+ waitForElementToBeRemoved,
113+ } = require ( '..' ) )
87114 } )
88115
89- afterEach ( ( ) => {
116+ afterEach ( async ( ) => {
117+ await cleanup ( )
118+ global . IS_REACT_ACT_ENVIRONMENT = false
90119 jest . useRealTimers ( )
91120 } )
92121
@@ -167,10 +196,25 @@ describe.each([
167196 'it waits for the data to be loaded in a microtask using %s' ,
168197 ( label , useTimers ) => {
169198 beforeEach ( ( ) => {
199+ jest . resetModules ( )
200+ global . IS_REACT_ACT_ENVIRONMENT = true
201+ process . env . RTL_SKIP_AUTO_CLEANUP = '0'
202+
170203 useTimers ( )
204+
205+ React = require ( 'react' )
206+ ; ( {
207+ cleanup,
208+ render,
209+ screen,
210+ waitFor,
211+ waitForElementToBeRemoved,
212+ } = require ( '..' ) )
171213 } )
172214
173- afterEach ( ( ) => {
215+ afterEach ( async ( ) => {
216+ await cleanup ( )
217+ global . IS_REACT_ACT_ENVIRONMENT = false
174218 jest . useRealTimers ( )
175219 } )
176220
@@ -218,3 +262,78 @@ describe.each([
218262 } )
219263 } ,
220264)
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