react-native-gesture-handler
react-native-gesture-handler copied to clipboard
enabled = false has no effect during tests
Description
Passing enable={false} to a RectButton will have no effect when running jest tests.
My guess is that since RectButton is using TouchableNativeFeedback (https://github.com/software-mansion/react-native-gesture-handler/blob/f0868f7ccf678c947ef65519ebf97ae149a10289/src/mocks.ts#L34) that uses disabled instead, it has no effect.
A potential fix could be:
import React from 'react';
import { Pressable } from 'react-native';
import type { RectButtonProperties } from 'react-native-gesture-handler';
const RectButtonMock = (props: RectButtonProperties) => {
const { onPress, enabled, ...rest } = props;
return (
<Pressable
disabled={!enabled}
onPress={() => onPress && onPress(true)}
{...rest}
>
{props.children}
</Pressable>
);
};
jest.mock('react-native-gesture-handler', () => {
const actual = jest.requireActual('react-native-gesture-handler');
return {
...actual,
RectButton: RectButtonMock,
};
});
Steps to reproduce
describe.only('Testing disabled Button', () => {
it('onPress does not trigger', function () {
const onPress = jest.fn();
const { getByTestId } = render(
<RectButton testID="btn" onPress={onPress} enabled={false} />
);
const btn = getByTestId('btn');
expect(onPress).not.toHaveBeenCalled();
fireEvent.press(btn);
expect(onPress).not.toHaveBeenCalled(); // Will fail
});
});
Snack or a link to a repository
NA
Gesture Handler version
2.8.0
React Native version
0.70.5
Platforms
Android
JavaScript runtime
Hermes
Workflow
None
Architecture
None
Build type
None
Device
None
Device model
No response
Acknowledgements
Yes
Hey! 👋
It looks like you've omitted a few important sections from the issue template.
Please complete Snack or a link to a repository section.
Hey! 👋
The issue doesn't seem to contain a minimal reproduction.
Could you provide a snack or a link to a GitHub repository under your username that reproduces the problem?
Same issue here. onPress fires no matter if enabled or not.
Hi! Could you please check if #3062 fixes this?