简体   繁体   中英

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

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.

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.

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.

Any comments/help are greatly appreciated.

Cheers

You can use pure ReactTestUtils to get instance of your component, for example using renderIntoDocument method:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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