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
This commit is contained in:
Michał Pierzchała
2018-10-16 18:51:01 +02:00
committed by GitHub
parent 07d3be1c51
commit 03e2c4b0f2
14 changed files with 1882 additions and 138 deletions
-6
View File
@@ -1,6 +0,0 @@
// @flow
export const View = (props: *) => props.children;
export const ScrollView = (props: *) => props.children;
export const Text = (props: *) => props.children;
export const TextInput = () => null;
export const TouchableOpacity = (props: *) => props.children;
@@ -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>
`;
@@ -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
testID="text-button"
>
<Text
testID="text-button"
>
Press me
</Text>
</TouchableOpacity>
</View>
Press me
</Component>
</Component>
`;
+1 -18
View File
@@ -6,7 +6,7 @@ import {
Text,
ScrollView,
TextInput,
} from '../__mocks__/reactNativeMock';
} from 'react-native';
import { render, fireEvent } from '..';
const OnPressComponent = ({ onPress }) => (
@@ -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();
@@ -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';
+1 -1
View File
@@ -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<*> {
+7 -9
View File
@@ -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();
});
+2 -2
View File
@@ -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<*, *> {
@@ -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>
-3
View File
@@ -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?: *) =>
@@ -44,7 +42,6 @@ const scrollHandler = (element: ReactTestInstance, data?: *) =>
const fireEvent = invokeEvent;
fireEvent.press = pressHandler;
fireEvent.doublePress = doublePressHandler;
fireEvent.changeText = changeTextHandler;
fireEvent.scroll = scrollHandler;