Add Imperative Gesture Handler Testing API#4309
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces an imperative testing helper (createGestureController) to simplify gesture lifecycle testing in RNGH Jest utilities, enabling step-by-step state transitions and assertions without manually crafting low-level state/oldState events.
Changes:
- Added
createGestureController,GestureController, andGestureControllerEventto drive gesture lifecycles imperatively in tests. - Exported the new controller API from
src/jestUtils/index.ts. - Added Jest tests covering lifecycle sequencing, validation, testID resolution, and disabled-gesture behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/react-native-gesture-handler/src/jestUtils/jestUtils.ts | Adds the gesture controller implementation and public API surface. |
| packages/react-native-gesture-handler/src/jestUtils/index.ts | Re-exports the controller types and factory function. |
| packages/react-native-gesture-handler/src/tests/gestureController.test.tsx | Adds tests validating the new imperative controller behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| expect.objectContaining({ translationX: 50 }) | ||
| ); | ||
|
|
||
| controller.end(); |
There was a problem hiding this comment.
I assume this will also call onDeactivate in this scenario, right?
| | `end()` | Ends a begun or active stream. Calls `onDeactivate` if active, then `onFinalize` with `canceled: false`. | | ||
| | `fail()` | Fails a begun or active stream. Calls `onDeactivate` if active, then `onFinalize` with `canceled: true`. | | ||
| | `cancel()` | Cancels a begun or active stream. Calls `onDeactivate` if active, then `onFinalize` with `canceled: true`. | | ||
| | `getState()` | Returns the controller's current state without dispatching an event. | |
There was a problem hiding this comment.
Do we need this when the internal states are no longer exposed to users from the hooks API?
There was a problem hiding this comment.
I'd go even further. If the user imperatively manipulates gesture states by calling begin(), etc., then do we even need to check state? If we don't support relations then we know exactly at which state we are after calling given lifecycle method. Or have I missed something?
| ```ts | ||
| import { createGestureController } from 'react-native-gesture-handler/jest-utils'; | ||
|
|
||
| createGestureController: (componentOrGesture) => GestureController; |
There was a problem hiding this comment.
Does GestureController work with older APIs or only V3?
| const gesture = hookGestures.get(handlerTag); | ||
|
|
||
| if (gesture && isTestEnv() && gesture.config.testID) { | ||
| if (gesture?.config.testID) { |
There was a problem hiding this comment.
| if (gesture?.config.testID) { | |
| if (gesture?.config?.testID) { |
| for (const field of FORBIDDEN_CONTROLLER_EVENT_FIELDS) { | ||
| invariant( | ||
| !hasProperty(event, field), | ||
| `createGestureController manages '${field}' internally. Pass only gesture event payload fields.` |
There was a problem hiding this comment.
| `createGestureController manages '${field}' internally. Pass only gesture event payload fields.` | |
| `GestureController manages '${field}' internally. Pass only gesture event payload fields.` |
| } | ||
|
|
||
| if (isHookGesture(target)) { | ||
| return findGesture(target.handlerTag) ?? target; |
There was a problem hiding this comment.
What's the case where findGesture(target.handlerTag) returns something else than target
| return; | ||
| } | ||
|
|
||
| this.resetIfFinished(); |
There was a problem hiding this comment.
What if not finished? I.e. begin called on a gesture in ACTIVE state.
|
|
||
| const tapController = createGestureController(tap); | ||
| tapController.begin({ x: 1 }); | ||
| // @ts-expect-error tap payloads do not include pan translation fields |
There was a problem hiding this comment.
This is only a type error, right? Will runtime allow this?
| the gesture. Every lifecycle method accepts an optional partial event payload and | ||
| fills omitted handler-specific properties with defaults. | ||
|
|
||
| The controller exposes these methods: |
There was a problem hiding this comment.
| The controller exposes these methods: | |
| The controller exposes the following methods: |
| | `end()` | Ends a begun or active stream. Calls `onDeactivate` if active, then `onFinalize` with `canceled: false`. | | ||
| | `fail()` | Fails a begun or active stream. Calls `onDeactivate` if active, then `onFinalize` with `canceled: true`. | | ||
| | `cancel()` | Cancels a begun or active stream. Calls `onDeactivate` if active, then `onFinalize` with `canceled: true`. | | ||
| | `getState()` | Returns the controller's current state without dispatching an event. | |
There was a problem hiding this comment.
I'd go even further. If the user imperatively manipulates gesture states by calling begin(), etc., then do we even need to check state? If we don't support relations then we know exactly at which state we are after calling given lifecycle method. Or have I missed something?
| return; | ||
| } | ||
|
|
||
| hookGestures.set(handlerTag, gesture); |
There was a problem hiding this comment.
Do we want to remove the necessity of testID?
Description
The goal is to make gesture lifecycle tests easier to write when we want to assert application state after each gesture step, without requiring users to manually construct RNGH state events with
state,oldState, orhandlerTag.Instead of this lower-level style:
tests can now use imperative controller:
Hook gesture rerenders
Hook-based gestures can be recreated when their callbacks or configuration change during a React rerender while retaining the same handler tag. The controller now resolves the latest registered gesture before every lifecycle operation. This ensures that subsequent steps use the newest callback closures and configuration, including the current enabled value.
For example, if a rerender occurs between
begin()andactivate(),activate()invokes the callback from the latest render rather than the callback captured when the controller was created.Reusing a controller for another stream
A controller can now run multiple gesture streams.
After
end(),fail(), orcancel(), the terminal state remains available for assertions throughgetState(). Callingbegin()again resets the finished controller internally and starts a new stream.Test plan
Added tests using new API.