diff --git a/packages/react-native-gesture-handler/src/__tests__/mocks.test.tsx b/packages/react-native-gesture-handler/src/__tests__/mocks.test.tsx index 3ab5541eb5..dd595358b7 100644 --- a/packages/react-native-gesture-handler/src/__tests__/mocks.test.tsx +++ b/packages/react-native-gesture-handler/src/__tests__/mocks.test.tsx @@ -221,3 +221,65 @@ test('StatefulPressable exposes its press and hover handlers to testing-library' expect(button.props.testOnly_onHoverIn).toBe(onHoverIn); expect(button.props.testOnly_onHoverOut).toBe(onHoverOut); }); + +test.each([ + ['Pressable', {}], + // A relation prop routes `Pressable` to the `StatefulPressable` engine. + ['StatefulPressable', { simultaneousWith: [] }], +])( + 'disabled %s fires none of its callbacks through testing-library', + (_engine, engineProps) => { + // On a device, `disabled` turns the native button off, so none of these + // callbacks fire. Testing-library calls the button's `testOnly_*` props in + // place of the native events, and falls back to handlers further up the + // tree when one is missing. So each of them has to be there and do + // nothing, even when called without an event, as a bare + // `fireEvent(button, 'pressIn')` does. + const callbacks = { + onPress: jest.fn(), + onPressIn: jest.fn(), + onPressOut: jest.fn(), + onLongPress: jest.fn(), + onHoverIn: jest.fn(), + onHoverOut: jest.fn(), + }; + + const { rerender } = render( + + + Press Me + + + ); + + const { props } = screen.getByTestId('pressable'); + + for (const name of Object.keys(callbacks)) { + const handler: unknown = props[`testOnly_${name}`]; + + expect(handler).toEqual(expect.any(Function)); + expect(() => (handler as () => void)()).not.toThrow(); + } + + for (const callback of Object.values(callbacks)) { + expect(callback).not.toHaveBeenCalled(); + } + + // Callbacks that weren't given stay unset, as on an enabled button, so + // testing-library keeps looking further up the tree, e.g. for an enabled + // `Pressable` around this one. + rerender( + + + Press Me + + + ); + + const { props: propsWithoutCallbacks } = screen.getByTestId('pressable'); + + for (const name of Object.keys(callbacks)) { + expect(propsWithoutCallbacks[`testOnly_${name}`]).toBeUndefined(); + } + } +); diff --git a/packages/react-native-gesture-handler/src/v3/components/PressableWithTouchable.tsx b/packages/react-native-gesture-handler/src/v3/components/PressableWithTouchable.tsx index 2109c14fc8..4a1bd8a57e 100644 --- a/packages/react-native-gesture-handler/src/v3/components/PressableWithTouchable.tsx +++ b/packages/react-native-gesture-handler/src/v3/components/PressableWithTouchable.tsx @@ -18,6 +18,7 @@ import { pointerStyle } from './pointerStyle'; import { Touchable } from './Touchable/Touchable'; const IS_TEST_ENV = isTestEnv(); +const noop = () => undefined; // RN's Pressable default. Touchable's own default is 600ms const DEFAULT_LONG_PRESS_DURATION = 500; @@ -306,6 +307,15 @@ const PressableWithTouchable = (props: PressableProps) => { ? children({ pressed: displayPressed }) : children; + // Testing-library calls the `testOnly_*` props in place of the native press + // events. A disabled button never fires them, so each callback given is + // swapped for a no-op rather than dropped: an unset prop would let + // testing-library fall back to handlers further up the tree, like the ones + // `Touchable` gets below, and those can still run. Callbacks that weren't + // given stay unset, as on an enabled button. + const testOnlyHandler = (handler: T) => + IS_TEST_ENV ? (handler && disabled === true ? noop : handler) : undefined; + return ( { onLongPress={handleLongPress} onHoverIn={handleHoverIn} onHoverOut={handleHoverOut} - testOnly_onPress={IS_TEST_ENV ? onPress : undefined} - testOnly_onPressIn={IS_TEST_ENV ? onPressIn : undefined} - testOnly_onPressOut={IS_TEST_ENV ? onPressOut : undefined} - testOnly_onLongPress={IS_TEST_ENV ? onLongPress : undefined} - testOnly_onHoverIn={IS_TEST_ENV ? onHoverIn : undefined} - testOnly_onHoverOut={IS_TEST_ENV ? onHoverOut : undefined}> + testOnly_onPress={testOnlyHandler(onPress)} + testOnly_onPressIn={testOnlyHandler(onPressIn)} + testOnly_onPressOut={testOnlyHandler(onPressOut)} + testOnly_onLongPress={testOnlyHandler(onLongPress)} + testOnly_onHoverIn={testOnlyHandler(onHoverIn)} + testOnly_onHoverOut={testOnlyHandler(onHoverOut)}> {resolvedChildren} {__DEV__ ? ( diff --git a/packages/react-native-gesture-handler/src/v3/components/StatefulPressable.tsx b/packages/react-native-gesture-handler/src/v3/components/StatefulPressable.tsx index 765c1b25de..bd004cd219 100644 --- a/packages/react-native-gesture-handler/src/v3/components/StatefulPressable.tsx +++ b/packages/react-native-gesture-handler/src/v3/components/StatefulPressable.tsx @@ -51,6 +51,7 @@ import { PureNativeButton } from './GestureButtons'; const DEFAULT_LONG_PRESS_DURATION = 500; const IS_TEST_ENV = isTestEnv(); +const noop = () => undefined; const StatefulPressable = (props: PressableProps) => { const { @@ -461,6 +462,14 @@ const StatefulPressable = (props: PressableProps) => { const tvProps = getTVProps(remainingProps); + // Testing-library calls the `testOnly_*` props in place of the native press + // events. A disabled button never fires them, so each callback given is + // swapped for a no-op rather than dropped: an unset prop would let + // testing-library fall back to handlers further up the tree. Callbacks that + // weren't given stay unset, as on an enabled button. + const testOnlyHandler = (handler: T) => + IS_TEST_ENV ? (handler && disabled === true ? noop : handler) : undefined; + return ( { borderless={android_ripple?.borderless ?? undefined} foreground={android_ripple?.foreground ?? undefined} style={[pointerStyle, styleProp]} - testOnly_onPress={IS_TEST_ENV ? onPress : undefined} - testOnly_onPressIn={IS_TEST_ENV ? onPressIn : undefined} - testOnly_onPressOut={IS_TEST_ENV ? onPressOut : undefined} - testOnly_onLongPress={IS_TEST_ENV ? onLongPress : undefined} - testOnly_onHoverIn={IS_TEST_ENV ? onHoverIn : undefined} - testOnly_onHoverOut={IS_TEST_ENV ? onHoverOut : undefined}> + testOnly_onPress={testOnlyHandler(onPress)} + testOnly_onPressIn={testOnlyHandler(onPressIn)} + testOnly_onPressOut={testOnlyHandler(onPressOut)} + testOnly_onLongPress={testOnlyHandler(onLongPress)} + testOnly_onHoverIn={testOnlyHandler(onHoverIn)} + testOnly_onHoverOut={testOnlyHandler(onHoverOut)}> {childrenProp} {__DEV__ ? (