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

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)