我正在为连接到Redux的React组件编写单元测试。其中一个功能是组件是它显示数据,如果questionReducer.showquestions == true
。我试图通过使用wrapper.setProps({ questionReducer: { showquestions: true } })
设置道具来在组件中重新创建此功能。但是,当我尝试这种方法时,我收到错误:
ReactWrapper::setProps() expects a function as its second argument
如何在我正在测试的组件中正确设置连接的减速器的道具?
您应该单独测试组件,而无需连接到 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} />