Skip to content

Commit 4b2fa18

Browse files
author
Andrzej Antoni Kwaśniewski
committed
rest
1 parent 87eea00 commit 4b2fa18

2 files changed

Lines changed: 70 additions & 8 deletions

File tree

packages/react-native-gesture-handler/src/__tests__/V3.test.tsx

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import React, { useRef } from 'react';
1+
import React from 'react';
22
import { render, cleanup } from '@testing-library/react-native';
33
import {
4+
Gesture,
5+
GestureDetector,
46
GestureHandlerRootView,
57
InterceptingGestureDetector,
6-
usePanGesture,
78
useTapGesture,
89
} from '../index';
910
import { findNodeHandle, View } from 'react-native';
@@ -34,20 +35,82 @@ describe('VirtualDetector', () => {
3435
(findNodeHandle as jest.Mock).mockReturnValue(123);
3536

3637
function VirtualDetectorAnimated() {
37-
const tap = usePanGesture({ useAnimated: true });
38-
const ref = useRef(null);
38+
const tap = useTapGesture({ useAnimated: true });
3939
return (
4040
<GestureHandlerRootView>
4141
<InterceptingGestureDetector>
4242
<VirtualDetector gesture={tap}>
43-
<View ref={ref} />
43+
<View />
4444
</VirtualDetector>
4545
</InterceptingGestureDetector>
4646
</GestureHandlerRootView>
4747
);
4848
}
4949
expect(() => render(<VirtualDetectorAnimated />)).toThrow(
50-
'[react-native-gesture-handler] VirtualGestureDetector cannot handle Animated events with native driver when used inside InterceptingGestureDetector. Use Reanimated or Animated events without native driver instead.'
50+
'VirtualGestureDetector cannot handle Animated events with native driver when used inside InterceptingGestureDetector. Use Reanimated or Animated events without native driver instead.'
51+
);
52+
});
53+
test('intercepting detector cant handle multiple types of events', () => {
54+
(findNodeHandle as jest.Mock).mockReturnValue(123);
55+
const mockWorklet = () => undefined;
56+
mockWorklet.__workletHash = 123;
57+
function InterceptingDetectorMultipleTypes() {
58+
const tap = useTapGesture({ useAnimated: true });
59+
const tap2 = useTapGesture({ onActivate: mockWorklet });
60+
return (
61+
<GestureHandlerRootView>
62+
<InterceptingGestureDetector gesture={tap}>
63+
<VirtualDetector gesture={tap2}>
64+
<View />
65+
</VirtualDetector>
66+
</InterceptingGestureDetector>
67+
</GestureHandlerRootView>
68+
);
69+
}
70+
expect(() => render(<InterceptingDetectorMultipleTypes />)).toThrow(
71+
'InterceptingGestureDetector can only handle either Reanimated or Animated events.'
72+
);
73+
});
74+
});
75+
76+
describe('Check if descendant of root view', () => {
77+
test('gesture detector', () => {
78+
function GestureDetectorNoRootView() {
79+
const tap = useTapGesture({});
80+
return (
81+
<GestureDetector gesture={tap}>
82+
<View />
83+
</GestureDetector>
84+
);
85+
}
86+
expect(() => render(<GestureDetectorNoRootView />)).toThrow(
87+
'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.'
88+
);
89+
});
90+
test('intercepting detector', () => {
91+
function GestureDetectorNoRootView() {
92+
const tap = useTapGesture({});
93+
return (
94+
<InterceptingGestureDetector gesture={tap}>
95+
<View />
96+
</InterceptingGestureDetector>
97+
);
98+
}
99+
expect(() => render(<GestureDetectorNoRootView />)).toThrow(
100+
'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.'
101+
);
102+
});
103+
test('legacy detector', () => {
104+
function GestureDetectorNoRootView() {
105+
const tap = Gesture.Tap();
106+
return (
107+
<GestureDetector gesture={tap}>
108+
<View />
109+
</GestureDetector>
110+
);
111+
}
112+
expect(() => render(<GestureDetectorNoRootView />)).toThrow(
113+
'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.'
51114
);
52115
});
53116
});

packages/react-native-gesture-handler/src/v3/detectors/useEnsureGestureHandlerRootView.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { use } from 'react';
2-
import { isTestEnv } from '../../utils';
32
import { Platform } from 'react-native';
43
import GestureHandlerRootViewContext from '../../GestureHandlerRootViewContext';
54

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

9-
if (__DEV__ && !rootViewContext && !isTestEnv() && Platform.OS !== 'web') {
8+
if (__DEV__ && !rootViewContext && Platform.OS !== 'web') {
109
throw new Error(
1110
'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.'
1211
);

0 commit comments

Comments
 (0)