Skip to content

Forward press handlers as testOnly_* in PressableWithTouchable - #4416

Open
huextrat wants to merge 1 commit into
software-mansion:mainfrom
huextrat:fix/pressable-with-touchable-testonly-handlers
Open

Forward press handlers as testOnly_* in PressableWithTouchable#4416
huextrat wants to merge 1 commit into
software-mansion:mainfrom
huextrat:fix/pressable-with-touchable-testonly-handlers

Conversation

@huextrat

@huextrat huextrat commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #4417.

Since 3.2.0, Pressable dispatches between two engines: StatefulPressable when one of the
relation props (simultaneousWith / requireToFail / block) is passed, and
PressableWithTouchable otherwise — the common case.

Only StatefulPressable sets testOnly_onPress / testOnly_onPressIn / testOnly_onPressOut /
testOnly_onLongPress on the button. PressableWithTouchable doesn't, so for a plain
<Pressable onPress={...}> React Native Testing Library finds no handler and
fireEvent(element, 'press') silently does nothing.

The press state machine runs natively, so in a test environment those props are the only way to
reach the handlers. This makes the regression silent and fairly wide-reaching: no error is
thrown, tests just stop observing presses. On 3.1.0 the single v3 Pressable always forwarded
them, so this is a 3.1.0 → 3.2.0 regression for any app whose Jest suite drives a Pressable.

The fix forwards the four handlers from PressableWithTouchable too, guarded by isTestEnv(),
exactly as StatefulPressable does. Touchable spreads its remaining props onto
GestureHandlerButton, which already declares them in ButtonProps, so nothing else needed to
change and the props stay stripped outside a test environment.

Note this is unrelated to #4414, which fixed the testOnly_pressed display state.

Test plan

Added a regression test in src/__tests__/mocks.test.tsx asserting the four testOnly_* props
are wired on the button for a relation-free Pressable. It fails on main and passes with the
fix.

The test asserts the props rather than calling fireEvent(element, 'press') because this repo is
on @testing-library/react-native@12.9, which predates the testOnly_* handler lookup (added in
v13). Asserting the props keeps the test meaningful on the pinned version and independent of the
RNTL version.

In packages/react-native-gesture-handler:

  • yarn test — 16 suites, 135 tests passing
  • yarn ts-check — clean
  • yarn lint:js — 0 errors
  • yarn format:js — clean

Also verified end-to-end in a real app (Expo 57 / RN 0.86, RNTL 14.0.1) that had ~3 suites broken
by the 3.1.0 → 3.2.0 bump: with this change applied as a patch, the full suite is green again
(136 suites, 1663 tests, 73 snapshots).

`Pressable` dispatches to `StatefulPressable` when a relation prop is
passed and to `PressableWithTouchable` otherwise. Only the former sets
`testOnly_onPress`/`onPressIn`/`onPressOut`/`onLongPress` on the button,
so in the common case (no relation props) React Native Testing Library
finds no handler and `fireEvent(element, 'press')` silently does
nothing.

The press state machine runs natively, so a test environment has no
other way to reach the handlers. Forward them from
`PressableWithTouchable` too, guarded by `isTestEnv()`, exactly as
`StatefulPressable` does.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Copilot AI lite review requested due to automatic review settings August 13, 2026 16:18
@coderabbitai

coderabbitai Bot commented Aug 13, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 729f145e-2fa8-4eb7-9c04-6c3ef95414ab

📥 Commits

Reviewing files that changed from the base of the PR and between 44046a6 and 5a40375.

📒 Files selected for processing (2)
  • packages/react-native-gesture-handler/src/__tests__/mocks.test.tsx
  • packages/react-native-gesture-handler/src/v3/components/PressableWithTouchable.tsx

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Improved test-environment handling for press interactions.
    • Ensured Pressable correctly forwards press, long-press, press-in, and press-out callbacks during testing.
  • Tests

    • Added coverage verifying that all supported press handlers are forwarded correctly.

Walkthrough

Changes

Pressable test callback forwarding

Layer / File(s) Summary
Environment-gated callback forwarding
packages/react-native-gesture-handler/src/v3/components/PressableWithTouchable.tsx
PressableWithTouchable forwards press, press-in, press-out, and long-press handlers through test-only Touchable props when isTestEnv is true.
Callback forwarding validation
packages/react-native-gesture-handler/src/__tests__/mocks.test.tsx
The test renders Pressable with four handlers and verifies the corresponding testOnly_* props on the rendered button.

Possibly related PRs

Suggested reviewers: m-bert

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely describes the main change: forwarding press handlers as testOnly_* props in PressableWithTouchable.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a testing regression in the v3 Pressable implementation where the common (relation-free) engine (PressableWithTouchable) did not forward press handlers via testOnly_* props, preventing React Native Testing Library from reaching user handlers in a Jest environment.

Changes:

  • Forward onPress / onPressIn / onPressOut / onLongPress from PressableWithTouchable onto the underlying button as testOnly_* props, gated by isTestEnv().
  • Add a Jest regression test asserting these testOnly_* props are present for a relation-free v3 Pressable.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
packages/react-native-gesture-handler/src/v3/components/PressableWithTouchable.tsx Adds isTestEnv() gating and forwards testOnly_* handler props in the Touchable-based v3 Pressable engine.
packages/react-native-gesture-handler/src/tests/mocks.test.tsx Adds a regression test verifying v3 Pressable exposes the four testOnly_* handler props on the rendered button.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fireEvent(element, 'press') is a no-op on a relation-free Pressable since 3.2.0

2 participants