Skip to content
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
30 changes: 21 additions & 9 deletions packages/react-native-gesture-handler/jestSetup.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
jest.mock('./src/RNGestureHandlerModule', () => require('./src/mocks/mocks'));
jest.mock('./src/components/GestureButtons', () => require('./src/mocks/mocks'));
jest.mock('./src/components/Pressable/Pressable', () => require('./src/mocks/Pressable'));

jest.mock('./src/RNGestureHandlerModule', () => require('./src/mocks/module'));
jest.mock('./src/components/GestureButtons', () => require('./src/mocks/gestureButtons'));
jest.mock('./src/components/Pressable/Pressable', () => require('./src/mocks/Pressable'));
jest.mock('./src/components/GestureComponents', () => require('./src/mocks/gestureComponents'));
jest.mock('./src/v3/detectors/HostGestureDetector', () => require('./src/mocks/hostDetector'));

jest.mock('./lib/commonjs/RNGestureHandlerModule', () =>
require('./lib/commonjs/mocks/mocks')
require('./lib/commonjs/mocks/module')
);
jest.mock('./lib/commonjs/components/GestureButtons', () =>
require('./lib/commonjs/mocks/mocks')
require('./lib/commonjs/mocks/GestureButtons')
);
jest.mock('./lib/commonjs/components/Pressable', () =>
require('./lib/commonjs/mocks/Pressable')
);

jest.mock('./lib/commonjs/components/GestureComponents', () =>
require('./lib/commonjs/mocks/gestureComponents')
);
jest.mock('./lib/commonjs/v3/detectors/HostGestureDetector', () =>
require('./lib/commonjs/mocks/hostDetector')
);

jest.mock('./lib/module/RNGestureHandlerModule', () =>
require('./lib/module/mocks/mocks')
require('./lib/module/mocks/module')
);
jest.mock('./lib/module/components/GestureButtons', () =>
require('./lib/module/mocks/mocks')
require('./lib/module/mocks/GestureButtons')
);
jest.mock('./lib/module/components/Pressable', () =>
require('./lib/module/mocks/Pressable')
);

jest.mock('./lib/module/components/GestureComponents', () =>
require('./lib/module/mocks/gestureComponents')
);
jest.mock('./lib/module/v3/detectors/HostGestureDetector', () =>
require('./lib/module/mocks/hostDetector')
);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Mocks from '../mocks/mocks';
import Mocks from '../mocks/module';

