MemoryRouter TypeError: Cannot read property 'pathname' of undefined
MemoryRouter TypeError: Cannot read property 'pathname' of undefined
我有一个 redux 容器,我想将其挂载到我的测试用例中。它有一个组件正在使用 react-router link。挂载容器抛出历史未定义错误,在阅读这篇文章后 https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/testing.md 我实现了 MemoryRouter,现在历史错误消失了,但我收到以下错误:
TypeError: Cannot read property 'pathname' of undefined
at Object.createPath (node_modules/history/PathUtils.js:47:26)
at Link.render (node_modules/react-router-dom/Link.js:76:44)
我的测试用例:
it('should load popup component', () => {
const container = mount(
<Provider store={ store }>
<MemoryRouter initialIndex='2' initialEntries={ ['/', 'home'] } >
<HomeContainer />
</MemoryRouter>
</Provider>
);
assert.equal(container.find('PopUp').length, 1, 'Popup component is not mounted');
});
我的组件:
class Home extends Component {
componentWillMount() {
this.props.init();
}
componentDidMount() {
document.title = 'Home';
}
render() {
return (
<div>
<Popup />
</div>
);
}
}
export default Home;
我的容器:
const mapStateToProps = state => ({
data: state.mainReducer.data
});
const HomeContainer = connect(mapStateToProps, { init })(Home);
export default HomeContainer;
此错误是因为您的 <Link to="undefined" />
,请确保 to
值有效而不是 undefined/empty。
我有一个 redux 容器,我想将其挂载到我的测试用例中。它有一个组件正在使用 react-router link。挂载容器抛出历史未定义错误,在阅读这篇文章后 https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/testing.md 我实现了 MemoryRouter,现在历史错误消失了,但我收到以下错误:
TypeError: Cannot read property 'pathname' of undefined
at Object.createPath (node_modules/history/PathUtils.js:47:26)
at Link.render (node_modules/react-router-dom/Link.js:76:44)
我的测试用例:
it('should load popup component', () => {
const container = mount(
<Provider store={ store }>
<MemoryRouter initialIndex='2' initialEntries={ ['/', 'home'] } >
<HomeContainer />
</MemoryRouter>
</Provider>
);
assert.equal(container.find('PopUp').length, 1, 'Popup component is not mounted');
});
我的组件:
class Home extends Component {
componentWillMount() {
this.props.init();
}
componentDidMount() {
document.title = 'Home';
}
render() {
return (
<div>
<Popup />
</div>
);
}
}
export default Home;
我的容器:
const mapStateToProps = state => ({
data: state.mainReducer.data
});
const HomeContainer = connect(mapStateToProps, { init })(Home);
export default HomeContainer;
此错误是因为您的 <Link to="undefined" />
,请确保 to
值有效而不是 undefined/empty。