Javascript 如何为调用reduxjs';是MapStateTops吗?

Javascript 如何为调用reduxjs';是MapStateTops吗?,javascript,reactjs,redux,jestjs,Javascript,Reactjs,Redux,Jestjs,我正在尝试为名为AsyncApp的容器组件编写单元测试,但出现以下错误“mapstatetops必须返回一个对象。而不是接收到未定义的对象。” 这是我的安排 Root.js import configureStore from '../configureStore'; import React, { Component } from 'react'; import { Provider } from 'react-redux'; import AsyncApp from './AsyncApp'

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

这是我的安排

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>
    );
  }
}
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);
}
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);
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.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>
    );
  }
}
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);
}
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);
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
,以及调用
onFoo
时是否调度
foo
操作


我想做的事情是可以实现的吗?我这样做对吗?

我在一些地方看到的建议是测试非连接组件,而不是连接版本。因此,请验证当您向组件传递特定的道具时,您是否获得了预期的渲染输出,并验证当您以特定形状传递状态时,您的
MapStateTrops()
是否返回了预期的片段。然后,您可以期望它们在组合在一起时都能正常工作。

Redux文档在中明确提出了这一点。在其他文章中也有一些相关的例子,比如和谢谢,redux文档确实很清楚这个问题。我自己应该已经看到了。你可能会在测试非连接组件的情况下结束…但是在一个相当复杂的应用程序中,非连接组件的a-子-可以连接。所以它并不总是那么简单(除非只连接了顶级组件)。除了模仿导入之外,还没有找到一个很好的解决方案。@shados你有没有尝试过类似的方法
expect(result.type).to.equal(ConnectedChildComponent)
其中
ConnectedChildComponent
是导入的、已连接的组件-请注意,您没有检查字符串或
,而是检查导出的
连接(…)(您的组件)
element.@shados您是否能够找到一种方法,使用嵌套的连接组件正确测试连接的组件?