TypeError:无法在react / redux测试中读取undefined的属性'pathname'

时间:2017-02-01 22:19:52

标签: javascript unit-testing reactjs redux

我正在测试一些反应组件,一个基本的测试套件,只是为了知道一个组件是否正在呈现它们的孩子。

我正在使用redux-mock-store来创建商店,{mount} enzyme将容器安装在提供程序中,但即使模拟正确的商店,也会始终触发此错误:

  

TypeError:无法读取未定义

的属性“pathname”

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

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-routerownProps中提供的位置对象属性}参数。

奇怪的事实:在应用程序中记录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;

1 个答案:

答案 0 :(得分:1)

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

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

也许试试:

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