react-native-gesture-handler icon indicating copy to clipboard operation
react-native-gesture-handler copied to clipboard

enabled = false has no effect during tests

Open Andarius opened this issue 2 years ago • 3 comments

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

Andarius avatar Jan 20 '23 14:01 Andarius

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.

github-actions[bot] avatar Jan 20 '23 14:01 github-actions[bot]

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?

github-actions[bot] avatar Jan 20 '23 14:01 github-actions[bot]

Same issue here. onPress fires no matter if enabled or not.

versten1uk avatar Jun 20 '24 10:06 versten1uk

Hi! Could you please check if #3062 fixes this?

m-bert avatar Aug 23 '24 07:08 m-bert