Skip to content

Commit e0a08fe

Browse files
committed
Auto-disable Reanimated in tests
- Default gesture hooks to `disableReanimated` in test env when unset - Update controller tests to exercise the new API without explicit test flags
1 parent 72bdad4 commit e0a08fe

2 files changed

Lines changed: 42 additions & 20 deletions

File tree

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

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import { renderHook } from '@testing-library/react-native';
1+
import { render, renderHook } from '@testing-library/react-native';
2+
import { View } from 'react-native';
23

4+
import GestureHandlerRootView from '../components/GestureHandlerRootView';
35
import { createGestureController } from '../jestUtils';
46
import { State } from '../State';
7+
import { GestureDetector } from '../v3/detectors';
58
import type { PanGesture, TapGesture } from '../v3/hooks/gestures';
69
import { usePanGesture, useTapGesture } from '../v3/hooks/gestures';
710

@@ -65,9 +68,7 @@ function getControllerState(gesture: object): State {
6568
describe('createGestureController', () => {
6669
test('transitions to the expected state after each gesture lifecycle step', () => {
6770
const { order, callbacks } = mockedContinuousCallbacks();
68-
const pan = renderHook(() =>
69-
usePanGesture({ disableReanimated: true, ...callbacks })
70-
).result.current;
71+
const pan = renderHook(() => usePanGesture(callbacks)).result.current;
7172
const gesture = createGestureController(pan);
7273

7374
gesture.begin({ translationX: 0 });
@@ -111,9 +112,7 @@ describe('createGestureController', () => {
111112

112113
test('can fail a gesture before activation', () => {
113114
const { order, callbacks } = mockedDiscreteCallbacks();
114-
const tap = renderHook(() =>
115-
useTapGesture({ disableReanimated: true, ...callbacks })
116-
).result.current;
115+
const tap = renderHook(() => useTapGesture(callbacks)).result.current;
117116
const gesture = createGestureController(tap);
118117

119118
gesture.begin({ x: 10 });
@@ -133,7 +132,6 @@ describe('createGestureController', () => {
133132
renderHook(() =>
134133
useTapGesture({
135134
testID: 'controlled-tap',
136-
disableReanimated: true,
137135
...callbacks,
138136
})
139137
);
@@ -151,9 +149,34 @@ describe('createGestureController', () => {
151149
]);
152150
});
153151

152+
test('controls a gesture created inside a component', () => {
153+
const onBegin = jest.fn();
154+
155+
function ComponentWithGesture() {
156+
const tap = useTapGesture({
157+
testID: 'component-tap',
158+
onBegin,
159+
});
160+
161+
return (
162+
<GestureHandlerRootView>
163+
<GestureDetector gesture={tap}>
164+
<View />
165+
</GestureDetector>
166+
</GestureHandlerRootView>
167+
);
168+
}
169+
170+
render(<ComponentWithGesture />);
171+
172+
const gesture = createGestureController('component-tap');
173+
gesture.begin();
174+
175+
expect(onBegin).toHaveBeenCalledTimes(1);
176+
});
177+
154178
test('guards against invalid lifecycle order', () => {
155-
const tap = renderHook(() => useTapGesture({ disableReanimated: true }))
156-
.result.current;
179+
const tap = renderHook(() => useTapGesture()).result.current;
157180
const gesture = createGestureController(tap);
158181

159182
expect(() => gesture.update()).toThrow(
@@ -176,9 +199,8 @@ describe('createGestureController', () => {
176199
(terminalAction, terminalState) => {
177200
const onBegin = jest.fn();
178201
const onFinalize = jest.fn();
179-
const tap = renderHook(() =>
180-
useTapGesture({ disableReanimated: true, onBegin, onFinalize })
181-
).result.current;
202+
const tap = renderHook(() => useTapGesture({ onBegin, onFinalize }))
203+
.result.current;
182204
const gesture = createGestureController(tap);
183205

184206
gesture.begin();
@@ -200,9 +222,7 @@ describe('createGestureController', () => {
200222

201223
test('rejects raw state-machine fields in event payloads', () => {
202224
const onBegin = jest.fn();
203-
const tap = renderHook(() =>
204-
useTapGesture({ disableReanimated: true, onBegin })
205-
).result.current;
225+
const tap = renderHook(() => useTapGesture({ onBegin })).result.current;
206226
const gesture = createGestureController(tap);
207227

208228
expect(() => gesture.begin({ state: State.BEGAN } as never)).toThrow(
@@ -221,7 +241,6 @@ describe('createGestureController', () => {
221241
const hook = renderHook(
222242
({ value }: { value: number }) =>
223243
usePanGesture({
224-
disableReanimated: true,
225244
onBegin: () => calls.push(`begin-${value}`),
226245
onActivate: () => calls.push(`activate-${value}`),
227246
}),
@@ -240,7 +259,7 @@ describe('createGestureController', () => {
240259
const onBegin = jest.fn();
241260
const hook = renderHook(
242261
({ enabled }: { enabled: boolean }) =>
243-
usePanGesture({ disableReanimated: true, enabled, onBegin }),
262+
usePanGesture({ enabled, onBegin }),
244263
{ initialProps: { enabled: true } }
245264
);
246265
const gesture = createGestureController(hook.result.current);
@@ -262,7 +281,6 @@ describe('createGestureController', () => {
262281
const { order, callbacks } = mockedContinuousCallbacks();
263282
const pan = renderHook(() =>
264283
usePanGesture({
265-
disableReanimated: true,
266284
enabled: false,
267285
...callbacks,
268286
})

packages/react-native-gesture-handler/src/v3/hooks/utils/configUtils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useMemo } from 'react';
22

33
import { Reanimated } from '../../../handlers/gestures/reanimatedWrapper';
4-
import { tagMessage } from '../../../utils';
4+
import { isTestEnv, tagMessage } from '../../../utils';
55
import type {
66
BaseGestureConfig,
77
ExcludeInternalConfigProps,
@@ -36,6 +36,10 @@ export function resolveInternalConfigProps<
3636
THandlerData,
3737
TExtendedHandlerData extends THandlerData,
3838
>(config: BaseGestureConfig<TConfig, THandlerData, TExtendedHandlerData>) {
39+
if (isTestEnv() && config.disableReanimated === undefined) {
40+
config.disableReanimated = true;
41+
}
42+
3943
if (
4044
__DEV__ &&
4145
isNativeAnimatedEvent(config.onUpdate) &&

0 commit comments

Comments
 (0)