Skip to content

fix: use actual react-native instead of homegrown mocks #36

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 2 commits into from
Oct 16, 2018
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
64 changes: 64 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,69 @@
[ignore]
; We fork some components by platform
.*/*[.]android.js

; Ignore duplicate module providers
; For RN Apps installed via npm, "Libraries" folder is inside
; "node_modules/react-native" but in the source repo it is in the root
.*/Libraries/react-native/React.js

[include]

[libs]
node_modules/react-native/Libraries/react-native/react-native-interface.js
node_modules/react-native/flow/
node_modules/react-native/flow-github/
flow-typed

[options]
emoji=true

esproposal.optional_chaining=enable
esproposal.nullish_coalescing=enable

module.system=haste
module.system.haste.use_name_reducers=true
# get basename
module.system.haste.name_reducers='^.*/\([a-zA-Z0-9$_.-]+\.js\(\.flow\)?\)$' -> '\1'
# strip .js or .js.flow suffix
module.system.haste.name_reducers='^\(.*\)\.js\(\.flow\)?$' -> '\1'
# strip .ios suffix
module.system.haste.name_reducers='^\(.*\)\.ios$' -> '\1'
module.system.haste.name_reducers='^\(.*\)\.android$' -> '\1'
module.system.haste.name_reducers='^\(.*\)\.native$' -> '\1'
module.system.haste.paths.blacklist=.*/__tests__/.*
module.system.haste.paths.blacklist=.*/__mocks__/.*
module.system.haste.paths.blacklist=<PROJECT_ROOT>/node_modules/react-native/Libraries/Animated/src/polyfills/.*
module.system.haste.paths.whitelist=<PROJECT_ROOT>/node_modules/react-native/Libraries/.*

munge_underscores=true

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'

module.file_ext=.js
module.file_ext=.jsx
module.file_ext=.json
module.file_ext=.native.js

suppress_type=$FlowIssue
suppress_type=$FlowFixMe
suppress_type=$FlowFixMeProps
suppress_type=$FlowFixMeState

suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe\\($\\|[^(]\\|(\\(<VERSION>\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)
suppress_comment=\\(.\\|\n\\)*\\$FlowIssue\\((\\(<VERSION>\\)? *\\(site=[a-z,_]*react_native[a-z,_]*\\)?)\\)?:? #[0-9]+
suppress_comment=\\(.\\|\n\\)*\\$FlowFixedInNextDeploy
suppress_comment=\\(.\\|\n\\)*\\$FlowExpectedError

include_warnings=true

[untyped]
; bugs in react-native typings
.*/Libraries/Components/Navigation/NavigatorIOS.ios.js
.*/Libraries/Components/TextInput/InputAccessoryView.js
.*/Libraries/Components/View/ViewPropTypes.js
.*/Libraries/YellowBox/UI/YellowBoxInspectorFooter.js
.*/Libraries/YellowBox/UI/YellowBoxList.js

[version]
^0.83.0
19 changes: 0 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,25 +193,6 @@ const { getByTestId } = render(
fireEvent.press(getByTestId('button'));
```

### `fireEvent.doublePress: (element: ReactTestInstance) => void`

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

```jsx
import { TouchableOpacity, Text } from 'react-native';
import { render, fireEvent } from 'react-native-testing-library';

const onDoublePressMock = jest.fn();

const { getByTestId } = render(
<TouchableOpacity onDoublePress={onDoublePressMock}>
<Text testID="button-text">Click me</Text>
</TouchableOpacity>
);

fireEvent.doublePress(getByTestId('button-text'));
```

### `fireEvent.changeText: (element: ReactTestInstance, data?: *) => void`

Invokes `changeText` event handler on the element or parent element in the tree.
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
"jest": "^23.6.0",
"metro-react-native-babel-preset": "^0.48.0",
"pretty-format": "^23.6.0",
"react": "^16.5.2",
"react-test-renderer": "^16.5.2",
"react": "16.6.0-alpha.8af6728",
"react-native": "^0.57.3",
"react-test-renderer": "16.6.0-alpha.8af6728",
"typescript": "^3.1.1"
},
"peerDependencies": {
Expand All @@ -35,5 +36,8 @@
"flow-check": "flow check",
"typescript-check": "tsc --noEmit --skipLibCheck --jsx react ./typings/__tests__/*",
"lint": "eslint src --cache"
},
"jest": {
"preset": "react-native"
}
}
6 changes: 0 additions & 6 deletions src/__mocks__/reactNativeMock.js

This file was deleted.

22 changes: 21 additions & 1 deletion src/__tests__/__snapshots__/render.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`toJSON 1`] = `"press me"`;
exports[`toJSON 1`] = `
<View
accessible={true}
isTVSelectable={true}
onResponderGrant={[Function]}
onResponderMove={[Function]}
onResponderRelease={[Function]}
onResponderTerminate={[Function]}
onResponderTerminationRequest={[Function]}
onStartShouldSetResponder={[Function]}
style={
Object {
"opacity": 1,
}
}
>
<Text>
press me
</Text>
</View>
`;
36 changes: 14 additions & 22 deletions src/__tests__/__snapshots__/shallow.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`shallow rendering React Test Instance 1`] = `
<TouchableOpacity
onPress={[Function]}
testID="button"
<TouchableText
accessible={true}
allowFontScaling={true}
ellipsizeMode="tail"
forwardedRef={null}
testID="text-button"
>
<Text
testID="text-button"
>
Press me
</Text>
</TouchableOpacity>
Press me
</TouchableText>
`;

exports[`shallow rendering React elements 1`] = `
<View
testID="root"
>
<TouchableOpacity
onPress={[Function]}
testID="button"
<Component>
<Component
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RN mocks are good at naming things XD

testID="text-button"
>
<Text
testID="text-button"
>
Press me
</Text>
</TouchableOpacity>
</View>
Press me
</Component>
</Component>
`;
19 changes: 1 addition & 18 deletions src/__tests__/fireEvent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Text,
ScrollView,
TextInput,
} from '../__mocks__/reactNativeMock';
} from 'react-native';
import { render, fireEvent } from '..';

const OnPressComponent = ({ onPress }) => (
Expand All @@ -29,12 +29,6 @@ const CustomEventComponent = ({ onCustomEvent }) => (
</TouchableOpacity>
);

const DoublePressComponent = ({ onDoublePress }) => (
<TouchableOpacity onDoublePress={onDoublePress}>
<Text testID="button-text">Click me</Text>
</TouchableOpacity>
);

describe('fireEvent', () => {
test('should invoke specified event', () => {
const onPressMock = jest.fn();
Expand Down Expand Up @@ -120,17 +114,6 @@ test('fireEvent.scroll', () => {
expect(onScrollMock).toHaveBeenCalledWith(eventData);
});

test('fireEvent.doublePress', () => {
const onDoublePressMock = jest.fn();
const { getByTestId } = render(
<DoublePressComponent onDoublePress={onDoublePressMock} />
);

fireEvent.doublePress(getByTestId('button-text'));

expect(onDoublePressMock).toHaveBeenCalled();
});

test('fireEvent.changeText', () => {
const onChangeTextMock = jest.fn();
const CHANGE_TEXT = 'content';
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/render.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow
/* eslint-disable react/no-multi-comp */
import React from 'react';
import { View, Text, TouchableOpacity } from '../__mocks__/reactNativeMock';
import { View, Text, TouchableOpacity } from 'react-native';
import { render } from '..';

class Button extends React.Component<*> {
Expand Down
16 changes: 7 additions & 9 deletions src/__tests__/shallow.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
// @flow
import React from 'react';
import { View, TouchableOpacity, Text } from '../__mocks__/reactNativeMock';
import { View, Text } from 'react-native';
import { shallow, render } from '..';

const OnPressComponent = ({ onPress }) => (
<View testID="root">
<TouchableOpacity onPress={onPress} testID="button">
<Text testID="text-button">Press me</Text>
</TouchableOpacity>
const TextPress = () => (
<View>
<Text testID="text-button">Press me</Text>
</View>
);

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

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

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

expect(output).toMatchSnapshot();
});
4 changes: 2 additions & 2 deletions src/__tests__/waitForElement.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow
/* eslint-disable react/no-multi-comp */
import React from 'react';
import { View, Text, TouchableOpacity } from '../__mocks__/reactNativeMock';
import { View, Text, TouchableOpacity } from 'react-native';
import { render, fireEvent, waitForElement } from '..';

class Banana extends React.Component<*, *> {
Expand All @@ -13,7 +13,7 @@ class Banana extends React.Component<*, *> {
return (
<View>
{this.props.fresh && <Text testID="fresh">Fresh</Text>}
<TouchableOpacity onPress={this.changeFresh} type="primary">
<TouchableOpacity onPress={this.changeFresh}>
Change freshness!
</TouchableOpacity>
</View>
Expand Down
3 changes: 0 additions & 3 deletions src/fireEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ const toEventHandlerName = (eventName: string) =>

const pressHandler = (element: ReactTestInstance) =>
invokeEvent(element, 'press');
const doublePressHandler = (element: ReactTestInstance) =>
invokeEvent(element, 'doublePress');
const changeTextHandler = (element: ReactTestInstance, data?: *) =>
invokeEvent(element, 'changeText', data);
const scrollHandler = (element: ReactTestInstance, data?: *) =>
Expand All @@ -44,7 +42,6 @@ const scrollHandler = (element: ReactTestInstance, data?: *) =>
const fireEvent = invokeEvent;

fireEvent.press = pressHandler;
fireEvent.doublePress = doublePressHandler;
fireEvent.changeText = changeTextHandler;
fireEvent.scroll = scrollHandler;

Expand Down
1 change: 0 additions & 1 deletion typings/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ tree.unmount();
fireEvent(getByNameString, 'press');
fireEvent(getByNameString, 'press', 'data');
fireEvent.press(getByNameString);
fireEvent.doublePress(getByNameString);
fireEvent.changeText(getByNameString, 'string');
fireEvent.scroll(getByNameString, 'eventData');

Expand Down
1 change: 0 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export type FireEventFunction = (

export type FireEventAPI = FireEventFunction & {
press: (element: ReactTestInstance) => any;
doublePress: (element: ReactTestInstance) => any;
changeText: (element: ReactTestInstance, data?: any) => any;
scroll: (element: ReactTestInstance, data?: any) => any;
};
Expand Down
Loading