【发布时间】:2018-03-04 21:35:31
【问题描述】:
我是 Jest / Enzyme 和 React 的新手,我正在尝试测试容器组件。
我的测试是检查一个 prop 是否与 initialState 匹配。
这是我的组件
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { loadHomeAction } from '../../actions/home';
export class Home extends Component {
componentDidMount() {
this.props.dispatch(loadHomeAction());
}
render() {
const { content, loading } = this.props.home;
return (
<div>
<div>{loading && <p>Loading...</p>}</div>
<div>{!loading && content.map(item => <p key={item.message}>{item.message}</p>)}</div>
</div>
);
}
}
const mapStateToProps = ({ home }) => ({ home });
export default connect(mapStateToProps)(Home);
这是我的测试
import React from 'react';
import { shallow, mount, configure } from 'enzyme';
import renderer from 'react-test-renderer';
import Adapter from 'enzyme-adapter-react-16';
import configureStore from 'redux-mock-store';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import App from '../../App';
import ConnectedHome, { Home } from './';
configure({ adapter: new Adapter() });
describe('HOME', () => {
it('should render the DUMB component', () => {
const { wrapper } = setup(defaultProps);
expect(wrapper.length).toEqual(1);
});
it('should display the initial loading message', () => {
const { wrapper } = setup(defaultProps);
expect(wrapper.contains('Loading...')).toEqual(true);
});
});
describe('CONNECTED HOME', () => {
it('should render the SMART component', () => {
const { container } = setup(defaultProps, initialState);
expect(container.length).toEqual(1);
});
it('should match props with initial state', () => {
const { container } = setup(defaultProps, initialState);
expect(container.prop('home')).toEqual(initialState);
});
});
const initialState = {
content: [],
loading: true,
error: null
};
const defaultProps = {
home: {
content: [],
loading: true,
error: null
},
dispatch: () => {}
};
function setup(props = {}, state = {}) {
const mockStore = configureStore();
const store = mockStore(state);
const wrapper = shallow(<Home {...props} />);
const container = shallow(<ConnectedHome store={store} />);
return { wrapper, container };
}
目前,所有测试都通过了,除了should match props with initial state - 此测试返回:
CONNECTED HOME › should match props with initial state
expect(received).toEqual(expected)
Expected value to equal:
{"content": [], "error": null, "loading": true}
Received:
undefined
Difference:
Comparing two different types of values. Expected object but received undefined.
在注销容器时,我可以看到 home 未定义。
我对这不起作用/为什么不起作用感到困惑。
我一直在关注这篇博文,试图了解测试这些连接的组件 - https://medium.com/netscape/testing-a-react-redux-app-using-jest-and-enzyme-b349324803a9
编辑
好的,所以我对测试进行了一些更改,我的设置函数现在看起来像
function setup(props = {}, state = {}) {
const mockStore = configureStore();
const store = mockStore(state);
const wrapper = shallow(<Home {...props} />);
const container = shallow(
<Provider store={store}>
<ConnectedHome />
</Provider>
);
return { wrapper, container };
}
我的测试看起来像
it('should match props with initial state', () => {
const { container } = setup({}, initialState);
expect(container.props().home).toEqual(initialState);
});
现在我从控制台得到这个
TypeError: Cannot destructure property `content` of 'undefined' or 'null'.
编辑 #2
通过添加 <ConnectedHome {...props} /> 我的测试现在有效。但是感觉就像我本质上说的是 1 = 1。
【问题讨论】:
标签: unit-testing react-redux enzyme jestjs