|
1 | 1 | import * as React from 'react'
|
2 |
| -import {render, waitForElementToBeRemoved, screen} from '../' |
| 2 | +import {render, waitForElementToBeRemoved, screen, waitFor} from '../' |
3 | 3 |
|
4 | 4 | const fetchAMessage = () =>
|
5 | 5 | new Promise(resolve => {
|
@@ -29,9 +29,37 @@ class ComponentWithLoader extends React.Component {
|
29 | 29 | }
|
30 | 30 | }
|
31 | 31 |
|
32 |
| -test('it waits for the data to be loaded', async () => { |
33 |
| - render(<ComponentWithLoader />) |
34 |
| - const loading = () => screen.getByText('Loading...') |
35 |
| - await waitForElementToBeRemoved(loading) |
36 |
| - expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/) |
| 32 | +describe.each([ |
| 33 | + ['real timers', () => jest.useRealTimers()], |
| 34 | + ['fake legacy timers', () => jest.useFakeTimers('legacy')], |
| 35 | + ['fake modern timers', () => jest.useFakeTimers('modern')], |
| 36 | +])('it waits for the data to be loaded using %s', (label, useTimers) => { |
| 37 | + beforeEach(() => { |
| 38 | + useTimers() |
| 39 | + }) |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + jest.useRealTimers() |
| 43 | + }) |
| 44 | + |
| 45 | + test('waitForElementToBeRemoved', async () => { |
| 46 | + render(<ComponentWithLoader />) |
| 47 | + const loading = () => screen.getByText('Loading...') |
| 48 | + await waitForElementToBeRemoved(loading) |
| 49 | + expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/) |
| 50 | + }) |
| 51 | + |
| 52 | + test('waitFor', async () => { |
| 53 | + render(<ComponentWithLoader />) |
| 54 | + const message = () => screen.getByText(/Loaded this message:/) |
| 55 | + await waitFor(message) |
| 56 | + expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/) |
| 57 | + }) |
| 58 | + |
| 59 | + test('findBy', async () => { |
| 60 | + render(<ComponentWithLoader />) |
| 61 | + await expect(screen.findByTestId('message')).resolves.toHaveTextContent( |
| 62 | + /Hello World/, |
| 63 | + ) |
| 64 | + }) |
37 | 65 | })
|
0 commit comments