ReactWrapper::setProps() error when attempting to set Redux props in component for Jest/Enzyme test
ReactWrapper::setProps() error when attempting to set Redux props in component for Jest/Enzyme test
I'm writing a unit test for a React component that is connected to Redux. One of the functions is the component is that it displays data if questionReducer.showquestions == true
. I have attempted to re-create this functionality in the component by setting props with wrapper.setProps( questionReducer: showquestions: true )
. However, when I attempt this approach, I get the error:
questionReducer.showquestions == true
wrapper.setProps( questionReducer: showquestions: true )
ReactWrapper::setProps() expects a function as its second argument
How can I properly set the props for the connected Reducer in the component I am testing?
1 Answer
1
You should test the component alone, without being connected to Redux. That allows you to give props directly to component.
Example:
export class Component_ extends React.Component
// your component logic here
const mapStateToProps =
showQuestions: questionReducer.showquestions
const Component = connect(mapStateToProps)(Component_)
export default Component
And then in test you can just do this
const wrapper = shallow(<Component_ showQuestions=true />
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy