如何用酶测试子成分方法? [英] How to test child component method with Enzyme?

查看:82
本文介绍了如何用酶测试子成分方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的组件:

<Parent>
  <Child/>
</Parent>

< Child /> 组件有一个方法 foo 。我想测试 foo 方法,但我不知道如何访问它。我试过了:

and <Child/> component have a method foo. I want test the foo method but I don't know how to access it. I tried:

mount(<Parent><Child/></Parent>).props().children.foo

mount(<Parent><Child/></Parent>).children().foo

但它们都是 undefined 。我不能使用 .instance()因为它不是root。我无法挂载< Child /> 只是因为< Parent> 添加了一些东西(react-router's) context 上的 context.router )当init < Child />时我需要它们。有这个想法吗?

but both them are undefined. I can't use .instance() because it's not root. I can't mount <Child/> only because the <Parent> add something (react-router's context.router) on context and I need them when init <Child/>. Any idea with this?

推荐答案

我更喜欢浅层装载而不是酶装满。

I prefer shallow mount over full mount from enzyme.

与proxyquire一起解决子组件(你要测试)
我做

In conjunction with proxyquire to resolve child component (which you want to test) I do

wrapper.find('Child1').props().propName

并测试它。

或者我使用浅

mount wrapper.dive()

这篇关于如何用酶测试子成分方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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