gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:39:24 25 4
gpt4 key购买 nike

我正在尝试为名为 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/

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