我有一个像这样的ReactComponent组件
export class ParentComponent {
render(){
<ChildComponent {...props} />
}
}
export default withApollo(ParentComponent)
在子组件中
class ChildComponent {
render(){
<ChildComponent {...props} />
}
}
export default withApollo(ChildComponent)
正如您所看到的,该组件正在使用withApollo。 当我在浏览器中运行此设置时,我没有收到任何错误。
TestCase:
const parentComponent = mount(
<ParentComponent
/>
);
expect(parentComponent).toHaveLength(1);
但是当我在Jest中测试ParentComponent时,我得到了:
不变违规:无法找到&#34;客户&#34;在&#34; withApollo(ChildComponent)&#34;的上下文中。将根组件包装在ApolloProvider标记中。
以同样的方式,如果我只在加载后渲染ChildComponent,那么测试用例传递的父组件
export class ParentComponent {
render(){
{!this.props.data.loading && <ChildComponent {...props} />
}
}
export default withApollo(ParentComponent)