【问题标题】:ReactWrapper::setProps() error when attempting to set Redux props in component for Jest/Enzyme test尝试在组件中为 Jest/Enzyme 测试设置 Redux 道具时出现 ReactWrapper::setProps() 错误
【发布时间】:2019-02-20 22:56:31
【问题描述】:
我正在为连接到 Redux 的 React 组件编写单元测试。该组件的功能之一是在questionReducer.showquestions == true 时显示数据。我试图通过使用wrapper.setProps({ questionReducer: { showquestions: true } }) 设置道具来在组件中重新创建此功能。但是,当我尝试这种方法时,我得到了错误:
ReactWrapper::setProps() expects a function as its second argument
如何在我正在测试的组件中正确设置连接的 Reducer 的 props?
【问题讨论】:
标签:
javascript
reactjs
react-redux
jestjs
enzyme
【解决方案1】:
你应该单独测试组件,而不是连接到 Redux。这使您可以直接将道具提供给组件。
例子:
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} />