Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add test for flushing before exiting waitFor #1215

Merged
merged 4 commits into from
May 28, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 73 additions & 3 deletions src/__tests__/end-to-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import * as React from 'react'
import {render, waitForElementToBeRemoved, screen, waitFor} from '../'

describe.each([
['real timers', () => jest.useRealTimers()],
['fake legacy timers', () => jest.useFakeTimers('legacy')],
// ['real timers', () => jest.useRealTimers()],
// ['fake legacy timers', () => jest.useFakeTimers('legacy')],
eps1lon marked this conversation as resolved.
Show resolved Hide resolved
['fake modern timers', () => jest.useFakeTimers('modern')],
])(
'it waits for the data to be loaded in a macrotask using %s',
Expand Down Expand Up @@ -80,7 +80,7 @@ describe.each([
['fake legacy timers', () => jest.useFakeTimers('legacy')],
['fake modern timers', () => jest.useFakeTimers('modern')],
])(
'it waits for the data to be loaded in a microtask using %s',
'it waits for the data to be loaded in many microtask using %s',
(label, useTimers) => {
beforeEach(() => {
useTimers()
Expand Down Expand Up @@ -162,3 +162,73 @@ describe.each([
})
},
)

describe.each([
['real timers', () => jest.useRealTimers()],
['fake legacy timers', () => jest.useFakeTimers('legacy')],
['fake modern timers', () => jest.useFakeTimers('modern')],
])(
'it waits for the data to be loaded in a microtask using %s',
(label, useTimers) => {
beforeEach(() => {
useTimers()
})

afterEach(() => {
jest.useRealTimers()
})

const fetchAMessageInAMicrotask = () =>
Promise.resolve({
status: 200,
json: () => Promise.resolve({title: 'Hello World'}),
})

function ComponentWithMicrotaskLoader() {
const [fetchState, setFetchState] = React.useState({fetching: true})

React.useEffect(() => {
if (fetchState.fetching) {
fetchAMessageInAMicrotask().then(res => {
return res.json().then(data => {
setFetchState({todo: data.title, fetching: false})
})
})
}
}, [fetchState])

if (fetchState.fetching) {
return <p>Loading..</p>
}

return (
<div data-testid="message">Loaded this message: {fetchState.todo}</div>
)
}

test('waitForElementToBeRemoved', async () => {
render(<ComponentWithMicrotaskLoader />)
const loading = () => screen.getByText('Loading..')
await waitForElementToBeRemoved(loading)
expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)
})

test('waitFor', async () => {
render(<ComponentWithMicrotaskLoader />)
await waitFor(() => {
screen.getByText('Loading..')
})
await waitFor(() => {
screen.getByText(/Loaded this message:/)
})
expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)
})

test('findBy', async () => {
render(<ComponentWithMicrotaskLoader />)
await expect(screen.findByTestId('message')).resolves.toHaveTextContent(
/Hello World/,
)
})
},
)