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

Min*_*aze 2 javascript reactjs react-router enzyme react-redux

我有一个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)
Run Code Online (Sandbox Code Playgroud)

我的测试用例:

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');
});
Run Code Online (Sandbox Code Playgroud)

我的组件:

class Home extends Component {
    componentWillMount() {
        this.props.init();
    }

    componentDidMount() {
        document.title = 'Home';
    }

    render() {
        return (
            <div>
                <Popup />
            </div>
        );
    }
}
export default Home;
Run Code Online (Sandbox Code Playgroud)

我的容器:

const mapStateToProps = state => ({
    data: state.mainReducer.data
});
const HomeContainer = connect(mapStateToProps, { init })(Home);
export default HomeContainer;
Run Code Online (Sandbox Code Playgroud)

Nir*_*han 6

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