javascript – 渲染子组件时无法设置上下文
作者:互联网
我正在尝试使用Enzyme测试自定义Material-ui React组件,但收到以下错误:
错误:’警告:失败的上下文类型:’ChildComponent’中未指定必需的上下文’muiTheme’.
我尝试过根据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
解决方法:
我不确定这是解决方案,但它离目标更近了一步.
const root = mount(<RootComponent />, {
context: {muiTheme},
childContextTypes: {muiTheme: React.PropTypes.object}
})
const child = root.find(ChildComponent)
请注意,我使用mount而不是shallow.问题是这个我不能再使用child.find({prop:’value’}) – 返回0项……
标签:javascript,unit-testing,reactjs,enzyme 来源: https://codeday.me/bug/20190627/1309683.html