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: Backport tests using the full timer matrix #962

Merged
merged 1 commit into from Sep 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 56 additions & 20 deletions src/__tests__/end-to-end.js
@@ -1,5 +1,5 @@
import * as React from 'react'
import {render, waitForElementToBeRemoved, screen} from '../'
import {render, waitForElementToBeRemoved, screen, waitFor} from '../'

const fetchAMessage = () =>
new Promise(resolve => {
Expand All @@ -11,27 +11,63 @@ const fetchAMessage = () =>
}, randomTimeout)
})

class ComponentWithLoader extends React.Component {
state = {loading: true}
async componentDidMount() {
const data = await fetchAMessage()
this.setState({data, loading: false}) // eslint-disable-line
}
render() {
if (this.state.loading) {
return <div>Loading...</div>
function ComponentWithLoader() {
const [state, setState] = React.useState({data: undefined, loading: true})
React.useEffect(() => {
let cancelled = false
fetchAMessage().then(data => {
if (!cancelled) {
setState({data, loading: false})
}
})

return () => {
cancelled = true
}
return (
<div data-testid="message">
Loaded this message: {this.state.data.returnedMessage}!
</div>
)
}, [])

if (state.loading) {
return <div>Loading...</div>
}

return (
<div data-testid="message">
Loaded this message: {state.data.returnedMessage}!
</div>
)
}

test('it waits for the data to be loaded', async () => {
render(<ComponentWithLoader />)
const loading = () => screen.getByText('Loading...')
await waitForElementToBeRemoved(loading)
expect(screen.getByTestId('message')).toHaveTextContent(/Hello World/)
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 using %s', (label, useTimers) => {
beforeEach(() => {
useTimers()
})

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

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

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

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