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,84 @@ 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+ ; ( {
280+ cleanup,
281+ render,
282+ screen,
283+ waitFor,
284+ waitForElementToBeRemoved,
285+ } = require ( '..' ) )
286+ } )
287+
288+ afterEach ( async ( ) => {
289+ await cleanup ( )
290+ jest . useRealTimers ( )
291+ global . IS_REACT_ACT_ENVIRONMENT = true
292+ } )
293+
294+ const fetchAMessageInAMicrotask = ( ) =>
295+ Promise . resolve ( {
296+ status : 200 ,
297+ json : ( ) => Promise . resolve ( { title : 'Hello World' } ) ,
298+ } )
299+
300+ function ComponentWithMicrotaskLoader ( ) {
301+ const [ fetchState , setFetchState ] = React . useState ( { fetching : true } )
302+
303+ React . useEffect ( ( ) => {
304+ if ( fetchState . fetching ) {
305+ fetchAMessageInAMicrotask ( ) . then ( res => {
306+ return res . json ( ) . then ( data => {
307+ setFetchState ( { todo : data . title , fetching : false } )
308+ } )
309+ } )
310+ }
311+ } , [ fetchState ] )
312+
313+ if ( fetchState . fetching ) {
314+ return < p > Loading..</ p >
315+ }
316+
317+ return (
318+ < div data-testid = "message" > Loaded this message: { fetchState . todo } </ div >
319+ )
320+ }
321+
322+ test ( 'waitFor' , async ( ) => {
323+ await render ( < ComponentWithMicrotaskLoader /> )
324+
325+ // TODO: How to assert on the intermediate state?
326+ await expect (
327+ waitFor ( ( ) => {
328+ expect ( screen . getByText ( 'Loading..' ) ) . toBeInTheDocument ( )
329+ } ) ,
330+ ) . rejects . toThrowError ( / U n a b l e t o f i n d a n e l e m e n t / )
331+
332+ expect ( screen . getByTestId ( 'message' ) ) . toHaveTextContent ( / H e l l o W o r l d / )
333+ } )
334+
335+ test ( 'findBy' , async ( ) => {
336+ await render ( < ComponentWithMicrotaskLoader /> )
337+
338+ // TODO: How to assert on the intermediate state?
339+ await expect ( screen . findByText ( 'Loading..' ) ) . rejects . toThrowError (
340+ / U n a b l e t o f i n d a n e l e m e n t / ,
341+ )
342+
343+ expect ( screen . getByTestId ( 'message' ) ) . toHaveTextContent ( / H e l l o W o r l d / )
344+ } )
345+ } )
0 commit comments