Skip to content

Commit 060e085

Browse files
committed
remove getState from public API
1 parent b08877a commit 060e085

3 files changed

Lines changed: 24 additions & 24 deletions

File tree

packages/docs-gesture-handler/docs/guides/testing.mdx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,8 @@ The controller exposes the following methods:
100100
| `end()` | Ends a begun or active stream. Calls `onDeactivate` if active, then `onFinalize` with `canceled: false`. |
101101
| `fail()` | Fails a begun or active stream. Calls `onDeactivate` if active, then `onFinalize` with `canceled: true`. |
102102
| `cancel()` | Cancels a begun or active stream. Calls `onDeactivate` if active, then `onFinalize` with `canceled: true`. |
103-
| `getState()` | Returns the controller's current state without dispatching an event. |
104103

105-
After `end()`, `fail()`, or `cancel()`, the terminal state remains available through
106-
`getState()`. Calling `begin()` again starts another stream with the same controller.
104+
Calling `begin()` again after `end()`, `fail()`, or `cancel()` starts another stream with the same controller.
107105
Calling methods in an invalid order throws an error. State-machine fields such as
108106
`state`, `oldState`, `handlerTag`, and `nativeEvent` cannot be supplied in event
109107
payloads because the controller manages them internally.

packages/react-native-gesture-handler/src/__tests__/gestureController.test.tsx

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ function assertGestureControllerTypes(pan: PanGesture, tap: TapGesture) {
5858

5959
void assertGestureControllerTypes;
6060

61+
function getControllerState(gesture: object): State {
62+
return (gesture as { state: State }).state;
63+
}
64+
6165
describe('createGestureController', () => {
62-
test('allows assertions after each gesture lifecycle step', () => {
66+
test('transitions to the expected state after each gesture lifecycle step', () => {
6367
const { order, callbacks } = mockedContinuousCallbacks();
6468
const pan = renderHook(() =>
6569
usePanGesture({ disableReanimated: true, ...callbacks })
@@ -68,38 +72,38 @@ describe('createGestureController', () => {
6872

6973
gesture.begin({ translationX: 0 });
7074

71-
expect(gesture.getState()).toBe(State.BEGAN);
7275
expect(order).toEqual(['onBegin']);
76+
expect(getControllerState(gesture)).toBe(State.BEGAN);
7377
expect(callbacks.onBegin).toHaveBeenCalledWith(
7478
expect.objectContaining({ translationX: 0 })
7579
);
7680

7781
gesture.activate({ translationX: 10 });
7882

79-
expect(gesture.getState()).toBe(State.ACTIVE);
8083
expect(order).toEqual(['onBegin', 'onActivate']);
84+
expect(getControllerState(gesture)).toBe(State.ACTIVE);
8185
expect(callbacks.onActivate).toHaveBeenCalledWith(
8286
expect.objectContaining({ translationX: 10 })
8387
);
8488

8589
gesture.update({ translationX: 50 });
8690

87-
expect(gesture.getState()).toBe(State.ACTIVE);
8891
expect(order).toEqual(['onBegin', 'onActivate', 'onUpdate']);
92+
expect(getControllerState(gesture)).toBe(State.ACTIVE);
8993
expect(callbacks.onUpdate).toHaveBeenCalledWith(
9094
expect.objectContaining({ translationX: 50 })
9195
);
9296

9397
gesture.end({ translationX: 50 });
9498

95-
expect(gesture.getState()).toBe(State.END);
9699
expect(order).toEqual([
97100
'onBegin',
98101
'onActivate',
99102
'onUpdate',
100103
'onDeactivate',
101104
'onFinalize',
102105
]);
106+
expect(getControllerState(gesture)).toBe(State.END);
103107
expect(callbacks.onFinalize).toHaveBeenCalledWith(
104108
expect.objectContaining({ canceled: false, translationX: 50 })
105109
);
@@ -115,8 +119,8 @@ describe('createGestureController', () => {
115119
gesture.begin({ x: 10 });
116120
gesture.fail({ x: 10 });
117121

118-
expect(gesture.getState()).toBe(State.FAILED);
119122
expect(order).toEqual(['onBegin', 'onFinalize']);
123+
expect(getControllerState(gesture)).toBe(State.FAILED);
120124
expect(callbacks.onActivate).not.toHaveBeenCalled();
121125
expect(callbacks.onDeactivate).not.toHaveBeenCalled();
122126
expect(callbacks.onFinalize).toHaveBeenCalledWith(
@@ -180,33 +184,36 @@ describe('createGestureController', () => {
180184
gesture.begin();
181185
gesture[terminalAction]();
182186

183-
expect(gesture.getState()).toBe(terminalState);
187+
expect(getControllerState(gesture)).toBe(terminalState);
184188

185189
gesture.begin();
186190

187-
expect(gesture.getState()).toBe(State.BEGAN);
191+
expect(getControllerState(gesture)).toBe(State.BEGAN);
188192

189193
gesture[terminalAction]();
190194

191-
expect(gesture.getState()).toBe(terminalState);
195+
expect(getControllerState(gesture)).toBe(terminalState);
192196
expect(onBegin).toHaveBeenCalledTimes(2);
193197
expect(onFinalize).toHaveBeenCalledTimes(2);
194198
}
195199
);
196200

197201
test('rejects raw state-machine fields in event payloads', () => {
198-
const tap = renderHook(() => useTapGesture({ disableReanimated: true }))
199-
.result.current;
202+
const onBegin = jest.fn();
203+
const tap = renderHook(() =>
204+
useTapGesture({ disableReanimated: true, onBegin })
205+
).result.current;
200206
const gesture = createGestureController(tap);
201207

202208
expect(() => gesture.begin({ state: State.BEGAN } as never)).toThrow(
203209
"createGestureController manages 'state' internally."
204210
);
205-
expect(gesture.getState()).toBe(State.UNDETERMINED);
211+
expect(getControllerState(gesture)).toBe(State.UNDETERMINED);
212+
expect(onBegin).not.toHaveBeenCalled();
206213

207214
gesture.begin();
208215

209-
expect(gesture.getState()).toBe(State.BEGAN);
216+
expect(getControllerState(gesture)).toBe(State.BEGAN);
210217
});
211218

212219
test('uses the latest callbacks after rerender', () => {
@@ -241,13 +248,13 @@ describe('createGestureController', () => {
241248
hook.rerender({ enabled: false });
242249
gesture.begin();
243250

244-
expect(gesture.getState()).toBe(State.UNDETERMINED);
251+
expect(getControllerState(gesture)).toBe(State.UNDETERMINED);
245252
expect(onBegin).not.toHaveBeenCalled();
246253

247254
hook.rerender({ enabled: true });
248255
gesture.begin();
249256

250-
expect(gesture.getState()).toBe(State.BEGAN);
257+
expect(getControllerState(gesture)).toBe(State.BEGAN);
251258
expect(onBegin).toHaveBeenCalledTimes(1);
252259
});
253260

@@ -267,7 +274,7 @@ describe('createGestureController', () => {
267274
gesture.update({ translationX: 50 });
268275
gesture.end();
269276

270-
expect(gesture.getState()).toBe(State.UNDETERMINED);
277+
expect(getControllerState(gesture)).toBe(State.UNDETERMINED);
271278
expect(order).toEqual([]);
272279
});
273280
});

packages/react-native-gesture-handler/src/jestUtils/jestUtils.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,6 @@ export interface GestureController<
528528
end: (event?: GestureControllerEvent<TEventPayload>) => void;
529529
fail: (event?: GestureControllerEvent<TEventPayload>) => void;
530530
cancel: (event?: GestureControllerEvent<TEventPayload>) => void;
531-
getState: () => State;
532531
}
533532

534533
const FORBIDDEN_CONTROLLER_EVENT_FIELDS = [
@@ -585,10 +584,6 @@ class GestureControllerImpl<
585584
// eslint-disable-next-line no-useless-constructor
586585
constructor(private resolveHandlerData: () => HandlerData) {}
587586

588-
public getState() {
589-
return this.state;
590-
}
591-
592587
public begin(event: GestureControllerEvent<TEventPayload> = {}) {
593588
const handlerData = this.resolveHandlerData();
594589

0 commit comments

Comments
 (0)