我正在尝试使用ReduxForm进行一些单元测试,我显然遇到了像这样的最简单的问题。
实际上,我有一个搜索栏,当我修改它的输入时,我需要状态来反映这些数据。
我写过这个测试:
beforeEach(() => {
const store = configureStore()({
searchBar:{
inputField:''
}
});
searchBar = TestUtils.renderIntoDocument(<SearchBar store={store}/>);
});
it('should have a form Field tag', () => {
const input = TestUtils.findRenderedComponentWithType(searchBar, Field);
expect(input).toBeTruthy();
});
那应该测试一下:
import React from 'react';
import { Field, reduxForm } from 'redux-form';
/**
* The seach bar component class definition
*/
class SearchBar extends React.Component {
/**
* Render the searchBar component
*/
render() {
return (
<form>
<Field name="inputField" component="input" type="text"/>
<button type="submit">Rechercher</button>
</form>
);
}
}
/**
* Decorate the form
*/
SearchBar = reduxForm({
form: 'searchBar'
})(SearchBar);
export default SearchBar;
但是我发出以下错误:
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)". in src/index.spec.js (line 3551)
即使我传递了我的组件中的道具......(参见beforeEach调用)
有什么想法吗?
答案 0 :(得分:3)
要测试redux-form,您需要有一个redux商店和一个提供商。
尝试这样的事情:
searchBar = TestUtils.renderIntoDocument(<Provider store={store}><SearchBar /></Provider>);