단위 테스트 (DOM 테스트)를 작성하려고하는 스마트 구성 요소가 있습니다. 다음 오류가 발생합니다. 테스트에서 소품을 전달하고 있는데 왜이 오류가 발생하는지 잘 모르겠습니다. ..?연결된 구성 요소에 대한 js 단위 테스트 반응
Invariant Violation: Could not find "store" in either the context or props of "Connect(myComponent)". Either wrap the root component in a , or explicitly pass "store" as a prop to "Connect(myComponent)".
업데이트 새로운 오류 : 형식 오류 : mapStateToProps에서 정의되지 않은 재산 'mainData'을 읽을 수 없습니다
테스트 코드 :이를 myComponent 바보 같은 구성 요소의 자식
import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
import { Provider } from 'react-redux';
import configureMockStore from 'redux-mock-store';
import { renderComponent, expect } from '../../test_helper';
import myComponent from '../../../src/containers/myComponent';
describe('myComponent',() => {
const mockStore = configureMockStore();
let connectedApp,
store,
initialItems;
let component;
let componentRender;
beforeEach(() => {
const DataMock = [1, 2, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0,
0, 1, 2, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 1,
2, 0, 0, 0, 0, 0];
const props = {
mainData: DataMock,
abc: 'def',
subData: {
test: '1',
testing: 12,
},
userID: '1',
Date: 'jan11',
};
initialItems = ['one'];
const initialState = {
items: initialItems
};
store = mockStore(initialState);
component = TestUtils.renderIntoDocument(
<Provider store={store}><myComponent {...props} /></Provider>);
componentRender = ReactDOM.findDOMNode(component);
});
it('loads',() => {
expect(component).to.exist;
});
});
myComponent code :
import React, { PropTypes, Component } from 'react';
import { connect } from 'react-redux';
import * as actions from '../../actions/component_actions';
class myComponent extends Component {
constructor(props) {
super(props);
this.state = {
someString: 'abc',
someotherstring: 'def',
};
}
componentDidMount() {
const { test1, test2, test3 } = this.props;
this.props.fetchEntriesDefault(test1, test2, test3);
this.props.fetchAnalyticsMainView(test1, test2, test3);
}
render() {
return (
<div className="container">
</div>
);
}
}
function mapStateToProps(state) {
return {
mainData: state.reducerName.mainData,
subDataData: state.reducerName.subDataData,
};
}
myComponent.propTypes = {
mainData: PropTypes.array,
abc: PropTypes.string,
subDataData: PropTypes.object,
userID: PropTypes.string,
actioncreatorfuncone: PropTypes.func,
actioncreatorfunctwo: PropTypes.func,
date: PropTypes.string,
};
export default connect(mapStateToProps, actions)(myComponent);
내 코드를 업데이트 해 주셨지만 새로운 오류가 발생했습니다. 최신 코드로 질문을 업데이트했습니다. TypeError : 정의되지 않은 'mainData'속성을 읽을 수 없습니다. mainData는 내 구성 요소에서 사용하고있는 단어입니다. - 소품을 지나가는거야? @ http://stackoverflow.com/users/308565/nagaraj-tantri – monkeyjs
응답을 업데이트했습니다. 왜냐하면 redux 상태에서'mainData'가 필요하기 때문에'props'로 전달할 수 없습니다.'mockStore'에서 초기화해야합니다 다음과 같이하십시오 :'const initialState = { mainData : DataMock, userID : '1' }''그리고 나서 store = mockStore (initialState); ' –
시도했는데 오류가 발생했습니다 - 방금 myComponent - mainData의 코드를 업데이트했습니다. : state.reducerName.mainData, 여기 감속기 이름과 관련된 문제입니까?오류가 구성 요소에서 정의되지 않은 mainData를 말하기 때문에 어떻게 테스트 케이스에서 처리 할 수 있습니까? http://stackoverflow.com/users/308565/nagaraj-tantri – monkeyjs