Skip to content
This repository was archived by the owner on Jul 30, 2020. It is now read-only.

Commit 61347d4

Browse files
committed
refactor: use jest-native text matcher in tests
1 parent 5ea9f66 commit 61347d4

12 files changed

+49
-33
lines changed

examples/__tests__/input-event.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import { Text, TextInput, View } from 'react-native';
3-
import { render, fireEvent } from '../../src';
3+
import { render, fireEvent } from 'native-testing-library';
44

55
class CostInput extends React.Component {
66
state = {

examples/__tests__/react-context.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import 'jest-native/extend-expect';
12
import React from 'react';
23
import { Text } from 'react-native';
4+
import { render } from 'native-testing-library';
35

4-
import { render } from '../../src';
56
import { NameContext, NameProvider, NameConsumer } from '../react-context';
67

78
test('NameConsumer shows default value', () => {
89
const { getByText } = render(<NameConsumer />);
9-
expect(getByText(/^My Name Is:/).props.children.join('')).toBe('My Name Is: Unknown');
10+
expect(getByText(/^My Name Is:/)).toHaveTextContent('My Name Is: Unknown');
1011
});
1112

1213
test('NameConsumer shows value from provider', () => {
@@ -16,7 +17,7 @@ test('NameConsumer shows value from provider', () => {
1617
</NameContext.Provider>
1718
);
1819
const { getByText } = render(tree);
19-
expect(getByText(/^My Name Is:/).props.children.join('')).toBe('My Name Is: C3P0');
20+
expect(getByText(/^My Name Is:/)).toHaveTextContent('My Name Is: C3P0');
2021
});
2122

2223
test('NameProvider composes full name from first, last', () => {
@@ -26,7 +27,7 @@ test('NameProvider composes full name from first, last', () => {
2627
</NameProvider>
2728
);
2829
const { getByText } = render(tree);
29-
expect(getByText(/^Received:/).props.children.join('')).toBe('Received: Boba Fett');
30+
expect(getByText(/^Received:/)).toHaveTextContent('Received: Boba Fett');
3031
});
3132

3233
test('NameProvider/Consumer shows name of character', () => {
@@ -36,5 +37,5 @@ test('NameProvider/Consumer shows name of character', () => {
3637
</NameProvider>
3738
);
3839
const { getByText } = render(tree);
39-
expect(getByText(/^My Name Is:/).props.children.join('')).toBe('My Name Is: Leia Organa');
40+
expect(getByText(/^My Name Is:/)).toHaveTextContent('My Name Is: Leia Organa');
4041
});

examples/__tests__/react-intl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { FormattedDate } from 'react-intl-native';
55
import IntlPolyfill from 'intl';
66
import 'intl/locale-data/jsonp/pt';
77

8-
import { getByText, render } from '../../src';
8+
import { getByText, render } from 'native-testing-library';
99

1010
const setupTests = () => {
1111
if (global.Intl) {

examples/__tests__/react-navigation.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import 'jest-native/extend-expect';
12
import React from 'react';
23
import { Button, Text, View } from 'react-native';
34
import { createStackNavigator, createAppContainer, withNavigation } from 'react-navigation';
45

5-
import { render, fireEvent } from '../../src';
6+
import { render, fireEvent } from 'native-testing-library';
67

78
jest.mock('NativeAnimatedHelper').mock('react-native-gesture-handler', () => {
8-
const View = require('react-native/Libraries/Components/View/View');
9+
const View = require('react-native').View;
910
return {
1011
State: {},
1112
PanGestureHandler: View,
@@ -14,6 +15,7 @@ jest.mock('NativeAnimatedHelper').mock('react-native-gesture-handler', () => {
1415
};
1516
});
1617

18+
const originalConsoleWarn = console.warn;
1719
console.warn = arg => {
1820
const warnings = [
1921
'Calling .measureInWindow()',
@@ -25,21 +27,23 @@ console.warn = arg => {
2527

2628
const finalArgs = warnings.reduce((acc, curr) => (arg.includes(curr) ? [...acc, arg] : acc), []);
2729

28-
if (!finalArgs.length) {
29-
console.warn(arg);
30+
if (finalArgs.length) {
31+
return;
3032
}
33+
34+
originalConsoleWarn(message);
3135
};
3236

3337
const Home = ({ navigation }) => (
3438
<View>
3539
<Text testID="title">Home page</Text>
36-
<Button title="About page" onPress={() => navigation.navigate('About')} />
40+
<Button title="Go to about" onPress={() => navigation.navigate('About')} />
3741
</View>
3842
);
3943
const About = ({ navigation }) => (
4044
<View>
4145
<Text testID="title">About page</Text>
42-
<Button title="About page" onPress={() => navigation.navigate('Home')} />
46+
<Button title="Go to home" onPress={() => navigation.navigate('Home')} />
4347
</View>
4448
);
4549
const Location = () => (
@@ -71,15 +75,18 @@ function renderWithNavigation({ screens = {}, navigatorConfig = {} } = {}) {
7175

7276
test('full app rendering/navigating', async () => {
7377
const { findByText, getByTestId, getByText } = renderWithNavigation();
74-
expect(getByTestId('title').props.children).toMatch('Home page');
75-
fireEvent.press(getByText(/About page/i));
76-
await expect(findByText('About page')).toBeTruthy();
78+
79+
expect(getByTestId('title')).toHaveTextContent('Home page');
80+
fireEvent.press(getByText(/Go to about/i));
81+
82+
const result = await findByText('About page');
83+
expect(result).toHaveTextContent('About page');
7784
});
7885

7986
test('rendering a component that uses withNavigation', () => {
8087
const initialRouteName = 'Location';
8188
const { getByTestId } = renderWithNavigation({
8289
navigatorConfig: { initialRouteName },
8390
});
84-
expect(getByTestId('location-display').props.children).toBe(initialRouteName);
91+
expect(getByTestId('location-display')).toHaveTextContent(initialRouteName);
8592
});

examples/__tests__/react-redux.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import 'jest-native/extend-expect';
12
import React from 'react';
2-
import { Button, Text, View } from 'react-native';
33
import { createStore } from 'redux';
44
import { Provider, connect } from 'react-redux';
5-
import { render, fireEvent } from '../../src';
5+
import { Button, Text, View } from 'react-native';
6+
import { render, fireEvent } from 'native-testing-library';
67

78
class Counter extends React.Component {
89
increment = () => {
@@ -54,15 +55,15 @@ function renderWithRedux(ui, { initialState, store = createStore(reducer, initia
5455
test('can render with redux with defaults', () => {
5556
const { getByTestId, getByText } = renderWithRedux(<ConnectedCounter />);
5657
fireEvent.press(getByText('+'));
57-
expect(getByTestId('count-value').props.children).toBe(1);
58+
expect(getByTestId('count-value')).toHaveTextContent(1);
5859
});
5960

6061
test('can render with redux with custom initial state', () => {
6162
const { getByTestId, getByText } = renderWithRedux(<ConnectedCounter />, {
6263
initialState: { count: 3 },
6364
});
6465
fireEvent.press(getByText('-'));
65-
expect(getByTestId('count-value').props.children).toBe(2);
66+
expect(getByTestId('count-value')).toHaveTextContent(2);
6667
});
6768

6869
test('can render with redux with custom store', () => {
@@ -71,7 +72,7 @@ test('can render with redux with custom store', () => {
7172
store,
7273
});
7374
fireEvent.press(getByText('+'));
74-
expect(getByTestId('count-value').props.children).toBe(1000);
75+
expect(getByTestId('count-value')).toHaveTextContent(1000);
7576
fireEvent.press(getByText('-'));
76-
expect(getByTestId('count-value').props.children).toBe(1000);
77+
expect(getByTestId('count-value')).toHaveTextContent(1000);
7778
});

examples/__tests__/update-props.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
2+
import 'jest-native/extend-expect';
23
import { Text, View } from 'react-native';
3-
import { render } from '../../src';
4+
import { render } from 'native-testing-library';
45

56
let idCounter = 1;
67

@@ -18,10 +19,10 @@ class NumberDisplay extends React.Component {
1819

1920
test('calling render with the same component on the same container does not remount', () => {
2021
const { getByTestId, rerender } = render(<NumberDisplay number={1} />);
21-
expect(getByTestId('number-display').props.children).toEqual(1);
22+
expect(getByTestId('number-display')).toHaveTextContent(1);
2223

2324
rerender(<NumberDisplay number={2} />);
24-
expect(getByTestId('number-display').props.children).toEqual(2);
25+
expect(getByTestId('number-display')).toHaveTextContent(2);
2526

26-
expect(getByTestId('instance-id').props.children).toEqual(1);
27+
expect(getByTestId('instance-id')).toHaveTextContent(1);
2728
});

examples/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ module.exports = {
44
roots: [__dirname],
55
rootDir: __dirname,
66
moduleNameMapper: {
7-
'rn-testing-library': '<rootDir>src',
7+
'native-testing-library': '<rootDir>src',
88
},
99
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"jest": "24.5.0",
4949
"jest-fetch-mock": "^2.1.1",
5050
"jest-in-case": "^1.0.2",
51+
"jest-native": "^1.2.0",
5152
"metro-react-native-babel-preset": "^0.52.0",
5253
"prettier": "^1.16.4",
5354
"pretty-quick": "^1.10.0",

src/__tests__/act.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import 'jest-native/extend-expect';
23
import { Button } from 'react-native';
34

45
import { render, fireEvent } from '../';
@@ -25,7 +26,7 @@ test('fireEvent triggers useEffect calls', () => {
2526
const buttonNode = getByText('0');
2627
effectCb.mockClear();
2728
fireEvent.press(buttonNode);
28-
expect(buttonNode.props.children).toEqual('1');
29+
expect(buttonNode).toHaveTextContent('1');
2930
expect(effectCb).toHaveBeenCalledTimes(1);
3031
});
3132

src/__tests__/end-to-end.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React from 'react';
2+
import 'jest-native/extend-expect';
23
import { Text } from 'react-native';
4+
35
import { render, wait } from '../';
46

57
const fetchAMessage = () =>
@@ -32,5 +34,5 @@ test('it waits for the data to be loaded', async () => {
3234
expect(queryByText('Loading...')).toBeTruthy();
3335

3436
await wait(() => expect(queryByText('Loading...')).toBeNull());
35-
expect(queryByTestId('message').props.children.join('')).toMatch(/Hello World/);
37+
expect(queryByTestId('message')).toHaveTextContent(/Hello World/);
3638
});

src/__tests__/fetch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import 'jest-native/extend-expect';
23
import { TouchableOpacity, Text, View } from 'react-native';
34

45
import { render, fireEvent, wait } from '../';
@@ -42,6 +43,6 @@ test('Fetch makes an API call and displays the greeting when load-greeting is cl
4243
expect(fetch).toHaveBeenCalledTimes(1);
4344
expect(fetch).toHaveBeenCalledWith(url);
4445

45-
expect(getByText('hello there').props.children).toEqual('hello there');
46+
expect(getByText('hello there')).toHaveTextContent('hello there');
4647
expect(container).toMatchSnapshot();
4748
});

src/__tests__/rerender.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import 'jest-native/extend-expect';
23
import { Text } from 'react-native';
34

45
import { render } from '../';
@@ -9,7 +10,7 @@ test('rerender will re-render the element', () => {
910

1011
const message = getByText('hi');
1112

12-
expect(message.props.children).toEqual('hi');
13+
expect(message).toHaveTextContent('hi');
1314
rerender(<Greeting message="hey" />);
14-
expect(message.props.children).toEqual('hey');
15+
expect(message).toHaveTextContent('hey');
1516
});

0 commit comments

Comments
 (0)