javascript - 如何为调用 reduxjs 的 mapStateToProps 的 React 组件编写单元测试?

标签 javascript reactjs redux jestjs

我正在尝试为名为 AsyncApp 的容器组件编写单元测试但我收到以下错误消息“mapStateToProps 必须返回一个对象。而是收到未定义的。”

这是我的设置。

Root.js

import configureStore from '../configureStore';
import React, { Component } from 'react';
import { Provider } from 'react-redux';
import AsyncApp from './AsyncApp';

const store = configureStore();

export default class Root extends Component {
  render() {
    return (
      <Provider store={store}>
        <AsyncApp />
      </Provider>
    );
  }
}

configureStore.js

import { createStore, applyMiddleware } from 'redux';
import thunkMiddleware from 'redux-thunk';
import createLogger from 'redux-logger';
import rootReducer from './reducers';

const loggerMiddleware = createLogger();

const createStoreWithMiddleware = applyMiddleware(
  thunkMiddleware
  //loggerMiddleware
)(createStore);

export default function configureStore(initialState) {
  return createStoreWithMiddleware(rootReducer, initialState);
}

AsyncApp.js

import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import { foo } from '../actions';
import FooComponent from '../components/FooComponent';

class AsyncApp extends Component {
  constructor(props) {
    super(props);
    this.onFoo= this.onFoo.bind(this);
    this.state = {}; // <--- adding this doesn't fix the issue
  }

  onFoo(count) {
    this.props.dispatch(foo(count));
  }

  render () {
    const {total} = this.props;

    return (
      <div>
        <FooComponent onFoo={this.onFoo} total={total}/>
      </div>
    );
  }
}

function mapStateToProps(state) {
  return state;
}

export default connect(mapStateToProps)(AsyncApp);

我路过 store直接到AsyncApp在我的测试中避免出现以下运行时错误:Could not find "store" in either the context or props of "Connect(AsyncApp)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(AsyncApp)".

测试尚未完成,因为我无法通过 mapStateToProps错误信息。

AsyncApp-test.js

jest.dontMock('../../containers/AsyncApp');
jest.dontMock('redux');
jest.dontMock('react-redux');
jest.dontMock('redux-thunk');
jest.dontMock('../../configureStore');

import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
const configureStore = require( '../../configureStore');
const AsyncApp = require('../../containers/AsyncApp');

const store = configureStore();

//const asyncApp = TestUtils.renderIntoDocument(
  //<AsyncApp store={store} />
//);

const shallowRenderer = TestUtils.createRenderer();
shallowRenderer.render(<AsyncApp store={store}/>);

我想最终测试 AsyncApp包含 FooComponent , 那一个 fooonFoo 时发送操作叫做。

我想做的事情可以实现吗?我这样做的方式正确吗?

最佳答案

我在一些地方看到的建议是测试未连接的组件,而不是连接的版本。因此,验证当您将特定 Prop 传递给您的组件时,您是否获得了预期的渲染输出,并验证当您传入具有特定形状的状态时,您的 mapStateToProps() 返回了预期的部分。然后您可以预期它们放在一起时应该都能正常工作。

关于javascript - 如何为调用 reduxjs 的 mapStateToProps 的 React 组件编写单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34611727/

相关文章:

javascript - 数据格式从单个对象到键值对对象数组的转换

SVG 使用标签和 ReactJS

javascript - redux 状态值返回未定义

javascript - 如何让 Commando 机器人响应消息中任意位置包含特定子字符串的消息

javascript - Ioniq 5 模态路线

javascript - 将 Flux 存储中的 Immutable.js 映射与内部 React 组件状态合并

reactjs - 如何使用react-router进行页面滑动(更改)

javascript - JS normalizr 如何向实体添加不相关的键

react-native - 将 socket.io 与 redux reducer 相结合

javascript - 如何神奇地获得对象键?