아래에 내 구성 요소를 붙여 넣었습니다. Component가 마운트되면 기본적으로 API에서 메시지를 반환하는 fetchMessage
Action을 호출합니다. 이 메시지는 mapStateToProps
함수에서 state.feature.message
으로 설정됩니다.연결된 Redux 구성 요소를 테스트 할 때의 요령
나는이 구성 요소 테스트를 어디서 시작해야 할 지 전혀 모르겠습니다. 나는 것을 테스트 할 것을 알고 :
1) 기능의 구성 요소가 렌더링
props
의 fetchMessage
기능이
3
라고 2))이 표시 또는 사용하여 렌더링 할 때 올바른message
속성이 있습니다 그
아래에서 볼 수 있듯이 테스트 파일을 설정하려고했지만 모든 시도에서 오류가 발생하면 반복되는 오류가 발생합니다.
누구나 내가 옳은 방향으로 나를 잘못 지적 할 수 있습니까?
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import * as actions from './actions';
class Feature extends Component {
static propTypes = {
fetchMessage: PropTypes.func.isRequired,
message: PropTypes.string
}
componentWillMount() {
this.props.fetchMessage();
}
render() {
return (
<div>{this.props.message}</div>
);
}
}
function mapStateToProps(state) {
return { message: state.feature.message };
}
export default connect(mapStateToProps, actions)(Feature);
테스트 파일 :
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { Provider } from 'react-redux';
import expect from 'expect';
import { shallow, render, mount } from 'enzyme';
import React from 'react';
import sinon from 'sinon';
import Feature from '../index';
const mockStore = configureStore([thunk]);
describe('<Feature />',() => {
let store;
beforeEach(() => {
store = mockStore({
feature: {
message: 'This is the message'
}
});
});
it('renders a <Feature /> component and calls fetchMessage',() => {
const props = {
fetchMessage: sinon.spy()
};
const wrapper = mount(
<Provider store={store}>
<Feature {...props} />
</Provider>
);
expect(wrapper.find('Feature').length).toEqual(1);
expect(props.fetchMessage.calledOnce).toEqual(true);
});
});
"UnconnectedFeature"를 명시 적으로 내 보내지 않았습니까? – JoeTidee
감사합니다. 명명 된 가져 오기를 업데이트합니다. – therewillbecode