export default {
...Mocks,
Expand Down
125 changes: 125 additions & 0 deletions packages/react-native-gesture-handler/src/__tests__/Errors.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import React from 'react';
import { render, cleanup } from '@testing-library/react-native';
import {
Gesture,
GestureDetector,
GestureHandlerRootView,
InterceptingGestureDetector,
useTapGesture,
} from '../index';
import { findNodeHandle, View } from 'react-native';
import { VirtualDetector } from '../v3/detectors/VirtualDetector/VirtualDetector';

beforeEach(() => cleanup());
jest.mock('react-native/Libraries/ReactNative/RendererProxy', () => ({
findNodeHandle: jest.fn(),
}));

describe('VirtualDetector', () => {
test('virtual detector must be under InterceptingGestureDetector', () => {
function VirtualDetectorWithNoBoundary() {
const tap = useTapGesture({});
return (
<GestureHandlerRootView>
<VirtualDetector gesture={tap}>
<View />
</VirtualDetector>
</GestureHandlerRootView>
);
}

expect(() => render(<VirtualDetectorWithNoBoundary />)).toThrow(
'VirtualGestureDetector must be a descendant of an InterceptingGestureDetector'
);
});
test('virtual detector does not handle animated events', () => {
(findNodeHandle as jest.Mock).mockReturnValue(123);

function VirtualDetectorAnimated() {
const tap = useTapGesture({ useAnimated: true });
return (
<GestureHandlerRootView>
<InterceptingGestureDetector>
<VirtualDetector gesture={tap}>
<View />
</VirtualDetector>
</InterceptingGestureDetector>
</GestureHandlerRootView>
);
}

expect(() => render(<VirtualDetectorAnimated />)).toThrow(
'VirtualGestureDetector cannot handle Animated events with native driver when used inside InterceptingGestureDetector. Use Reanimated or Animated events without native driver instead.'
);
});

test('intercepting detector cant handle multiple types of events', () => {
(findNodeHandle as jest.Mock).mockReturnValue(123);
const mockWorklet = () => undefined;
mockWorklet.__workletHash = 123;

function InterceptingDetectorMultipleTypes() {
const tap = useTapGesture({ useAnimated: true });
const tap2 = useTapGesture({ onActivate: mockWorklet });
return (
<GestureHandlerRootView>
<InterceptingGestureDetector gesture={tap}>
<VirtualDetector gesture={tap2}>
<View />
</VirtualDetector>
</InterceptingGestureDetector>
</GestureHandlerRootView>
);
}

expect(() => render(<InterceptingDetectorMultipleTypes />)).toThrow(
'InterceptingGestureDetector can only handle either Reanimated or Animated events.'
);
});
});

describe('Check if descendant of root view', () => {
test('gesture detector', () => {
function GestureDetectorNoRootView() {
const tap = useTapGesture({});
return (
<GestureDetector gesture={tap}>
<View />
</GestureDetector>
);
}
expect(() => render(<GestureDetectorNoRootView />)).toThrow(
'GestureDetector must be used as a descendant of GestureHandlerRootView. Otherwise the gestures will not be recognized. See https://docs.swmansion.com/react-native-gesture-handler/docs/fundamentals/installation for more details.'
);
});

test('intercepting detector', () => {
function GestureDetectorNoRootView() {
const tap = useTapGesture({});
return (
<InterceptingGestureDetector gesture={tap}>
<View />
</InterceptingGestureDetector>
);
}

expect(() => render(<GestureDetectorNoRootView />)).toThrow(
'GestureDetector must be used as a descendant of GestureHandlerRootView. Otherwise the gestures will not be recognized. See https://docs.swmansion.com/react-native-gesture-handler/docs/fundamentals/installation for more details.'
);
});

test('legacy detector', () => {
function GestureDetectorNoRootView() {
const tap = Gesture.Tap();
return (
<GestureDetector gesture={tap}>
<View />
</GestureDetector>
);
}

expect(() => render(<GestureDetectorNoRootView />)).toThrow(
'GestureDetector must be used as a descendant of GestureHandlerRootView. Otherwise the gestures will not be recognized. See https://docs.swmansion.com/react-native-gesture-handler/docs/fundamentals/installation for more details.'
);
});
});
10 changes: 10 additions & 0 deletions packages/react-native-gesture-handler/src/mocks/GestureButtons.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import { TouchableNativeFeedback, View } from 'react-native';
export const RawButton = ({ enabled, ...rest }: any) => (
<TouchableNativeFeedback disabled={enabled === false} {...rest}>
<View />
</TouchableNativeFeedback>
);
export const BaseButton = RawButton;
export const RectButton = RawButton;
export const BorderlessButton = TouchableNativeFeedback;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
TouchableHighlight,
TouchableNativeFeedback,
TouchableOpacity,
TouchableWithoutFeedback,
ScrollView,
FlatList,
Switch,
TextInput,
DrawerLayoutAndroid,
} from 'react-native';

export default {
TouchableHighlight,
TouchableNativeFeedback,
TouchableOpacity,
TouchableWithoutFeedback,
ScrollView,
FlatList,
Switch,
TextInput,
DrawerLayoutAndroid,
} as const;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { View } from 'react-native';

const HostGestureDetector = View;

export default HostGestureDetector;
76 changes: 0 additions & 76 deletions packages/react-native-gesture-handler/src/mocks/mocks.tsx

This file was deleted.

25 changes: 25 additions & 0 deletions packages/react-native-gesture-handler/src/mocks/module.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const NOOP = () => {
// Do nothing
};

const attachGestureHandler = NOOP;
const createGestureHandler = NOOP;
const dropGestureHandler = NOOP;
const setGestureHandlerConfig = NOOP;
const updateGestureHandlerConfig = NOOP;
const flushOperations = NOOP;
const configureRelations = NOOP;
const setReanimatedAvailable = NOOP;
const install = NOOP;

export default {
attachGestureHandler,
createGestureHandler,
dropGestureHandler,
setGestureHandlerConfig,
updateGestureHandlerConfig,
configureRelations,
setReanimatedAvailable,
flushOperations,
install,
} as const;
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { use } from 'react';
import { isTestEnv } from '../../utils';
import { Platform } from 'react-native';
import GestureHandlerRootViewContext from '../../GestureHandlerRootViewContext';

export function useEnsureGestureHandlerRootView() {
const rootViewContext = use(GestureHandlerRootViewContext);

if (__DEV__ && !rootViewContext && !isTestEnv() && Platform.OS !== 'web') {
if (__DEV__ && !rootViewContext && Platform.OS !== 'web') {
throw new Error(
'GestureDetector must be used as a descendant of GestureHandlerRootView. Otherwise the gestures will not be recognized. See https://docs.swmansion.com/react-native-gesture-handler/docs/fundamentals/installation for more details.'
);
Expand Down
Loading