Commit caf03a3
Add Imperative Gesture Handler Testing API (#4309)
## 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`,
or `handlerTag`.
Instead of this lower-level style:
```ts
fireGestureHandler(panGesture, [
{ oldState: State.UNDETERMINED, state: State.BEGAN },
{ oldState: State.BEGAN, state: State.ACTIVE },
{ oldState: State.ACTIVE, state: State.END },
]);
```
tests can now use imperative controller:
```ts
const gesture = createGestureController(panGesture);
gesture.begin();
expect(onBegin).toHaveBeenCalled();
gesture.activate();
expect(onActivate).toHaveBeenCalled();
gesture.update({ translationX: 50 });
expect(onUpdate).toHaveBeenCalledWith(
expect.objectContaining({ translationX: 50 })
);
gesture.end();
expect(onFinalize).toHaveBeenCalledWith(
expect.objectContaining({ canceled: false })
);
```
## 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()` and `activate()`,
`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()`, or `cancel()`, the terminal state remains
available for assertions through `getState()`. Calling `begin()` again
resets the finished controller internally and starts a new stream.
```ts
controller.begin();
controller.activate();
controller.end();
expect(controller.getState()).toBe(State.END);
controller.begin();
expect(controller.getState()).toBe(State.BEGAN);
```
## Test plan
Added tests using new API.
---------
Co-authored-by: Jakub Piasecki <jakubpiasecki67@gmail.com>
Co-authored-by: Michał Bert <63123542+m-bert@users.noreply.github.com>1 parent 8896a63 commit caf03a3
8 files changed
Lines changed: 731 additions & 17 deletions
File tree
- packages
- docs-gesture-handler/docs/guides
- react-native-gesture-handler/src
- __tests__
- handlers
- jestUtils
- v3/hooks/utils
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
66 | 178 | | |
67 | 179 | | |
68 | 180 | | |
| |||
133 | 245 | | |
134 | 246 | | |
135 | 247 | | |
136 | | - | |
| 248 | + | |
137 | 249 | | |
138 | 250 | | |
139 | 251 | | |
| |||
Lines changed: 4 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
69 | 72 | | |
70 | 73 | | |
71 | 74 | | |
| |||
0 commit comments