我正在为连接到 Redux 的 React 组件编写单元测试。该组件的功能之一是如果 questionReducer.showquestions == true
则显示数据。我尝试通过使用 wrapper.setProps({ QuestionReducer: { showquestions: true } })
设置 props 在组件中重新创建此功能。但是,当我尝试这种方法时,出现错误:
ReactWrapper::setProps() expects a function as its second argument
如何在我正在测试的组件中正确设置连接的Reducer的props?
最佳答案
您应该单独测试该组件,而不连接到 Redux。这允许您直接向组件提供 Prop 。
示例:
export class Component_ extends React.Component {
// your component logic here
}
const mapStateToProps = {
showQuestions: questionReducer.showquestions
}
const Component = connect(mapStateToProps)(Component_)
export default Component
然后在测试中你可以这样做
const wrapper = shallow(<Component_ showQuestions={true} />
关于javascript - 尝试在组件中设置 Redux props 以进行 Jest/Enzyme 测试时,ReactWrapper::setProps() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52360585/