使用到达路由器的应用。
具有两页,分别显示apage和bpage Apage和BPage
Apage具有标题和按钮。单击该按钮后,应用程序导航到bpage。
App.js
import { Router, navigate } from "@reach/router";
export default function AppWithRouter() {
const APage = () => {
const handle = () => {
navigate('bpage')
}
return(
<div>
<h1>A Page</h1>
<button onClick={handle}>Sign In</button>
</div>
)
}
const BPage = () => (<div><h1>B Page</h1>BPage</div>)
return (
<Router>
<APage path="/apage" />
<BPage path="/bpage" />
</Router>
);
}
使用@ testing-library / react使用标题的文本内容测试从一页到另一页的导航。
基于https://testing-library.com/docs/example-reach-router
renderwithRouter.js
export default function renderWithRouter(ui, route = "/") {
let source = createMemorySource(route);
let history = createHistory(source);
return {
...render(<LocationProvider history={history}>{ui}</LocationProvider>),
history
};
}
我对导航的测试
App.test.js
test('click navigates to b page', async () => {
const { getByRole } = renderWithRouter(<App />, '/apage')
fireEvent.click(getByRole("button"));
await wait(() => {
expect(getByRole('heading')).toHaveTextContent(/b page/i)
});
});
给予失败
Expected element to have text content:
/b page/i
Received:
A Page
如何测试此导航?
答案 0 :(得分:1)
只需模拟@reach/router 导航功能
import { navigate } from '@reach/router'
import * as React from 'react'
import { render, fireEvent } from 'react-testing-library'
import PageA from './PageA'
jest.mock('@reach/router', () => ({
navigate: jest.fn(),
}))
describe('Page A', () => {
it('Should navigate from Page A to Page B', () => {
const { getByText } = render(<Page A />)
fireEvent.click(getByText(/Sign In/i))
expect(navigate).toHaveBeenCalledTimes(1)
expect(navigate).toHaveBeenCalledWith('/bpage')
})
})
我们相信如果使用正确路径调用导航函数将导航到页面 B,example here
答案 1 :(得分:0)
我有同样的问题。我使用的解决方法是模拟@ reach-router并断言其导航功能已被调用。在测试期间,历史记录不会更改,但可以在应用程序上使用。不知道是什么导致了问题的开始。抱歉,我帮不上忙。这是一个例子。
import {navigate} from '@reach/router'
import {waitFor, act} from '@testing-library/react'
import MagicUser from '@testing-library/user-event'
import {ValidUser} from '@test-utils/fixtures/user'
import {Routes} from '@utils/routes'
import {renderAuthForm} from './_utils'
const {username, password} = ValidUser
jest.mock('../../api', () => ({
AuthFormApi: {
login: jest.fn(() => Promise.resolve({user: {username}})),
register: jest.fn(() => Promise.resolve({user: {username}})),
},
}))
jest.mock('@reach/router', () => ({
navigate: jest.fn(),
}))
describe('Successful user login', () => {
describe('submitting the form', () => {
it('navigates to the home page and adds user to local storage', async () => {
const {getByRole} = renderAuthForm({
username,
password,
})
const submitButton = getByRole('button')
await act(async () => {
MagicUser.click(submitButton)
await waitFor(() => {
expect(navigate).toHaveBeenCalledWith(Routes.Home)
expect(navigate).toHaveBeenCalledTimes(1)
})
})
})
})
})