简体   繁体   English

没有酶的组件中的测试方法-React Native

[英]Test method in component without Enzyme - React Native

I'm pretty new to React Native and Javascript, I'm currently trying to test methods inside my components, I've seen this being done with Enzyme like 我对React Native和Java脚本还很陌生,我目前正在尝试测试组件内部的方法,我已经看到这是通过像酶这样的方式完成的

const wrapper = shallow(<Component/>);
wrapper.instance().methodIwannaCall();

Coming from the iOS Dev world, I'm having a hard to understanding why it seems to be so complicated to get an instance of a class and call a method on it. 来自iOS Dev世界,我很难理解为什么获取类的实例并在其上调用方法似乎如此复杂。

Unfortunately I'm using React 16.0.0-alpha.12 which means I can't for now use Enzyme , which seems to be the library everyone is using to accomplish what I need. 不幸的是我正在使用React 16.0.0-alpha.12 ,这意味着我现在不能使用Enzyme ,这似乎是每个人都用来完成我所需要的库。

Also notice I'm not using Redux, I suspect this would be less of a pain if I'd use Redux, since that way all my business logic would be within actions and hence would be easier to test. 还要注意,我没有使用Redux,如果使用Redux,我怀疑这样做不会那么麻烦,因为那样一来,我所有的业务逻辑都将在行动之内,因此更易于测试。

Any comments/help are greatly appreciated. 任何意见/帮助,不胜感激。

Cheers 干杯

You can use pure ReactTestUtils to get instance of your component, for example using renderIntoDocument method: 您可以使用纯ReactTestUtils来获取组件的实例,例如,使用renderIntoDocument方法:

import ReactTestUtils from 'react-dom/test-utils';

const wrapper = ReactTestUtils.renderIntoDocument(<Component/>);
wrapper.methodIwannaCall();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM