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

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

问题描述

我有一个这样的组件:

<Parent>
  <Child/>
</Parent>

组件有一个方法 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.我不能挂载 只是因为 在上面添加了一些东西(react-router 的 context.router)context 并且我在 init 时需要它们.对此有什么想法吗?

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?

推荐答案

这对我有用:

mount(<Parent><Child/></Parent>).find(Child).instance().foo

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

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