gpt4 book ai didi

javascript - 类型错误 : Cannot read property 'pathname' of undefined in react/redux testing

转载 作者:行者123 更新时间:2023-11-29 23:54:34 24 4
gpt4 key购买 nike

我正在测试一些 React 组件,这是一个基本的测试套件,只是为了了解码件及其子组件是否正在渲染。

我正在使用 redux-mock-store 来制作商店,并使用 {mount} enzyme 将容器安装在提供程序中,但即使是模拟正确的商店总是会触发错误:

TypeError: Cannot read property 'pathname' of undefined

这是我非常致命的基本测试:

import React from 'react';
import { mount } from 'enzyme';
import configureStore from 'redux-mock-store';
import { Provider } from 'react-redux';
import App from '../containers/App.container';

describe('App', () => {
let wrapper;
const mockStore = configureStore([]);
const store = mockStore({
router: {
location: { pathname: '/home', query: {}, search: '' },
params: {}
}
});
console.log(store.getState());
beforeEach(() => {
wrapper = mount(
<Provider store={store}>
<App />
</Provider>
);
});

it('Should render app and container elements', () => {
expect(wrapper.find('.app').exists()).toBeTruthy();
expect(wrapper.find('.container').exists()).toBeTruthy();
});

it('Should render the navbar', () => {
expect(wrapper.find('nav').exists()).toBeTruthy();
});
});

以及(甚至更)简单的组件/容器:

import React, { Component } from 'react';
import NavBar from '../components/Navbar';

class App extends Component {

render() {
const { location, logout} = this.props;
console.log(location);
return (
<section className='app'>
<NavBar location={location.pathname} onLogoutClick={logout}/>
<div className='container'>
{this.props.children}
</div>
</section>
);
}
}

export default App;

容器:

import { connect } from 'react-redux';
import { signOut } from '../actions/auth.actions'
import App from '../components/App';

const mapStateToProps = (state, ownProps) => {
return {
location: ownProps.location
}
}

const mapDispatchToProps = (dispatch, ownProps) => {
return {
logout: () => {
dispatch(signOut())
}
}
};

export default connect(mapStateToProps, mapDispatchToProps)(App);

我测试不出问题,mockStore格式正确:

enter image description here

有什么想法吗?


更新:

考虑到这一点,我在 rootReducer 中没有 reducer/prop 用于位置,但是,我只想通过子组件传递 react-redux-router 制作的位置对象属性在 ownProps 参数中可用。

奇怪的事实:在应用程序中记录 location 属性会返回正确的对象。

在测试中,始终未定义...(如错误所示)。

enter image description here

这是我的 rootReducer:

import { combineReducers } from 'redux';
import { reducer as formReducer } from 'redux-form';
import { routerReducer } from 'react-router-redux';

import authReducer from './auth.reducer';
import analysisReportsReducer from './AnalysisReports.reducer';
import titleAnalysisReducer from './TitleAnalysis.reducer';
import postsReportsReducer from './PostsReports.reducer';

const rootReducer = combineReducers({
form: formReducer,
routing: routerReducer,
auth: authReducer,
analysis: titleAnalysisReducer,
analysis_reports: analysisReportsReducer,
posts: postsReportsReducer
});

export default rootReducer;

最佳答案

看起来您的位置对象在路由器下方。

您的测试可能正在获取 window.location 属性,您的测试套件可能不会复制该属性,假设测试是 cli 而不是在浏览器中。

也许试试:

<NavBar location={this.props.router.location.pathname} onLogoutClick={logout}/>

关于javascript - 类型错误 : Cannot read property 'pathname' of undefined in react/redux testing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41990907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com