简体   繁体   中英

How to test React Native classes that do not have a render method?

I am building a mobile application with React Native and ran into a problem when trying to unit test my code. Currently I do have both Jest and Enzyme being used in the testing.

How would I be able to test the methods in a certain class/file that does not contain a render() method and does not "extend as a Component"? This class is used to query information from an API and saving it into variables.

The documentation about Jest and Enzyme at

https://facebook.github.io/jest/docs/api.html#content

http://airbnb.io/enzyme/docs/api/index.html

seem to be focused on testing rendered components.

Assuming you've got a module export set up, you actually just import the object/class and then write a test against it using a jasmine syntax. You can try it out live right here:

https://facebook.github.io/jest/docs/getting-started.html#content

If you notice on the "add-test.js" file, the first line is a require to get your object under test. In this case:

const add = require('./add');

Then it's just a matter of regular old jasmine-style testing.

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