我正在尝试使用 enzyme 测试自定义Material-ui React组件,但出现以下错误:
ERROR: 'Warning: Failed context type: Required context 'muiTheme' was not specified in 'ChildComponent'.
我尝试过的是根据this设置上下文。我要接触和测试的组件是子组件。

const root = shallow(<RootComponent />, {context: {muiTheme}, childContextTypes: {muiTheme: React.PropTypes.object}})
const child = root.find(ChildComponent)
child.render() // <--- this line is throwing the error

更新:this is related

最佳答案

我不确定这是否是解决方案,但离目标还差一步。

const root = mount(<RootComponent />, {
  context: {muiTheme},
  childContextTypes: {muiTheme: React.PropTypes.object}
})
const child = root.find(ChildComponent)

注意,我使用mount而不是shallow。问题是我无法再使用child.find({prop: 'value'})-返回0个项目...

10-04 22:40
查看更多