You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/docs/ReactNavigation.md
+153-2Lines changed: 153 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,9 @@ title: React Navigation
5
5
6
6
This section deals with integrating `@testing-library/react-native` with `react-navigation`, using Jest.
7
7
8
-
## Setting up
8
+
## Stack Navigator
9
+
10
+
### Setting up
9
11
10
12
Install the packages required for React Navigation. For this example, we will use a [stack navigator](https://reactnavigation.org/docs/stack-navigator/) to transition to the second page when any of the items are clicked on.
@@ -147,6 +149,8 @@ Notice the 2 entries that don't come with the default React Native project:
147
149
-`setupFiles` – an array of files that Jest is going to execute before running your tests. In this case, we run `react-native-gesture-handler/jestSetup.js` which sets up necessary mocks for `react-native-gesture-handler` native module
148
150
-`transformIgnorePatterns` – an array of paths that Jest ignores when transforming code. In this case, the negative lookahead regular expression is used, to tell Jest to transform (with Babel) every package inside `node_modules/` that starts with `react-native`, `@react-native-community` or `@react-navigation` (added by us, the rest is in `react-native` preset by default, so you don't have to worry about it).
149
151
152
+
### Example tests
153
+
150
154
For this example, we are going to test out two things. The first thing is that the page is laid out as expected. The second, and most important, is that the page will transition to the detail screen when any item is tapped on.
151
155
152
156
Let's add a [`AppNavigator.test.js`](https://github.com/callstack/react-native-testing-library/blob/master/examples/reactnavigation/src/__tests__/AppNavigator.js) file in `src/__tests__` directory:
Testing the Drawer Navigation requires an additional setup step for mocking the Reanimated library.
208
+
209
+
### Setting up
210
+
211
+
Install the packages required for React Navigation. For this example, we will use a [drawer navigator](https://reactnavigation.org/docs/drawer-navigator/) to transition between a home screen and an additional screen.
Create a [`./DrawerAppNavigator.js`](https://github.com/callstack/react-native-testing-library/blob/master/examples/reactnavigation/src/DrawerAppNavigator.js) component which will list the navigation stack:
<Button onPress={() =>navigation.goBack()} title="Go back home"/>
256
+
</View>
257
+
);
258
+
}
259
+
```
260
+
261
+
### Setting up the test environment
262
+
263
+
Install required dev dependencies:
264
+
265
+
```
266
+
$ yarn add -D jest @testing-library/react-native
267
+
```
268
+
269
+
Create a [`mock file`](https://github.com/callstack/react-native-testing-library/blob/master/examples/reactnavigation/jest-mocks.js) necessary for your tests:
Make sure that the path to the file in `setupFiles` is correct. Jest will run these files before running your tests, so it's the best place to put your global mocks.
301
+
302
+
This setup is copied from the [React Navigation documentation](https://reactnavigation.org/docs/testing/).
303
+
304
+
### Example tests
305
+
306
+
For this example, we are going to test out two things. The first thing is that the screen is loaded correctly. The second, and most important, is that the page will transition to the notifications screen when the button is tapped on.
307
+
308
+
Let's add a [`DrawerAppNavigator.test.js`](https://github.com/callstack/react-native-testing-library/blob/master/examples/reactnavigation/src/__tests__/DrawerAppNavigator.js) file in `src/__tests__` directory:
0 commit comments