用开玩笑 / RTL进行React路由器的测试路线
我正在尝试使用RTL和JEST进行React测试一些路线,但是由于测试不知道路线,测试似乎失败了吗?我试图遵循 rtl docs 我正在使用> rtl docs React-Router-dom”:“^5.2.0”
我的路线:
const Routing: React.FunctionComponent = () => {
return (
<>
<BrowserRouter>
<Header/>
<div className="App">
<Switch>
<Route exact path="/">
<Redirect to="/home" />
</Route>
<Route exact path={["/home", "/"]} component={Home} />
<Route path="/account-details/:account?/:id" render={(props: RouteComponentProps<any>) => <AccountDetail {...props} />} />
<Route component={NotFound} />
</Switch>
</div>
</BrowserRouter>
</>
)
}
export default Routing;
我的测试:
const accountQuery = new QueryClient()
it('Should test 404 route', async () => {
const history = createMemoryHistory();
history.push('/bad/route')
await render(
<QueryClientProvider client={accountQuery}>
<Router history={history}>
<App />
</Router>,
</QueryClientProvider>
)
expect(await screen.findByText(/404 error/i)).toBeInTheDocument();
});
如果我断言以下:
expect(history.location.pathname).toBe('/bad/route');
它通过即使我已经渲染了该应用程序的路线?
有什么想法吗?我希望它实际上从应用程序中获取路线?
I am trying to test some routes using RTL and Jest for React, But the test seem to be failing as I don't think the test knows the routes? I have tried to follow the RTL docs I am using "react-router-dom": "^5.2.0"
My routes:
const Routing: React.FunctionComponent = () => {
return (
<>
<BrowserRouter>
<Header/>
<div className="App">
<Switch>
<Route exact path="/">
<Redirect to="/home" />
</Route>
<Route exact path={["/home", "/"]} component={Home} />
<Route path="/account-details/:account?/:id" render={(props: RouteComponentProps<any>) => <AccountDetail {...props} />} />
<Route component={NotFound} />
</Switch>
</div>
</BrowserRouter>
</>
)
}
export default Routing;
My test:
const accountQuery = new QueryClient()
it('Should test 404 route', async () => {
const history = createMemoryHistory();
history.push('/bad/route')
await render(
<QueryClientProvider client={accountQuery}>
<Router history={history}>
<App />
</Router>,
</QueryClientProvider>
)
expect(await screen.findByText(/404 error/i)).toBeInTheDocument();
});
If I assert the following:
expect(history.location.pathname).toBe('/bad/route');
It passes, but it does not seem to be actually taking the routes from the App even though I have rendered it?
Any Idea's? I want it to actually get the routes from the App?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要进行一些重构,分为两个部分:
路由
和app
组件。browserrouter
将创建浏览器历史
,然后将其传递给&lt; router/&gt;
组件,您的自定义内存历史记录将不使用。参见 browserrouter.js#l14browserrouter.js
( react-router-dom v5 ):doutes.tsx
:doutes.test.tsx
:app.tsxx
:通过这种方式,您可以使用自己的自定义内存历史记录测试
路由
组件。测试结果:
You need to do some refactoring, split into two parts:
Routes
andApp
components. TheBrowserRouter
will create browserhistory
and pass it to the<Router/>
component, your custom memory history will not be used. See BrowserRouter.js#L14BrowserRouter.js
(react-router-dom v5):routes.tsx
:routes.test.tsx
:app.tsx
:In this way, you can test the
Routing
component with your own custom memory history.Test result: