我正在尝试使用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)
即使我把组件中的props传递给它…(参见beforeEach调用)
有什么想法吗?
要测试redux-form,您需要有一个redux存储和一个提供程序。
试试这样:
searchBar = TestUtils.renderIntoDocument(<Provider store={store}><SearchBar /></Provider>);