In a React App, I have a button that goes to a new url programatically:
<ComposeButton
onClick={() => {
history.push('some-link'))
}}
>
In my test, I render my component as mentioned in the react-testing-library documentation for React Router:
const renderComponent = (page: Pages) => {
const history = createMemoryHistory()
return renderWithState(
<MockedProvider
mocks={mocks}
addTypename={false}
defaultOptions={{
watchQuery: { fetchPolicy: 'no-cache' },
query: { fetchPolicy: 'no-cache' }
}}
>
<Router history={history}>
<ComponentToRender />
</Router>
</MockedProvider>
)
}
How can I wait for this change page in react-testing-library?
it('sends invitations', async () => {
const { getByTestId, queryByTestId, debug, getByText } = renderComponent(
Pages.contacts
)
await new Promise((resolve) => setTimeout(resolve))
const writeMessageBtn = getByTestId('write-message-btn')
waitFor(() => fireEvent.click(writeMessageBtn))
await new Promise((resolve) => setTimeout(resolve))
waitFor(() => debug()) // <- expect to see new page
getByText('EV_305_NEW_INVITATION_SEND') // <- this is in the second page, never get here
})
I can never see the content of the new page (after clicking the button) when using debug
Routerwith a defined route for 'some-link'? - thedudeRouterto the test, but still test does not wait for new page content. - AlbertMunichMar