software-mansion / react-native-gesture-handler

Declarative API exposing platform native touch and gesture system to React Native.
https://docs.swmansion.com/react-native-gesture-handler/
MIT License
6.13k stars 982 forks source link

[Jest] Don't fire callbacks on disabled gestures #3119

Closed m-bert closed 2 months ago

m-bert commented 2 months ago

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

Run the following test: ```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 ( ); }; describe('Some Random Tests', () => { type TestData = { title: string; enabled: boolean; timesCalled: number; }; it.each([ { 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(); fireGestureHandler(getByGestureTestId('tap')); expect(callback).toHaveBeenCalledTimes(timesCalled); }); }); describe('Button test', () => { const callback = jest.fn(); const { getByTestId } = render( ); fireGestureHandler(getByTestId('btn')); expect(callback).toHaveBeenCalledTimes(0); }); describe('Old API test', () => { const callback = jest.fn(); const { getByTestId } = render( ); fireGestureHandler(getByTestId('tap')); expect(callback).toHaveBeenCalledTimes(0); }); ```