Skip to content
Merged
4 changes: 4 additions & 0 deletions packages/docs-gesture-handler/docs/guides/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ RNGH provides APIs, specifically [`fireGestureHandler`](#firegesturehandler) and
### fireGestureHandler

```ts
import { fireGestureHandler } from 'react-native-gesture-handler/jest-utils';

fireGestureHandler: (componentOrGesture, eventList) => void;
Comment thread
m-bert marked this conversation as resolved.
```

Expand Down Expand Up @@ -80,6 +82,8 @@ const allImplicits = []; // 3 events, one BEGIN, one ACTIVE, one END with defaul
### getByGestureTestId

```tsx
import { getByGestureTestId } from 'react-native-gesture-handler/jest-utils';

getByGestureTestId: (testID: string) => Gesture;
Comment thread
m-bert marked this conversation as resolved.
```

Expand Down
10 changes: 6 additions & 4 deletions packages/react-native-gesture-handler/jestSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ jest.mock('./src/components/Pressable/Pressable', () =>
jest.mock('./src/components/GestureComponents', () =>
require('./src/mocks/gestureComponents')
);
jest.mock('./src/components/touchables', () =>
require('./src/mocks/Touchables')
);
jest.mock('./src/v3/detectors/HostGestureDetector', () =>
require('./src/mocks/hostDetector')
);
Expand All @@ -18,6 +21,9 @@ jest.mock('./lib/module/RNGestureHandlerModule', () =>
jest.mock('./lib/module/components/GestureButtons', () =>
require('./lib/module/mocks/GestureButtons')
);
jest.mock('./lib/module/components/touchables', () =>
require('./lib/module/mocks/Touchables')
);
jest.mock('./lib/module/components/Pressable', () =>
require('./lib/module/mocks/Pressable')
);
Expand All @@ -27,7 +33,3 @@ jest.mock('./lib/module/components/GestureComponents', () =>
jest.mock('./lib/module/v3/detectors/HostGestureDetector', () =>
require('./lib/module/mocks/hostDetector')
);

jest.mock('react-native-worklets', () =>
require('react-native-worklets/src/mock')
);
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import { findNodeHandle, View } from 'react-native';
import { VirtualDetector } from '../v3/detectors/VirtualDetector/VirtualDetector';

jest.mock('react-native-worklets', () =>
require('react-native-worklets/src/mock')

Check warning on line 14 in packages/react-native-gesture-handler/src/__tests__/Errors.test.tsx

View workflow job for this annotation

GitHub Actions / check

Unsafe return of an `any` typed value
);

beforeEach(() => cleanup());
jest.mock('react-native/Libraries/ReactNative/RendererProxy', () => ({
findNodeHandle: jest.fn(),
Expand Down
136 changes: 136 additions & 0 deletions packages/react-native-gesture-handler/src/__tests__/mocks.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import React from 'react';
import { Text } from 'react-native';
import { render, fireEvent } from '@testing-library/react-native';

import {
LegacyRawButton,
LegacyBaseButton,
LegacyRectButton,
LegacyBorderlessButton,
LegacyPureNativeButton,
} from '../mocks/GestureButtons';

import {
LegacyScrollView,
LegacyFlatList,
LegacySwitch,
LegacyTextInput,
LegacyRefreshControl,
} from '../mocks/gestureComponents';

import {
TouchableHighlight,
TouchableNativeFeedback,
TouchableOpacity,
TouchableWithoutFeedback,
} from '../mocks/Touchables';

import LegacyPressable from '../mocks/Pressable';

import GestureHandlerRootView from '../components/GestureHandlerRootView';
import { Touchable } from '../v3/components';

Comment thread
m-bert marked this conversation as resolved.
describe('Jest mocks – legacy components render without crashing', () => {
test('LegacyRawButton', () => {
expect(() => render(<LegacyRawButton />)).not.toThrow();
});

test('LegacyBaseButton', () => {
expect(() => render(<LegacyBaseButton />)).not.toThrow();
});

test('LegacyRectButton', () => {
expect(() => render(<LegacyRectButton />)).not.toThrow();
});

test('LegacyBorderlessButton', () => {
expect(() => render(<LegacyBorderlessButton />)).not.toThrow();
});

test('LegacyPureNativeButton', () => {
expect(() => render(<LegacyPureNativeButton />)).not.toThrow();
});

test('LegacyPressable', () => {
expect(() => render(<LegacyPressable />)).not.toThrow();
});

test('LegacyScrollView', () => {
expect(() => render(<LegacyScrollView />)).not.toThrow();
});

test('LegacyFlatList', () => {
expect(() =>
render(<LegacyFlatList data={[]} renderItem={() => null} />)
).not.toThrow();
});

test('LegacySwitch', () => {
expect(() => render(<LegacySwitch />)).not.toThrow();
});

test('LegacyTextInput', () => {
expect(() => render(<LegacyTextInput />)).not.toThrow();
});

test('LegacyRefreshControl', () => {
expect(() =>
render(<LegacyRefreshControl refreshing={false} />)
).not.toThrow();
});

test('TouchableHighlight', () => {
expect(() =>
render(
<TouchableHighlight>
<Text>Test</Text>
</TouchableHighlight>
)
).not.toThrow();
});

test('TouchableNativeFeedback', () => {
expect(() =>
render(
<TouchableNativeFeedback>
<Text>Test</Text>
</TouchableNativeFeedback>
)
).not.toThrow();
});

test('TouchableOpacity', () => {
expect(() =>
render(
<TouchableOpacity>
<Text>Test</Text>
</TouchableOpacity>
)
).not.toThrow();
});

test('TouchableWithoutFeedback', () => {
expect(() =>
render(
<TouchableWithoutFeedback>
<Text>Test</Text>
</TouchableWithoutFeedback>
)
).not.toThrow();
});
});

test('Trigger press by text', () => {
const onPress = jest.fn();
const { getByText } = render(
<GestureHandlerRootView>
<Touchable activeOpacity={0.3} onPress={onPress}>
<Text>Press Me</Text>
</Touchable>
</GestureHandlerRootView>
);

fireEvent.press(getByText('Press Me'));
Comment thread
m-bert marked this conversation as resolved.

expect(onPress).toHaveBeenCalled();
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import React from 'react';
import { TouchableNativeFeedback, View } from 'react-native';
export const RawButton = ({ enabled, children, ...rest }: any) => (

const RawButton = ({ enabled, children, ...rest }: any) => (
<TouchableNativeFeedback disabled={enabled === false} {...rest}>
{children ?? <View />}
</TouchableNativeFeedback>
);
export const BaseButton = RawButton;
export const RectButton = RawButton;
export const BorderlessButton = TouchableNativeFeedback;

export const LegacyRawButton = RawButton;
export const LegacyBaseButton = RawButton;
export const LegacyRectButton = RawButton;
export const LegacyBorderlessButton = RawButton;
export const LegacyPureNativeButton = RawButton;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export {
TouchableHighlight,
TouchableNativeFeedback,
TouchableOpacity,
TouchableWithoutFeedback,
} from 'react-native';
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
import {
TouchableHighlight,
TouchableNativeFeedback,
TouchableOpacity,
TouchableWithoutFeedback,
ScrollView,
FlatList,
Switch,
TextInput,
DrawerLayoutAndroid,
export {
ScrollView as LegacyScrollView,
FlatList as LegacyFlatList,
Switch as LegacySwitch,
TextInput as LegacyTextInput,
DrawerLayoutAndroid as LegacyDrawerLayoutAndroid,
RefreshControl as LegacyRefreshControl,
} from 'react-native';

export default {
TouchableHighlight,
TouchableNativeFeedback,
TouchableOpacity,
TouchableWithoutFeedback,
Comment thread
j-piasecki marked this conversation as resolved.
ScrollView,
FlatList,
Switch,
TextInput,
DrawerLayoutAndroid,
} as const;
Loading