我正在为连接到 Redux 的 React 组件编写单元测试。功能之一是组件,如果questionReducer.showquestions == true
,它将显示数据。我试图通过使用wrapper.setProps({ questionReducer: { showquestions: true } })
设置道具来在组件中重新创建此功能。但是,当我尝试这种方法时,出现错误:
ReactWrapper::setProps() expects a function as its second argument
如何在要测试的组件中正确设置连接的Reducer的道具?
答案 0 :(得分: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} />