我刚刚将Redux-Form从5.3.2升级到6.0.1,但在5.3.2中运行的单元测试在6.0.1中失败。
/* MyForm.jsx */
...
import { Field, reduxForm } from 'redux-form';
class MyForm extends Component {
...
<form onSubmit={handleSubmit(...)}>
...
}
export default reduxForm({
form: 'myForm'
})(MyForm);
我在渲染表单之前安装了表单缩减器:
import {reducer as formReducer} from 'redux-form';
const myReducer = combineReducers({
...
form: formReducer
});
这是在顶级组件中创建的商店:
const createStoreWithMiddleware = applyMiddleware(thunkMiddleware)(createStore);
const store = createStoreWithMiddleware(myReducer);
我的测试用例(Karma + jasmine),在5.3.2中有效,但在6.0.1中失败
/* form.test.js */
import React, { PropTypes } from 'react';
import TestUtils from 'react/lib/ReactTestUtils';
import findDOMNode from 'react/lib/findDOMNode';
import { Provider } from 'react-redux';
...
import MyForm from '../MyForm';
const createStoreWithMiddleware = applyMiddleware(thunkMiddleware)(createStore);
const store = createStoreWithMiddleware(myReducer);
describe('MyForm', () => {
beforeAll(function() {
this.props = {
...
store: store
}
});
it('should render', function() {
const element = TestUtils.renderIntoDocument(
<MyForm {...this.props} />
);
expect(element).toBeTruthy();
});
error: Invariant Violation: Could not find "store" in either the context or props of "Connect(ConnectedField)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(ConnectedField)".
如果我使用Provider传入商店,则会收到另一个错误:
it('should render', function() {
const element = TestUtils.renderIntoDocument(
<Provider store={ store }>
{ () => <MyForm {...this.props} />}
</Provider>
);
expect(element).toBeTruthy();
});
*
error: Invariant Violation: onlyChild must be passed a children with exactly one child.
ERROR: 'Warning: Failed propType: Invalid prop `children` supplied to `Provider`, expected a single ReactElement.'
*
为什么测试失败的任何想法?我在网上搜索但找不到特定于此主题的信息。
谢谢,