javascript - 尝试在组件中设置 Redux props 以进行 Jest/Enzyme 测试时,ReactWrapper::setProps() 错误

标签 javascript reactjs react-redux jestjs enzyme

我正在为连接到 ReduxReact 组件编写单元测试。该组件的功能之一是如果 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/

相关文章:

reactjs - Webpack 检查文件是否存在并导入

javascript - 使用react-materialize遇到错误 “ReferenceError: $ is not defined”

javascript - 无法传递标签属性参数?(React)

android - 如何在 React Native 中使用 ListView 获得跑马灯效果?

react-native - react native : How to remove the current stack navigator from the navigation-stack

javascript - Flux 中函数参数解构背后的原因是什么?

javascript - 高效的 jQuery 或 HTML5 图像 slider

javascript - 在 Rails 2.3.x 中获取 Rails 3 样式 UJS 的插件/Gem?

javascript - 如何从 php 中获取一个值并通过 javascript 将其放入 input-element 中?

javascript - 对象不是函数,在 jspdf 调用中