MemoryRouter TypeError:无法读取属性' pathname'未定义的

时间:2017-05-18 05:36:36

标签: javascript reactjs react-router react-redux enzyme

我有一个redux容器,我想在我的测试用例中安装。它有一个使用react-router链接的组件。安装容器给我带来了历史未定义的错误,在阅读了这个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;

1 个答案:

答案 0 :(得分:6)

此错误是因为您的<Link to="undefined" />,请确保to值有效且未定义/为空。