用酶反应新的上下文API [英] React’s new context api with enzyme

查看:37
本文介绍了用酶反应新的上下文API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用酶,并且非常喜欢它.它可以与react 16一起使用,直到我想测试使用react的新上下文api的新项目.

I have been using enzyme and love it a lot. It works with react 16 until I wanted to test my new project that uses react’s new context api.

如果仅使用浅层渲染基本组件,并通过控制台日志记录该组件的调试,则可以看到其内容,但是当我将新的上下文api与提供者和消费者一起使用时,会得到< undefined/> 作为渲染.酶不会渲染组件,但会反应.

If I render only my basic component using shallow and console log the debug of the component I can see its content but when I use the new context api with provider and consumer, I get <undefined /> as a render out. Enzyme does not render the component but react does.

有人可以提供一些指导吗?

Can someone please provide some guidance.

谢谢.

推荐答案

此功能和其他React 16.3功能的支持将包含在今年夏天的下一个酶发布中(如所说的此处).

Support for this and other React 16.3 features are going to be included in the next enzyme release which is going to happen in midst summer, this year (as said here).

目前,这是一种解决方法:

For now, here's a workaround:

const outer = shallow(<SimpleComp />);
const Children = outer.props().children({ /* context */ });
const wrapper = shallow(Children);

要在新的Context API中使用mount(),请使用此酶补丁.

这篇关于用酶反应新的上下文API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