0

我正在为连接到Redux的React组件编写单元测试。该组件的功能之一是它显示数据 if 。我试图通过设置 props 来在组件中重新创建此功能。但是,当我尝试这种方法时,我得到了错误:questionReducer.showquestions == truewrapper.setProps({ questionReducer: { showquestions: true } })

ReactWrapper::setProps() expects a function as its second argument

如何在我正在测试的组件中正确设置连接的 Reducer 的道具?

4

1 回答 1

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} />
于 2018-09-17T10:02:05.520 回答