我有这个简化的render()
方法:
render() {
return (
<React.Fragment>
<Router>
<div>
<Route path='/abc' component={Test} />
<Route path='/def' render={() => (
<div>Hello</div>
)} />
</div>
</Router>
</React.Fragment>
)
}
这个酶测试:
test('Render <Test> on /abc', () => {
const wrapper = mount(
<MemoryRouter initialEntries={['/abc']} >
<App />
</MemoryRouter>
);
console.log(wrapper.debug());
expect(wrapper.find(Test)).to.have.lengthOf(1);
});
但是,mount()
不在Test
路线上渲染组件/abc
,也没有在<div>Hello</div>
路线上渲染/def
。
console.log(wrapper.debug());
的输出:
<MemoryRouter initialEntries={{...}}>
<Router history={{...}}>
<App>
<BrowserRouter>
<Router history={{...}}>
<div>
<Route path="/abc" component={[Function: Test]} />
<Route path="/def" render={[Function: render]} />
</div>
</Router>
</BrowserRouter>
</App>
</Router>
</MemoryRouter>
我如何测试该应用程序实际上将在Test
路线上呈现/abc
组件?
答案 0 :(得分:0)
我设法找到了一个模拟路由器的解决方案:
import React from 'react';
const reactRouterDom = require('react-router-dom');
// Render plain div with children
reactRouterDom.BrowserRouter = ({children}) => <div>{children}</div>
module.exports = reactRouterDom;