我正在尝试为组件创建快照测试,但是,我的商店不断出现未定义,我不知道为什么。我需要将什么传递到我的商店才能使此正确?我已经使用其他不需要 mapStateToProps 或 withRouter 的组件进行了快照测试,之前没有任何问题。
工作流表单.jsx
const mapStateToProps = (state, ownProps) => {
return {
...ownProps,
userID: state.data.userID,
userObj: state.ui.userObj
};
};
export default withRouter(
connect(mapStateToProps, null)(WorkflowForm)
);
WorkflowForm.test.jsx
import React from 'react';
import renderer from 'react-test-renderer';
import { Provider } from 'react-redux';
import { BrowserRouter } from "react-router-dom";
import WorkflowForm from "../WorkflowForm";
import { cleanup } from '@testing-library/react';
import configureMockStore from 'redux-mock-store';
afterEach(cleanup);
const mockStore = configureMockStore();
const store = mockStore({
userID: "121",
userObj: "Name"
});
describe('Test suits for WorkflowForm', () => {
it('should match with snapshot', () => {
const tree = renderer
.create(
<Provider store={store}>
<BrowserRouter>
<WorkflowForm />
</BrowserRouter>
</Provider>
)
.toJSON();
expect(tree).toMatchSnapshot();
});
});
这是我运行测试时遇到的错误
TypeError: Cannot read property 'userID' of undefined
392 | return {
393 | ...ownProps,
> 394 | userID: state.data.userID,
| ^
395 | userObj: state.ui.userObj
396 | };
397 | };