【发布时间】:2018-07-12 13:22:34
【问题描述】:
我正在使用酶和代码进行测试。我正在尝试测试由 react-router-dom 生成的路由。只要路线是未装饰的“未连接”路线,我就没有任何问题。但是如果路由是“连接的”redux 路由,测试就会失败。首先是我的 Routes 组件。
请注意,<PrivateRoute /> 只是一个 HOC,它在发送路由之前首先检查用户是否被授权。为了我要测试的内容,您可以将它们视为来自 react-router-dom 的任何其他 <Route />。
const Routes = () => {
return (
<Switch>
<Route exact path='/' component={WelcomeScreen} />
<Route path='/auth' component={Authorize} />
<PrivateRoute path='/text_editor' component={TextEditor} />
<PrivateRoute path='/files' component={FileSearch} />
<Route component={SplashPage} />
</Switch>
)
}
这是我的“未装饰”路线之一的测试。 此测试通过,没有任何问题
it('should direct to the home page', () => {
const wrapper = mount(
<MemoryRouter initialEntries={['/']}>
<Routes />
</MemoryRouter>
)
expect(wrapper.find('WelcomeScreen')).to.have.length(1);
})
但是,当我在连接的路由上运行相同的测试时......
it('should direct to the authorized page', () => {
const wrapper = mount(
<MemoryRouter initialEntries={['/auth']}>
<Routes />
</MemoryRouter>
)
expect(wrapper.find('Connect(Authorized')).to.have.length(1);
})
测试因错误而崩溃:
不变违规:在上下文中找不到“商店”或 “连接(授权)”的道具。要么将根组件包装在 , 或明确地将“商店”作为道具传递给 “连接(授权)”。
所以我使用 redux-mock-store 重构了测试以包含一个 mockStore...
it('should direct to the authorize page', () => {
const mockStore = configureStore()
const store = mockStore({ state: '' })
const component = mount(
<Provider store={store}>
<MemoryRouter initialEntries={['/auth']}>
<Routes />
</MemoryRouter>
</Provider>
)
expect(component.find('Connect(Authorize)')).to.have.length(1);
})
但现在我的错误信息是:
TypeError:无法将对象转换为原始值
我现在不知道如何将商店传递给连接的组件以测试它的存在?有人对测试连接路由的最佳方法有任何想法吗?
【问题讨论】:
标签: unit-testing redux react-router