Skip to content

Commit 5b41fb1

Browse files
authored
[Jest] Don't fire callbacks on disabled gestures (#3119)
## Description As pointed in #3117, `fireGestureHandler` runs callback function even if gesture has been marked as disabled with `enable(false)`. This PR removes this behavior. Closes #3117 ## Test plan <details> <summary>Run the following test:</summary> ```tsx import React from 'react'; import { View } from 'react-native'; import Animated from 'react-native-reanimated'; import { Gesture, GestureDetector, GestureHandlerRootView, TapGestureHandler, type TapGesture, } from '../'; import { fireGestureHandler, getByGestureTestId } from '../jestUtils'; import { render } from '@testing-library/react-native'; import Mocks from '../mocks'; type ComponentProps = { enabled: boolean; callback: () => void; }; const Component = ({ enabled, callback }: ComponentProps) => { const tap = Gesture.Tap() .withTestId('tap') .enabled(enabled) .runOnJS(true) .onEnd(callback); return ( <GestureDetector gesture={tap}> <Animated.View style={{ width: 200, height: 200, backgroundColor: 'orange' }} /> </GestureDetector> ); }; describe('Some Random Tests', () => { type TestData = { title: string; enabled: boolean; timesCalled: number; }; it.each<TestData>([ { title: 'should trigger callback once', enabled: true, timesCalled: 1 }, { title: 'should not trigger callback', enabled: false, timesCalled: 0 }, ])('$title', ({ enabled, timesCalled }) => { const callback = jest.fn(); render(<Component enabled={enabled} callback={callback} />); fireGestureHandler<TapGesture>(getByGestureTestId('tap')); expect(callback).toHaveBeenCalledTimes(timesCalled); }); }); describe('Button test', () => { const callback = jest.fn(); const { getByTestId } = render( <Mocks.RectButton enabled={false} onPress={callback} testID="btn" /> ); fireGestureHandler(getByTestId('btn')); expect(callback).toHaveBeenCalledTimes(0); }); describe('Old API test', () => { const callback = jest.fn(); const { getByTestId } = render( <GestureHandlerRootView> <TapGestureHandler testID="tap" onActivated={callback} enabled={false}> <View /> </TapGestureHandler> </GestureHandlerRootView> ); fireGestureHandler(getByTestId('tap')); expect(callback).toHaveBeenCalledTimes(0); }); ``` </details>
1 parent a92c219 commit 5b41fb1

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/handlers/createHandler.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ export default function createHandler<
529529
? {
530530
handlerType: name,
531531
handlerTag: this.handlerTag,
532+
enabled: this.props.enabled,
532533
}
533534
: {}),
534535
testID: this.props.testID ?? child.props.testID,

src/jestUtils/jestUtils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ interface HandlerData {
405405
emitEvent: EventEmitter;
406406
handlerType: HandlerNames;
407407
handlerTag: number;
408+
enabled: boolean | undefined;
408409
}
409410
function getHandlerData(
410411
componentOrGesture: ReactTestInstance | GestureType
@@ -417,6 +418,7 @@ function getHandlerData(
417418
},
418419
handlerType: gesture.handlerName as HandlerNames,
419420
handlerTag: gesture.handlerTag,
421+
enabled: gesture.config.enabled,
420422
};
421423
}
422424
const gestureHandlerComponent = componentOrGesture;
@@ -426,6 +428,7 @@ function getHandlerData(
426428
},
427429
handlerType: gestureHandlerComponent.props.handlerType as HandlerNames,
428430
handlerTag: gestureHandlerComponent.props.handlerTag as number,
431+
enabled: gestureHandlerComponent.props.enabled,
429432
};
430433
}
431434
type AllGestures =
@@ -467,9 +470,13 @@ export function fireGestureHandler<THandler extends AllGestures | AllHandlers>(
467470
componentOrGesture: ReactTestInstance | GestureType,
468471
eventList: Partial<GestureHandlerTestEvent<ExtractConfig<THandler>>>[] = []
469472
): void {
470-
const { emitEvent, handlerType, handlerTag } =
473+
const { emitEvent, handlerType, handlerTag, enabled } =
471474
getHandlerData(componentOrGesture);
472475

476+
if (enabled === false) {
477+
return;
478+
}
479+
473480
let _ = fillMissingStatesTransitions(
474481
eventList,
475482
isDiscreteHandler(handlerType)

0 commit comments

Comments
 (0)