Skip to content

Commit 03e2c4b

Browse files
authored
fix: use actual react-native instead of homegrown mocks (#36)
<!-- Please provide enough information so that others can review your pull request. --> <!-- Keep pull requests small and focused on a single change. --> ### Summary This actually uncovered some bugs, like lack of `onDoublePress` (lol XD) and enabled us to have better snapshots for deep rendering. ### Test plan no new tests
1 parent 07d3be1 commit 03e2c4b

File tree

14 files changed

+1882
-138
lines changed

14 files changed

+1882
-138
lines changed

.flowconfig

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,69 @@
1+
[ignore]
2+
; We fork some components by platform
3+
.*/*[.]android.js
4+
5+
; Ignore duplicate module providers
6+
; For RN Apps installed via npm, "Libraries" folder is inside
7+
; "node_modules/react-native" but in the source repo it is in the root
8+
.*/Libraries/react-native/React.js
9+
10+
[include]
11+
112
[libs]
13+
node_modules/react-native/Libraries/react-native/react-native-interface.js
14+
node_modules/react-native/flow/
15+
node_modules/react-native/flow-github/
216
flow-typed
317

418
[options]
19+
emoji=true
20+
21+
esproposal.optional_chaining=enable
22+
esproposal.nullish_coalescing=enable
23+
24+
module.system=haste
25+
module.system.haste.use_name_reducers=true
26+
# get basename
27+
module.system.haste.name_reducers='^.*/\([a-zA-Z0-9$_.-]+\.js\(\.flow\)?\)$' -> '\1'
28+
# strip .js or .js.flow suffix
29+
module.system.haste.name_reducers='^\(.*\)\.js\(\.flow\)?$' -> '\1'
30+
# strip .ios suffix
31+
module.system.haste.name_reducers='^\(.*\)\.ios$' -> '\1'
32+
module.system.haste.name_reducers='^\(.*\)\.android$' -> '\1'
33+
module.system.haste.name_reducers='^\(.*\)\.native$' -> '\1'
34+
module.system.haste.paths.blacklist=.*/__tests__/.*
35+
module.system.haste.paths.blacklist=.*/__mocks__/.*
36+
module.system.haste.paths.blacklist=<PROJECT_ROOT>/node_modules/react-native/Libraries/Animated/src/polyfills/.*
37+
module.system.haste.paths.whitelist=<PROJECT_ROOT>/node_modules/react-native/Libraries/.*
38+
39+
munge_underscores=true
40+
41+
module.name_mapper='^[./a-zA-Z0-9$_-]+\.\(bmp\|gif\|jpg\|jpeg\|png\|psd\|svg\|webp\|m4v\|mov\|mp4\|mpeg\|mpg\|webm\|aac\|aiff\|caf\|m4a\|mp3\|wav\|html\|pdf\)$' -> 'RelativeImageStub'
42+
43+
module.file_ext=.js
44+
module.file_ext=.jsx
45+
module.file_ext=.json
46+
module.file_ext=.native.js
47+
48+
suppress_type=$FlowIssue
49+
suppress_type=$FlowFixMe
50+
suppress_type=$FlowFixMeProps
51+
suppress_type=$FlowFixMeState
52+
53+
suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe\\($\\|[^(]\\|(\\(<VERSION>\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)
54+
suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(<VERSION>\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)?:? #[0-9]+
55+
suppress_comment=\\(.\\|\n\\)*\\$FlowFixedInNextDeploy
56+
suppress_comment=\\(.\\|\n\\)*\\$FlowExpectedError
57+
558
include_warnings=true
59+
60+
[untyped]
61+
; bugs in react-native typings
62+
.*/Libraries/Components/Navigation/NavigatorIOS.ios.js
63+
.*/Libraries/Components/TextInput/InputAccessoryView.js
64+
.*/Libraries/Components/View/ViewPropTypes.js
65+
.*/Libraries/YellowBox/UI/YellowBoxInspectorFooter.js
66+
.*/Libraries/YellowBox/UI/YellowBoxList.js
67+
68+
[version]
69+
^0.83.0

README.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -193,25 +193,6 @@ const { getByTestId } = render(
193193
fireEvent.press(getByTestId('button'));
194194
```
195195

196-
### `fireEvent.doublePress: (element: ReactTestInstance) => void`
197-
198-
Invokes `doublePress` event handler on the element or parent element in the tree.
199-
200-
```jsx
201-
import { TouchableOpacity, Text } from 'react-native';
202-
import { render, fireEvent } from 'react-native-testing-library';
203-
204-
const onDoublePressMock = jest.fn();
205-
206-
const { getByTestId } = render(
207-
<TouchableOpacity onDoublePress={onDoublePressMock}>
208-
<Text testID="button-text">Click me</Text>
209-
</TouchableOpacity>
210-
);
211-
212-
fireEvent.doublePress(getByTestId('button-text'));
213-
```
214-
215196
### `fireEvent.changeText: (element: ReactTestInstance, data?: *) => void`
216197

217198
Invokes `changeText` event handler on the element or parent element in the tree.

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
"jest": "^23.6.0",
2222
"metro-react-native-babel-preset": "^0.48.0",
2323
"pretty-format": "^23.6.0",
24-
"react": "^16.5.2",
25-
"react-test-renderer": "^16.5.2",
24+
"react": "16.6.0-alpha.8af6728",
25+
"react-native": "^0.57.3",
26+
"react-test-renderer": "16.6.0-alpha.8af6728",
2627
"typescript": "^3.1.1"
2728
},
2829
"peerDependencies": {
@@ -35,5 +36,8 @@
3536
"flow-check": "flow check",
3637
"typescript-check": "tsc --noEmit --skipLibCheck --jsx react ./typings/__tests__/*",
3738
"lint": "eslint src --cache"
39+
},
40+
"jest": {
41+
"preset": "react-native"
3842
}
3943
}

src/__mocks__/reactNativeMock.js

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`toJSON 1`] = `"press me"`;
3+
exports[`toJSON 1`] = `
4+
<View
5+
accessible={true}
6+
isTVSelectable={true}
7+
onResponderGrant={[Function]}
8+
onResponderMove={[Function]}
9+
onResponderRelease={[Function]}
10+
onResponderTerminate={[Function]}
11+
onResponderTerminationRequest={[Function]}
12+
onStartShouldSetResponder={[Function]}
13+
style={
14+
Object {
15+
"opacity": 1,
16+
}
17+
}
18+
>
19+
<Text>
20+
press me
21+
</Text>
22+
</View>
23+
`;
Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`shallow rendering React Test Instance 1`] = `
4-
<TouchableOpacity
5-
onPress={[Function]}
6-
testID="button"
4+
<TouchableText
5+
accessible={true}
6+
allowFontScaling={true}
7+
ellipsizeMode="tail"
8+
forwardedRef={null}
9+
testID="text-button"
710
>
8-
<Text
9-
testID="text-button"
10-
>
11-
Press me
12-
</Text>
13-
</TouchableOpacity>
11+
Press me
12+
</TouchableText>
1413
`;
1514

1615
exports[`shallow rendering React elements 1`] = `
17-
<View
18-
testID="root"
19-
>
20-
<TouchableOpacity
21-
onPress={[Function]}
22-
testID="button"
16+
<Component>
17+
<Component
18+
testID="text-button"
2319
>
24-
<Text
25-
testID="text-button"
26-
>
27-
Press me
28-
</Text>
29-
</TouchableOpacity>
30-
</View>
20+
Press me
21+
</Component>
22+
</Component>
3123
`;

src/__tests__/fireEvent.test.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
Text,
77
ScrollView,
88
TextInput,
9-
} from '../__mocks__/reactNativeMock';
9+
} from 'react-native';
1010
import { render, fireEvent } from '..';
1111

1212
const OnPressComponent = ({ onPress }) => (
@@ -29,12 +29,6 @@ const CustomEventComponent = ({ onCustomEvent }) => (
2929
</TouchableOpacity>
3030
);
3131

32-
const DoublePressComponent = ({ onDoublePress }) => (
33-
<TouchableOpacity onDoublePress={onDoublePress}>
34-
<Text testID="button-text">Click me</Text>
35-
</TouchableOpacity>
36-
);
37-
3832
describe('fireEvent', () => {
3933
test('should invoke specified event', () => {
4034
const onPressMock = jest.fn();
@@ -120,17 +114,6 @@ test('fireEvent.scroll', () => {
120114
expect(onScrollMock).toHaveBeenCalledWith(eventData);
121115
});
122116

123-
test('fireEvent.doublePress', () => {
124-
const onDoublePressMock = jest.fn();
125-
const { getByTestId } = render(
126-
<DoublePressComponent onDoublePress={onDoublePressMock} />
127-
);
128-
129-
fireEvent.doublePress(getByTestId('button-text'));
130-
131-
expect(onDoublePressMock).toHaveBeenCalled();
132-
});
133-
134117
test('fireEvent.changeText', () => {
135118
const onChangeTextMock = jest.fn();
136119
const CHANGE_TEXT = 'content';

src/__tests__/render.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @flow
22
/* eslint-disable react/no-multi-comp */
33
import React from 'react';
4-
import { View, Text, TouchableOpacity } from '../__mocks__/reactNativeMock';
4+
import { View, Text, TouchableOpacity } from 'react-native';
55
import { render } from '..';
66

77
class Button extends React.Component<*> {

src/__tests__/shallow.test.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
// @flow
22
import React from 'react';
3-
import { View, TouchableOpacity, Text } from '../__mocks__/reactNativeMock';
3+
import { View, Text } from 'react-native';
44
import { shallow, render } from '..';
55

6-
const OnPressComponent = ({ onPress }) => (
7-
<View testID="root">
8-
<TouchableOpacity onPress={onPress} testID="button">
9-
<Text testID="text-button">Press me</Text>
10-
</TouchableOpacity>
6+
const TextPress = () => (
7+
<View>
8+
<Text testID="text-button">Press me</Text>
119
</View>
1210
);
1311

1412
test('shallow rendering React elements', () => {
15-
const { output } = shallow(<OnPressComponent onPress={jest.fn} />);
13+
const { output } = shallow(<TextPress />);
1614

1715
expect(output).toMatchSnapshot();
1816
});
1917

2018
test('shallow rendering React Test Instance', () => {
21-
const { getByTestId } = render(<OnPressComponent onPress={jest.fn} />);
22-
const { output } = shallow(getByTestId('root'));
19+
const { getByTestId } = render(<TextPress />);
20+
const { output } = shallow(getByTestId('text-button'));
2321

2422
expect(output).toMatchSnapshot();
2523
});

src/__tests__/waitForElement.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @flow
22
/* eslint-disable react/no-multi-comp */
33
import React from 'react';
4-
import { View, Text, TouchableOpacity } from '../__mocks__/reactNativeMock';
4+
import { View, Text, TouchableOpacity } from 'react-native';
55
import { render, fireEvent, waitForElement } from '..';
66

77
class Banana extends React.Component<*, *> {
@@ -13,7 +13,7 @@ class Banana extends React.Component<*, *> {
1313
return (
1414
<View>
1515
{this.props.fresh && <Text testID="fresh">Fresh</Text>}
16-
<TouchableOpacity onPress={this.changeFresh} type="primary">
16+
<TouchableOpacity onPress={this.changeFresh}>
1717
Change freshness!
1818
</TouchableOpacity>
1919
</View>

src/fireEvent.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ const toEventHandlerName = (eventName: string) =>
3434

3535
const pressHandler = (element: ReactTestInstance) =>
3636
invokeEvent(element, 'press');
37-
const doublePressHandler = (element: ReactTestInstance) =>
38-
invokeEvent(element, 'doublePress');
3937
const changeTextHandler = (element: ReactTestInstance, data?: *) =>
4038
invokeEvent(element, 'changeText', data);
4139
const scrollHandler = (element: ReactTestInstance, data?: *) =>
@@ -44,7 +42,6 @@ const scrollHandler = (element: ReactTestInstance, data?: *) =>
4442
const fireEvent = invokeEvent;
4543

4644
fireEvent.press = pressHandler;
47-
fireEvent.doublePress = doublePressHandler;
4845
fireEvent.changeText = changeTextHandler;
4946
fireEvent.scroll = scrollHandler;
5047

typings/__tests__/index.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ tree.unmount();
7171
fireEvent(getByNameString, 'press');
7272
fireEvent(getByNameString, 'press', 'data');
7373
fireEvent.press(getByNameString);
74-
fireEvent.doublePress(getByNameString);
7574
fireEvent.changeText(getByNameString, 'string');
7675
fireEvent.scroll(getByNameString, 'eventData');
7776

typings/index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export type FireEventFunction = (
4141

4242
export type FireEventAPI = FireEventFunction & {
4343
press: (element: ReactTestInstance) => any;
44-
doublePress: (element: ReactTestInstance) => any;
4544
changeText: (element: ReactTestInstance, data?: any) => any;
4645
scroll: (element: ReactTestInstance, data?: any) => any;
4746
};

0 commit comments

Comments
 (0)