ReduxForm 需要存储在测试中作为道具传递
ReduxForm needs store to be pass as props on tests
我正在尝试使用 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 调用)
有什么想法吗?
为了测试 redux-form,你需要有一个 redux store 和一个 provider。
尝试这样的事情:
searchBar = TestUtils.renderIntoDocument(<Provider store={store}><SearchBar /></Provider>);
我正在尝试使用 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 调用)
有什么想法吗?
为了测试 redux-form,你需要有一个 redux store 和一个 provider。
尝试这样的事情:
searchBar = TestUtils.renderIntoDocument(<Provider store={store}><SearchBar /></Provider>);