| 
1 | 1 | import * as React from 'react'  | 
2 | 2 | import {render, waitForElementToBeRemoved, screen, waitFor} from '../'  | 
3 | 3 | 
 
  | 
4 |  | -const fetchAMessage = () =>  | 
5 |  | -  new Promise(resolve => {  | 
6 |  | -    // we are using random timeout here to simulate a real-time example  | 
7 |  | -    // of an async operation calling a callback at a non-deterministic time  | 
8 |  | -    const randomTimeout = Math.floor(Math.random() * 100)  | 
9 |  | -    setTimeout(() => {  | 
10 |  | -      resolve({returnedMessage: 'Hello World'})  | 
11 |  | -    }, randomTimeout)  | 
12 |  | -  })  | 
13 |  | - | 
14 |  | -function ComponentWithLoader() {  | 
15 |  | -  const [state, setState] = React.useState({data: undefined, loading: true})  | 
16 |  | -  React.useEffect(() => {  | 
17 |  | -    let cancelled = false  | 
18 |  | -    fetchAMessage().then(data => {  | 
19 |  | -      if (!cancelled) {  | 
20 |  | -        setState({data, loading: false})  | 
21 |  | -      }  | 
 | 4 | +describe.each([  | 
 | 5 | +  ['real timers', () => jest.useRealTimers()],  | 
 | 6 | +  ['fake legacy timers', () => jest.useFakeTimers('legacy')],  | 
 | 7 | +  ['fake modern timers', () => jest.useFakeTimers('modern')],  | 
 | 8 | +])(  | 
 | 9 | +  'it waits for the data to be loaded in a macrotask using %s',  | 
 | 10 | +  (label, useTimers) => {  | 
 | 11 | +    beforeEach(() => {  | 
 | 12 | +      useTimers()  | 
 | 13 | +    })  | 
 | 14 | + | 
 | 15 | +    afterEach(() => {  | 
 | 16 | +      jest.useRealTimers()  | 
22 | 17 |     })  | 
23 | 18 | 
 
  | 
24 |  | -    return () => {  | 
25 |  | -      cancelled = true  | 
 | 19 | +    const fetchAMessageInAMacrotask = () =>  | 
 | 20 | +      new Promise(resolve => {  | 
 | 21 | +        // we are using random timeout here to simulate a real-time example  | 
 | 22 | +        // of an async operation calling a callback at a non-deterministic time  | 
 | 23 | +        const randomTimeout = Math.floor(Math.random() * 100)  | 
 | 24 | +        setTimeout(() => {  | 
 | 25 | +          resolve({returnedMessage: 'Hello World'})  | 
 | 26 | +        }, randomTimeout)  | 
 | 27 | +      })  | 
 | 28 | + | 
 | 29 | +    function ComponentWithMacrotaskLoader() {  | 
 | 30 | +      const [state, setState] = React.useState({data: undefined, loading: true})  | 
 | 31 | +      React.useEffect(() => {  | 
 | 32 | +        let cancelled = false  | 
 | 33 | +        fetchAMessageInAMacrotask().then(data => {  | 
 | 34 | +          if (!cancelled) {  | 
 | 35 | +            setState({data, loading: false})  | 
 | 36 | +          }  | 
 | 37 | +        })  | 
 | 38 | + | 
 | 39 | +        return () => {  | 
 | 40 | +          cancelled = true  | 
 | 41 | +        }  | 
 | 42 | +      }, [])  | 
 | 43 | + | 
 | 44 | +      if (state.loading) {  | 
 | 45 | +        return <div>Loading...</div>  | 
 | 46 | +      }  | 
 | 47 | + | 
 | 48 | +      return (  | 
 | 49 | +        <div data-testid="message">  | 
 | 50 | +          Loaded this message: {state.data.returnedMessage}!  | 
 | 51 | +        </div>  | 
 | 52 | +      )  | 
26 | 53 |     }  | 
27 |  | -  }, [])  | 
28 | 54 | 
 
  | 
29 |  | -  if (state.loading) {  | 
30 |  | -    return <div>Loading...</div>  | 
31 |  | -  }  | 
 | 55 | +    test('waitForElementToBeRemoved', async () => {  | 
 | 56 | +      render(<ComponentWithMacrotaskLoader />)  | 
 | 57 | +      const loading = () => screen.getByText('Loading...')  | 
 | 58 | +      await waitForElementToBeRemoved(loading)  | 
 | 59 | +      expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)  | 
 | 60 | +    })  | 
 | 61 | + | 
 | 62 | +    test('waitFor', async () => {  | 
 | 63 | +      render(<ComponentWithMacrotaskLoader />)  | 
 | 64 | +      await waitFor(() => screen.getByText(/Loading../))  | 
 | 65 | +      await waitFor(() => screen.getByText(/Loaded this message:/))  | 
 | 66 | +      expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)  | 
 | 67 | +    })  | 
32 | 68 | 
 
  | 
33 |  | -  return (  | 
34 |  | -    <div data-testid="message">  | 
35 |  | -      Loaded this message: {state.data.returnedMessage}!  | 
36 |  | -    </div>  | 
37 |  | -  )  | 
38 |  | -}  | 
 | 69 | +    test('findBy', async () => {  | 
 | 70 | +      render(<ComponentWithMacrotaskLoader />)  | 
 | 71 | +      await expect(screen.findByTestId('message')).resolves.toHaveTextContent(  | 
 | 72 | +        /Hello World/,  | 
 | 73 | +      )  | 
 | 74 | +    })  | 
 | 75 | +  },  | 
 | 76 | +)  | 
39 | 77 | 
 
  | 
40 | 78 | describe.each([  | 
41 | 79 |   ['real timers', () => jest.useRealTimers()],  | 
42 | 80 |   ['fake legacy timers', () => jest.useFakeTimers('legacy')],  | 
43 | 81 |   ['fake modern timers', () => jest.useFakeTimers('modern')],  | 
44 |  | -])('it waits for the data to be loaded using %s', (label, useTimers) => {  | 
45 |  | -  beforeEach(() => {  | 
46 |  | -    useTimers()  | 
47 |  | -  })  | 
48 |  | - | 
49 |  | -  afterEach(() => {  | 
50 |  | -    jest.useRealTimers()  | 
51 |  | -  })  | 
52 |  | - | 
53 |  | -  test('waitForElementToBeRemoved', async () => {  | 
54 |  | -    render(<ComponentWithLoader />)  | 
55 |  | -    const loading = () => screen.getByText('Loading...')  | 
56 |  | -    await waitForElementToBeRemoved(loading)  | 
57 |  | -    expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)  | 
58 |  | -  })  | 
59 |  | - | 
60 |  | -  test('waitFor', async () => {  | 
61 |  | -    render(<ComponentWithLoader />)  | 
62 |  | -    const message = () => screen.getByText(/Loaded this message:/)  | 
63 |  | -    await waitFor(message)  | 
64 |  | -    expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)  | 
65 |  | -  })  | 
66 |  | - | 
67 |  | -  test('findBy', async () => {  | 
68 |  | -    render(<ComponentWithLoader />)  | 
69 |  | -    await expect(screen.findByTestId('message')).resolves.toHaveTextContent(  | 
70 |  | -      /Hello World/,  | 
71 |  | -    )  | 
72 |  | -  })  | 
73 |  | -})  | 
 | 82 | +])(  | 
 | 83 | +  'it waits for the data to be loaded in a microtask using %s',  | 
 | 84 | +  (label, useTimers) => {  | 
 | 85 | +    beforeEach(() => {  | 
 | 86 | +      useTimers()  | 
 | 87 | +    })  | 
 | 88 | + | 
 | 89 | +    afterEach(() => {  | 
 | 90 | +      jest.useRealTimers()  | 
 | 91 | +    })  | 
 | 92 | + | 
 | 93 | +    const fetchAMessageInAMicrotask = () =>  | 
 | 94 | +      Promise.resolve({  | 
 | 95 | +        status: 200,  | 
 | 96 | +        json: () => Promise.resolve({title: 'Hello World'}),  | 
 | 97 | +      })  | 
 | 98 | + | 
 | 99 | +    function ComponentWithMicrotaskLoader() {  | 
 | 100 | +      const [fetchState, setFetchState] = React.useState({fetching: true})  | 
 | 101 | + | 
 | 102 | +      React.useEffect(() => {  | 
 | 103 | +        if (fetchState.fetching) {  | 
 | 104 | +          fetchAMessageInAMicrotask().then(res => {  | 
 | 105 | +            return (  | 
 | 106 | +              res  | 
 | 107 | +                .json()  | 
 | 108 | +                // By spec, the runtime can only yield back to the event loop once  | 
 | 109 | +                // the microtask queue is empty.  | 
 | 110 | +                // So we ensure that we actually wait for that as well before yielding back from `waitFor`.  | 
 | 111 | +                .then(data => data)  | 
 | 112 | +                .then(data => data)  | 
 | 113 | +                .then(data => data)  | 
 | 114 | +                .then(data => data)  | 
 | 115 | +                .then(data => data)  | 
 | 116 | +                .then(data => data)  | 
 | 117 | +                .then(data => data)  | 
 | 118 | +                .then(data => data)  | 
 | 119 | +                .then(data => data)  | 
 | 120 | +                .then(data => data)  | 
 | 121 | +                .then(data => data)  | 
 | 122 | +                .then(data => {  | 
 | 123 | +                  setFetchState({todo: data.title, fetching: false})  | 
 | 124 | +                })  | 
 | 125 | +            )  | 
 | 126 | +          })  | 
 | 127 | +        }  | 
 | 128 | +      }, [fetchState])  | 
 | 129 | + | 
 | 130 | +      if (fetchState.fetching) {  | 
 | 131 | +        return <p>Loading..</p>  | 
 | 132 | +      }  | 
 | 133 | + | 
 | 134 | +      return (  | 
 | 135 | +        <div data-testid="message">Loaded this message: {fetchState.todo}</div>  | 
 | 136 | +      )  | 
 | 137 | +    }  | 
 | 138 | + | 
 | 139 | +    test('waitForElementToBeRemoved', async () => {  | 
 | 140 | +      render(<ComponentWithMicrotaskLoader />)  | 
 | 141 | +      const loading = () => screen.getByText('Loading..')  | 
 | 142 | +      await waitForElementToBeRemoved(loading)  | 
 | 143 | +      expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)  | 
 | 144 | +    })  | 
 | 145 | + | 
 | 146 | +    test('waitFor', async () => {  | 
 | 147 | +      render(<ComponentWithMicrotaskLoader />)  | 
 | 148 | +      await waitFor(() => {  | 
 | 149 | +        screen.getByText('Loading..')  | 
 | 150 | +      })  | 
 | 151 | +      await waitFor(() => {  | 
 | 152 | +        screen.getByText(/Loaded this message:/)  | 
 | 153 | +      })  | 
 | 154 | +      expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)  | 
 | 155 | +    })  | 
 | 156 | + | 
 | 157 | +    test('findBy', async () => {  | 
 | 158 | +      render(<ComponentWithMicrotaskLoader />)  | 
 | 159 | +      await expect(screen.findByTestId('message')).resolves.toHaveTextContent(  | 
 | 160 | +        /Hello World/,  | 
 | 161 | +      )  | 
 | 162 | +    })  | 
 | 163 | +  },  | 
 | 164 | +)  | 
0 commit comments