forked from testing-library/native-testing-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact-navigation.js
78 lines (66 loc) · 2.19 KB
/
react-navigation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import '@testing-library/jest-native/extend-expect';
import React from 'react';
import { Button, Text, View } from 'react-native';
import { createStackNavigator } from 'react-navigation-stack';
import { createAppContainer, withNavigation } from 'react-navigation';
import { render, fireEvent, cleanup } from '../../src';
jest
.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper')
.mock('react-native-gesture-handler', () => {
const View = require('react-native').View;
return {
State: {},
PanGestureHandler: View,
BaseButton: View,
Directions: {},
};
});
const Home = ({ navigation }) => (
<View>
<Text testID="title">Home page</Text>
<Button title="Go to about" onPress={() => navigation.navigate('About')} />
</View>
);
const About = ({ navigation }) => (
<View>
<Text testID="title">About page</Text>
<Button title="Go to home" onPress={() => navigation.navigate('Home')} />
</View>
);
const Location = () => (
<View>
<Text testID="title">Location page</Text>
<LocationDisplay />
</View>
);
const LocationDisplay = withNavigation(({ navigation }) => (
<Text testID="location-display">{navigation.state.routeName}</Text>
));
function renderWithNavigation({ screens = {}, navigatorConfig = {} } = {}) {
const AppNavigator = createStackNavigator(
{
Home,
About,
Location,
...screens,
},
{ initialRouteName: 'Home', ...navigatorConfig },
);
const App = createAppContainer(AppNavigator);
return { ...render(<App detached />), navigationTestRenderer: App };
}
afterEach(cleanup);
test('full app rendering/navigating', async () => {
const { findByText, getByTestId, getByTitle } = renderWithNavigation();
expect(getByTestId('title')).toHaveTextContent('Home page');
fireEvent.press(getByTitle(/Go to about/i));
const result = await findByText('About page');
expect(result).toHaveTextContent('About page');
});
test('rendering a component that uses withNavigation', () => {
const initialRouteName = 'Location';
const { getByTestId } = renderWithNavigation({
navigatorConfig: { initialRouteName },
});
expect(getByTestId('location-display')).toHaveTextContent(initialRouteName);
});