【发布时间】:2018-12-17 01:40:58
【问题描述】:
我想测试从/ 路径到语言环境路径(例如/en)的重定向。所以这是组件的样子:
// GuessLocale is imported from a helper
const App = () => (
<Router>
<Switch>
<Route exact path='/' render={() => (
<Redirect to={`/${guessLocale()}`} />
)} />
<Route exact path='/:lang' component={Home} />
</Switch>
</Router>
)
这是当前的测试功能:
it('redirects to a localed path', () => {
const wrapper = mount(
<MemoryRouter initialEntries={['/']}>
<App />
</MemoryRouter>
)
expect(wrapper.find('Redirect')).toHaveLength(1)
})
显然,测试失败,因为 Redirect 组件作为 render 到 Route 的函数在子组件内
在测试中,我将应用程序包装在内存路由器中,但在应用程序组件中,浏览器路由器已经存在,因此我可能需要对其进行重构。
但即使在 Routes 组件中拆分路由,我也不知道如何在 render 属性中进行测试。
【问题讨论】:
标签: javascript reactjs react-router jestjs enzyme