Skip to content

docs: add example of testing React Navigation #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ node_modules
build
.idea
.DS_Store

# Ignore lock files in examples for now
examples/**/yarn.lock
215 changes: 215 additions & 0 deletions docs/ReactNavigation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
---
id: react-navigation
title: React Navigation
---

This section deals with integrating `react-native-testing-library` with `react-navigation`, using Jest.

## Setting up

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.

```
$ yarn add @react-native-community/masked-view @react-navigation/native @react-navigation/stack react-native-gesture-handler react-native-reanimated react-native-safe-area-context react-native-screens
```

Create an [`./AppNavigator.js`](https://github.com/callstack/react-native-testing-library/blob/master/examples/reactnavigation/src/AppNavigator.js) component which will list the navigation stack:

```jsx
import 'react-native-gesture-handler';
import React from 'react';
import { createStackNavigator } from '@react-navigation/stack';

import HomeScreen from './screens/HomeScreen';
import DetailsScreen from './screens/DetailsScreen';

const { Screen, Navigator } = createStackNavigator();

export default function Navigation() {
const options = {};

return (
<Navigator>
<Screen name="Home" component={HomeScreen} />
<Screen options={options} name="Details" component={DetailsScreen} />
</Navigator>
);
}
```

Create your two screens which we will transition to and from them. The homescreen, found in [`./screens/HomeScreen.js`](https://github.com/callstack/react-native-testing-library/blob/master/examples/reactnavigation/src/screens/HomeScreen.js), contains a list of elements presented in a list view. On tap of any of these items will move to the details screen with the item number:

```jsx
import React from 'react';
import {
Text,
View,
FlatList,
TouchableOpacity,
StyleSheet,
} from 'react-native';

export default function HomeScreen({ navigation }) {
const [items] = React.useState(
new Array(20).fill(null).map((_, idx) => idx + 1)
);

const onOpacityPress = item => navigation.navigate('Details', item);

return (
<View>
<Text style={styles.header}>List of numbers from 1 to 20</Text>
<FlatList
keyExtractor={(_, idx) => `${idx}`}
data={items}
renderItem={({ item }) => (
<TouchableOpacity
onPress={() => onOpacityPress(item)}
style={styles.row}
>
<Text>Item number {item}</Text>
</TouchableOpacity>
)}
/>
</View>
);
}

const divider = '#DDDDDD';

const styles = StyleSheet.create({
header: {
fontSize: 20,
textAlign: 'center',
marginVertical: 16,
},
row: {
paddingVertical: 16,
paddingHorizontal: 24,
borderBottomColor: divider,
borderBottomWidth: 1,
},
});
```

The details screen, found in [`./screens/DetailsScreen.js`](https://github.com/callstack/react-native-testing-library/blob/master/examples/reactnavigation/src/screens/DetailsScreen.js), contains a header with the item number passed from the home screen:

```jsx
// ./screens/DetailsScreen.js
import React from 'react';
import { Text, StyleSheet, View } from 'react-native';

export default function DetailsScreen(props) {
const item = Number.parseInt(props.route.params, 10);

return (
<View>
<Text style={styles.header}>Showing details for {item}</Text>
<Text style={styles.body}>the number you have chosen is {item}</Text>
</View>
);
}

const styles = StyleSheet.create({
header: {
fontSize: 20,
textAlign: 'center',
marginVertical: 16,
},
body: {
textAlign: 'center',
},
});
```

## Setting up the test environment

Install required dev dependencies:

```
$ yarn add -D jest react-native-testing-library
```

Create your `jest.config.js` file (or place the following properties in your `package.json` as a "jest" property)

```js
module.exports = {
preset: 'react-native',
setupFiles: ['./node_modules/react-native-gesture-handler/jestSetup.js'],
transformIgnorePatterns: [
'node_modules/(?!(jest-)?react-native|@react-native-community|@react-navigation)',
],
};
```

Notice the 2 entries that don't come with the default React Native project:

- `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
- `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).

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.

Let's 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:

```jsx
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { render, fireEvent, cleanup } from 'react-native-testing-library';

import AppNavigator from '../AppNavigator';

// Silence the warning https://github.com/facebook/react-native/issues/11094#issuecomment-263240420
jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');

describe('Testing react navigation', () => {
afterEach(cleanup);

test('page contains the header and 10 items', () => {
const component = (
<NavigationContainer>
<AppNavigator />
</NavigationContainer>
);

const { getByText, getAllByText } = render(component);

const header = getByText('List of numbers from 1 to 20');
const items = getAllByText(/Item number/);

expect(header).toBeTruthy();
expect(items.length).toBe(10);
});

test('clicking on one item takes you to the details screen', async () => {
const component = (
<NavigationContainer>
<AppNavigator />
</NavigationContainer>
);

const { getByText } = render(component);
const toClick = getByText('Item number 5');

fireEvent(toClick, 'press');
const newHeader = getByText('Showing details for 5');
const newBody = getByText('the number you have chosen is 5');

expect(newHeader).toBeTruthy();
expect(newBody).toBeTruthy();
});
});
```

## Running tests

To run the tests, place a test script inside your `package.json`

```json
{
"scripts": {
"test": "jest"
}
}
```

And run the `test` script with `npm test` or `yarn test`.
3 changes: 3 additions & 0 deletions examples/reactnavigation/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
};
7 changes: 7 additions & 0 deletions examples/reactnavigation/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
preset: 'react-native',
setupFiles: ['./node_modules/react-native-gesture-handler/jestSetup.js'],
transformIgnorePatterns: [
'node_modules/(?!(jest-)?react-native|@react-native-community|@react-navigation)',
],
};
30 changes: 30 additions & 0 deletions examples/reactnavigation/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "react-navigation-example",
"description": "Testing react-navigation with react-native-testing-library",
"version": "0.0.1",
"private": true,
"scripts": {
"test": "jest"
},
"dependencies": {
"@react-native-community/masked-view": "^0.1.9",
"@react-navigation/native": "^5.1.6",
"@react-navigation/stack": "^5.2.13",
"prop-types": "^15.7.2",
"react": "^16.13.1",
"react-native": "^0.62.2",
"react-native-gesture-handler": "^1.6.1",
"react-native-reanimated": "^1.8.0",
"react-native-safe-area-context": "^0.7.3",
"react-native-screens": "^2.5.0"
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/runtime": "^7.9.2",
"babel-jest": "^25.4.0",
"jest": "^25.4.0",
"metro-react-native-babel-preset": "^0.59.0",
"react-native-testing-library": "^1.13.0",
"react-test-renderer": "^16.13.1"
}
}
23 changes: 23 additions & 0 deletions examples/reactnavigation/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import { StatusBar, StyleSheet, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';

import AppNavigator from './AppNavigator';

export default function App() {
return (
<NavigationContainer>
<View style={styles.container}>
<StatusBar barStyle="dark-content" />

<AppNavigator />
</View>
</NavigationContainer>
);
}

const styles = StyleSheet.create({
container: {
flex: 1,
},
});
19 changes: 19 additions & 0 deletions examples/reactnavigation/src/AppNavigator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'react-native-gesture-handler';
import React from 'react';
import { createStackNavigator } from '@react-navigation/stack';

import HomeScreen from './screens/HomeScreen';
import DetailsScreen from './screens/DetailsScreen';

const { Screen, Navigator } = createStackNavigator();

export default function Navigation() {
const options = {};

return (
<Navigator>
<Screen name="Home" component={HomeScreen} />
<Screen options={options} name="Details" component={DetailsScreen} />
</Navigator>
);
}
46 changes: 46 additions & 0 deletions examples/reactnavigation/src/__tests__/AppNavigator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { render, fireEvent, cleanup } from 'react-native-testing-library';

import AppNavigator from '../AppNavigator';

// Silence the warning https://github.com/facebook/react-native/issues/11094#issuecomment-263240420
jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');

describe('Testing react navigation', () => {
afterEach(cleanup);

test('page contains the header and 10 items', () => {
const component = (
<NavigationContainer>
<AppNavigator />
</NavigationContainer>
);

const { getByText, getAllByText } = render(component);

const header = getByText('List of numbers from 1 to 20');
const items = getAllByText(/Item number/);

expect(header).toBeTruthy();
expect(items.length).toBe(10);
});

test('clicking on one item takes you to the details screen', async () => {
const component = (
<NavigationContainer>
<AppNavigator />
</NavigationContainer>
);

const { getByText } = render(component);
const toClick = getByText('Item number 5');

fireEvent(toClick, 'press');
const newHeader = getByText('Showing details for 5');
const newBody = getByText('the number you have chosen is 5');

expect(newHeader).toBeTruthy();
expect(newBody).toBeTruthy();
});
});
24 changes: 24 additions & 0 deletions examples/reactnavigation/src/screens/DetailsScreen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';
import { Text, StyleSheet, View } from 'react-native';

export default function DetailsScreen(props) {
const item = Number.parseInt(props.route.params, 10);

return (
<View>
<Text style={styles.header}>Showing details for {item}</Text>
<Text style={styles.body}>the number you have chosen is {item}</Text>
</View>
);
}

const styles = StyleSheet.create({
header: {
fontSize: 20,
textAlign: 'center',
marginVertical: 16,
},
body: {
textAlign: 'center',
},
});
Loading