From 2a7a9affa6267ed35e46d4512dee9ce3448bb520 Mon Sep 17 00:00:00 2001 From: maykonmichel Date: Wed, 4 Nov 2026 11:16:55 -0800 Subject: [PATCH 1/3] [Jest] Don't expose `testOnly_*` handlers on a disabled `Pressable` In a test environment both v3 `Pressable` engines forward their callbacks to the button as `testOnly_*` props, which testing-library (>= 13) calls from `fireEvent`/`userEvent`. They were forwarded regardless of `disabled`, so pressing a disabled `Pressable` in a test called `onPress` even though the disabled native button never fires it on a device. Withhold the `testOnly_*` handlers while `disabled`. --- .../src/__tests__/mocks.test.tsx | 50 +++++++++++++++++++ .../v3/components/PressableWithTouchable.tsx | 16 +++--- .../src/v3/components/StatefulPressable.tsx | 16 +++--- 3 files changed, 70 insertions(+), 12 deletions(-) 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..b44cfc570f 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,53 @@ 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); }); + +// Fresh mocks for every callback `Pressable` forwards as a `testOnly_*` prop. +const pressableCallbacks = () => ({ + onPress: jest.fn(), + onPressIn: jest.fn(), + onPressOut: jest.fn(), + onLongPress: jest.fn(), + onHoverIn: jest.fn(), + onHoverOut: jest.fn(), +}); + +// Names of the `testOnly_*` handlers testing-library could call on the element. +function exposedTestOnlyHandlers(testID: string) { + const { props } = screen.getByTestId(testID); + + return Object.keys(props).filter( + (key) => key.startsWith('testOnly_on') && typeof props[key] === 'function' + ); +} + +test('disabled Pressable does not expose its handlers to testing-library', () => { + // On a device, `disabled` turns the native button off, so none of these + // callbacks fire. The `testOnly_*` props have to follow suit, otherwise + // `fireEvent(element, 'press')` still calls `onPress` in a test environment. + render( + + + Press Me + + + ); + + expect(exposedTestOnlyHandlers('pressable')).toEqual([]); +}); + +test('disabled StatefulPressable does not expose its handlers to testing-library', () => { + render( + + + Press Me + + + ); + + expect(exposedTestOnlyHandlers('pressable')).toEqual([]); +}); 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..1ec046b978 100644 --- a/packages/react-native-gesture-handler/src/v3/components/PressableWithTouchable.tsx +++ b/packages/react-native-gesture-handler/src/v3/components/PressableWithTouchable.tsx @@ -306,6 +306,10 @@ const PressableWithTouchable = (props: PressableProps) => { ? children({ pressed: displayPressed }) : children; + // The callbacks reach testing-library through the `testOnly_*` props. A + // disabled button never fires them, so they're withheld while disabled. + const exposeTestOnlyHandlers = IS_TEST_ENV && disabled !== true; + 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={exposeTestOnlyHandlers ? onPress : undefined} + testOnly_onPressIn={exposeTestOnlyHandlers ? onPressIn : undefined} + testOnly_onPressOut={exposeTestOnlyHandlers ? onPressOut : undefined} + testOnly_onLongPress={exposeTestOnlyHandlers ? onLongPress : undefined} + testOnly_onHoverIn={exposeTestOnlyHandlers ? onHoverIn : undefined} + testOnly_onHoverOut={exposeTestOnlyHandlers ? onHoverOut : undefined}> {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..cb08e15687 100644 --- a/packages/react-native-gesture-handler/src/v3/components/StatefulPressable.tsx +++ b/packages/react-native-gesture-handler/src/v3/components/StatefulPressable.tsx @@ -461,6 +461,10 @@ const StatefulPressable = (props: PressableProps) => { const tvProps = getTVProps(remainingProps); + // The callbacks reach testing-library through the `testOnly_*` props. A + // disabled button never fires them, so they're withheld while disabled. + const exposeTestOnlyHandlers = IS_TEST_ENV && disabled !== true; + 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={exposeTestOnlyHandlers ? onPress : undefined} + testOnly_onPressIn={exposeTestOnlyHandlers ? onPressIn : undefined} + testOnly_onPressOut={exposeTestOnlyHandlers ? onPressOut : undefined} + testOnly_onLongPress={exposeTestOnlyHandlers ? onLongPress : undefined} + testOnly_onHoverIn={exposeTestOnlyHandlers ? onHoverIn : undefined} + testOnly_onHoverOut={exposeTestOnlyHandlers ? onHoverOut : undefined}> {childrenProp} {__DEV__ ? ( From 525644975ecf66a31f4ddac85b310909e4f4eb1a Mon Sep 17 00:00:00 2001 From: maykonmichel Date: Wed, 4 Nov 2026 12:14:14 -0800 Subject: [PATCH 2/3] [Jest] Give a disabled `Pressable` no-op `testOnly_*` handlers Withholding the `testOnly_*` handlers of a disabled `Pressable` let testing-library (13.1+) fall back to handlers further up the tree. For `PressableWithTouchable` those are the ones `Touchable` gets: a bare `fireEvent(button, 'pressIn')` threw reading `event.x`, and a `pressIn` followed by `press` within `hitSlop` still fired `onPressIn` and `onPress`. Give a disabled button no-op `testOnly_*` handlers instead, so testing-library stops at the button. The handlers `Touchable` gets stay as they are, since the native button still reports press-out and hover-out when it gets disabled mid-interaction. --- .../src/__tests__/mocks.test.tsx | 93 +++++++++---------- .../v3/components/PressableWithTouchable.tsx | 22 +++-- .../src/v3/components/StatefulPressable.tsx | 21 +++-- 3 files changed, 69 insertions(+), 67 deletions(-) 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 b44cfc570f..901c4a2c59 100644 --- a/packages/react-native-gesture-handler/src/__tests__/mocks.test.tsx +++ b/packages/react-native-gesture-handler/src/__tests__/mocks.test.tsx @@ -222,52 +222,47 @@ test('StatefulPressable exposes its press and hover handlers to testing-library' expect(button.props.testOnly_onHoverOut).toBe(onHoverOut); }); -// Fresh mocks for every callback `Pressable` forwards as a `testOnly_*` prop. -const pressableCallbacks = () => ({ - onPress: jest.fn(), - onPressIn: jest.fn(), - onPressOut: jest.fn(), - onLongPress: jest.fn(), - onHoverIn: jest.fn(), - onHoverOut: jest.fn(), -}); - -// Names of the `testOnly_*` handlers testing-library could call on the element. -function exposedTestOnlyHandlers(testID: string) { - const { props } = screen.getByTestId(testID); - - return Object.keys(props).filter( - (key) => key.startsWith('testOnly_on') && typeof props[key] === 'function' - ); -} - -test('disabled Pressable does not expose its handlers to testing-library', () => { - // On a device, `disabled` turns the native button off, so none of these - // callbacks fire. The `testOnly_*` props have to follow suit, otherwise - // `fireEvent(element, 'press')` still calls `onPress` in a test environment. - render( - - - Press Me - - - ); - - expect(exposedTestOnlyHandlers('pressable')).toEqual([]); -}); - -test('disabled StatefulPressable does not expose its handlers to testing-library', () => { - render( - - - Press Me - - - ); - - expect(exposedTestOnlyHandlers('pressable')).toEqual([]); -}); +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(), + }; + + 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(); + } + } +); 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 1ec046b978..5c92a3419e 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,9 +307,12 @@ const PressableWithTouchable = (props: PressableProps) => { ? children({ pressed: displayPressed }) : children; - // The callbacks reach testing-library through the `testOnly_*` props. A - // disabled button never fires them, so they're withheld while disabled. - const exposeTestOnlyHandlers = IS_TEST_ENV && disabled !== true; + // Testing-library calls the `testOnly_*` props in place of the native press + // events. A disabled button never fires them, so it gets no-ops. Unset props + // would let testing-library fall back to handlers further up the tree, like + // the ones `Touchable` gets below, and those can still run. + const testOnlyHandler = (handler: T) => + IS_TEST_ENV ? (disabled === true ? noop : handler) : undefined; return ( { onLongPress={handleLongPress} onHoverIn={handleHoverIn} onHoverOut={handleHoverOut} - testOnly_onPress={exposeTestOnlyHandlers ? onPress : undefined} - testOnly_onPressIn={exposeTestOnlyHandlers ? onPressIn : undefined} - testOnly_onPressOut={exposeTestOnlyHandlers ? onPressOut : undefined} - testOnly_onLongPress={exposeTestOnlyHandlers ? onLongPress : undefined} - testOnly_onHoverIn={exposeTestOnlyHandlers ? onHoverIn : undefined} - testOnly_onHoverOut={exposeTestOnlyHandlers ? 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 cb08e15687..34ef95359e 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,9 +462,11 @@ const StatefulPressable = (props: PressableProps) => { const tvProps = getTVProps(remainingProps); - // The callbacks reach testing-library through the `testOnly_*` props. A - // disabled button never fires them, so they're withheld while disabled. - const exposeTestOnlyHandlers = IS_TEST_ENV && disabled !== true; + // Testing-library calls the `testOnly_*` props in place of the native press + // events. A disabled button never fires them, so it gets no-ops. Unset props + // would let testing-library fall back to handlers further up the tree. + const testOnlyHandler = (handler: T) => + IS_TEST_ENV ? (disabled === true ? noop : handler) : undefined; return ( @@ -481,12 +484,12 @@ const StatefulPressable = (props: PressableProps) => { borderless={android_ripple?.borderless ?? undefined} foreground={android_ripple?.foreground ?? undefined} style={[pointerStyle, styleProp]} - testOnly_onPress={exposeTestOnlyHandlers ? onPress : undefined} - testOnly_onPressIn={exposeTestOnlyHandlers ? onPressIn : undefined} - testOnly_onPressOut={exposeTestOnlyHandlers ? onPressOut : undefined} - testOnly_onLongPress={exposeTestOnlyHandlers ? onLongPress : undefined} - testOnly_onHoverIn={exposeTestOnlyHandlers ? onHoverIn : undefined} - testOnly_onHoverOut={exposeTestOnlyHandlers ? 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__ ? ( From ef9a7490081317e48d645293a7751e410077be5f Mon Sep 17 00:00:00 2001 From: maykonmichel Date: Wed, 4 Nov 2026 12:31:07 -0800 Subject: [PATCH 3/3] [Jest] Only swap the callbacks a disabled `Pressable` was given for no-ops Giving a disabled button a no-op for every `testOnly_*` prop also made testing-library (13.1+) stop at the button for events the `Pressable` has no callback for. A press on a disabled `Pressable` without `onPress` then no longer reached an enabled `Pressable` or `Touchable` around it, as it does on main and for an enabled button. Swap only the callbacks that were given. The rest stay unset, so the lookup continues as it does on main. --- .../src/__tests__/mocks.test.tsx | 19 ++++++++++++++++++- .../v3/components/PressableWithTouchable.tsx | 10 ++++++---- .../src/v3/components/StatefulPressable.tsx | 8 +++++--- 3 files changed, 29 insertions(+), 8 deletions(-) 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 901c4a2c59..dd595358b7 100644 --- a/packages/react-native-gesture-handler/src/__tests__/mocks.test.tsx +++ b/packages/react-native-gesture-handler/src/__tests__/mocks.test.tsx @@ -244,7 +244,7 @@ test.each([ onHoverOut: jest.fn(), }; - render( + const { rerender } = render( Press Me @@ -264,5 +264,22 @@ test.each([ 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 5c92a3419e..4a1bd8a57e 100644 --- a/packages/react-native-gesture-handler/src/v3/components/PressableWithTouchable.tsx +++ b/packages/react-native-gesture-handler/src/v3/components/PressableWithTouchable.tsx @@ -308,11 +308,13 @@ const PressableWithTouchable = (props: PressableProps) => { : children; // Testing-library calls the `testOnly_*` props in place of the native press - // events. A disabled button never fires them, so it gets no-ops. Unset props - // would let testing-library fall back to handlers further up the tree, like - // the ones `Touchable` gets below, and those can still run. + // 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 ? (disabled === true ? noop : handler) : undefined; + IS_TEST_ENV ? (handler && disabled === true ? noop : handler) : undefined; return ( { const tvProps = getTVProps(remainingProps); // Testing-library calls the `testOnly_*` props in place of the native press - // events. A disabled button never fires them, so it gets no-ops. Unset props - // would let testing-library fall back to handlers further up the tree. + // 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 ? (disabled === true ? noop : handler) : undefined; + IS_TEST_ENV ? (handler && disabled === true ? noop : handler) : undefined; return (