Commit 5b41fb1
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
2 files changed
+9
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
529 | 529 | | |
530 | 530 | | |
531 | 531 | | |
| 532 | + | |
532 | 533 | | |
533 | 534 | | |
534 | 535 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
405 | 405 | | |
406 | 406 | | |
407 | 407 | | |
| 408 | + | |
408 | 409 | | |
409 | 410 | | |
410 | 411 | | |
| |||
417 | 418 | | |
418 | 419 | | |
419 | 420 | | |
| 421 | + | |
420 | 422 | | |
421 | 423 | | |
422 | 424 | | |
| |||
426 | 428 | | |
427 | 429 | | |
428 | 430 | | |
| 431 | + | |
429 | 432 | | |
430 | 433 | | |
431 | 434 | | |
| |||
467 | 470 | | |
468 | 471 | | |
469 | 472 | | |
470 | | - | |
| 473 | + | |
471 | 474 | | |
472 | 475 | | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
473 | 480 | | |
474 | 481 | | |
475 | 482 | | |
| |||
0 commit comments