react-native-test.s
Owner: Aurore Malherbes
Description
Writing tests helps the technical team to:
Architecture its code
Develop faster
Prevent bugs
Document the code
Impact
A lack of tests will put in jeopardy the 4 points listed above.
Checks
My React Native app is well tested if:
The reducers and selectors are tested. It helps to develop faster by reducing the number of manual testings. Furthermore it helps you to not forget edge cases.
The sagas, order of execution and effects on the state are tested, when the logic is not straight-forward. It prevents regressions as they hold the business logic of the app.
The props existence are tested in both containers and presentational components to ensure it's consistent. It helps to develop faster by reducing the number of manual testings.
The presentational components are tested with a snapshot. It avoids UI regression and save time when you make a change as you don't have to check all the app manually.
The services are tested. It helps to not forget edge cases.
Bad Examples
// @TODO
Good Examples
In these examples we use jest, redux-saga-test-plan and flow.
Here is the MO to write each kind of test.
Reducer (~2min)
The reducer you want to test is the following:
And the tests you will write are these ones:
KEY POINT: The first test is important, it allows you to check that only the 'SET_USER_INFO' action has an action on the user part of the state.
Selector (~2min)
Here is the selector you want to test:
And the corresponding test:
Sagas ( ~5 -> ~15 min)
Here is a saga you want to test. It makes an API call to get the user favorites books by type:
First, let's test the order of execution. NB: You're not supposed to test the order of execution of all your sagas, but only the ones with complex logic (loop, conditions, ...).
Nevertheless, for a learning purpose, we write the test for getFavoriteBooksByTypeSaga:
KEY POINT: The test ensure that your saga has no side-effect.
A more interesting test to do is to test the saga effect on the state:
The props presence of a presentational component and a container (~ 5min)
Here are the presentational component and the corresponding container we want to test:
KEY POINT: Use
flowto write this kind of test.
The UI of a component (~5 min)
Let's say we want to take a snapshot of the BookView component of the previous part:
KEY POINT: Test the component with several sets of props. For instance if book is allowed to not have an author, make a snapshot with and one without the author name.
If a child of this component is connected, you need to mock the store in your test:
The services (~ 5min)
Let's say we want to test a service which formats an ISEN book code. The service is not given here, as the tests are the better explanation of what the service is supposed to do!
KEY POINT: You should be exhaustive in the cases.
Last updated