Skip to content

enabled = false has no effect during testsΒ #2385

@Andarius

Description

@Andarius

Description

Passing enable={false} to a RectButton will have no effect when running jest tests.
My guess is that since RectButton is using TouchableNativeFeedback (

const RectButton = TouchableNativeFeedback;
) 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions