在React应用中,我有一个按钮可以通过程序跳转到新的URL:
我该如何在react-testing-library中等待页面变化?
<ComposeButton
onClick={() => {
history.push('some-link'))
}}
>
在我的测试中,我按照React Router的react-testing-library文档所述渲染了我的组件:
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>
)
}
我该如何在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
})
当使用 debug
时,我无法看到新页面的内容(在点击按钮后)
Router
来渲染你的组件? - thedudeRouter
添加到测试中,但测试仍然不等待新页面内容。 - AlbertMunichMar