Skip to content

Commit 1094f92

Browse files
committed
Testing
1 parent 12abfeb commit 1094f92

1 file changed

Lines changed: 50 additions & 57 deletions

File tree

  • packages/docs-gesture-handler/docs/guides

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

Lines changed: 50 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,31 @@ Example:
2323

2424
## Testing Gestures' and Gesture handlers' callbacks
2525

26-
RNGH provides an API for triggering selected handlers:
26+
RNGH provides APIs, specifically [`fireGestureHandler`](#firegesturehandler) and [`getByGestureTestId`](#getbygesturetestid), to trigger selected handlers.
2727

28-
- [`fireGestureHandler(gestureOrHandler, eventList)`](/docs/guides/testing#firegesturehandlergestureorhandler-eventlist)
29-
- [`getByGestureTestId(testID)`](/docs/guides/testing#getbygesturetestidtestid)
28+
### fireGestureHandler
3029

31-
## fireGestureHandler(gestureOrHandler, eventList)
30+
```ts
31+
fireGestureHandler: (componentOrGesture, eventList) => void;
32+
```
3233

3334
Simulates one event stream (i.e. event sequence starting with `BEGIN` state and ending
3435
with one of `END`/`FAIL`/`CANCEL` states), calling appropriate callbacks associated with given gesture handler.
3536

36-
### Arguments
37-
38-
#### `gestureOrHandler`
39-
40-
Represents either:
41-
42-
1. Gesture handler component found by Jest queries (e.g. `getByTestId`)
43-
2. Gesture found by [`getByGestureTestId()`](/docs/guides/testing#getbygesturetestidtestid)
44-
45-
#### `eventList`
37+
- `componentOrGesture` - Either Gesture Handler component found by `Jest` queries (e.g. `getByTestId`) or Gesture found by [`getByGestureTestId()`](#getbygesturetestidtestid)
4638

47-
Event data passed to appropriate callback. RNGH fills event list if required
48-
data is missing using these rules:
39+
- `eventList` - Event data passed to appropriate callback. RNGH fills event list if required
40+
data is missing using these rules:
41+
- `oldState` is filled using state of the previous event. `BEGIN` events use
42+
`UNDETERMINED` value as previous event.
43+
- Events after first `ACTIVE` state can omit `state` field.
44+
- Handler specific data is filled (e.g. `numberOfTouches`, `x` fields) with
45+
defaults.
46+
- Missing `BEGIN` and `END` events are added with data copied from first and last
47+
passed event, respectively.
48+
- If first event don't have `state` field, the `ACTIVE` state is assumed.
4949

50-
1. `oldState` is filled using state of the previous event. `BEGIN` events use
51-
`UNDETERMINED` value as previous event.
52-
2. Events after first `ACTIVE` state can omit `state` field.
53-
3. Handler specific data is filled (e.g. `numberOfTouches`, `x` fields) with
54-
defaults.
55-
4. Missing `BEGIN` and `END` events are added with data copied from first and last
56-
passed event, respectively.
57-
5. If first event don't have `state` field, the `ACTIVE` state is assumed.
58-
59-
Some examples:
50+
Some `eventList` examples:
6051

6152
```jsx
6253
const oldStateFilled = [
@@ -86,44 +77,46 @@ const implicitBeginAndEnd = [
8677
const allImplicits = []; // 3 events, one BEGIN, one ACTIVE, one END with defaults.
8778
```
8879

89-
### Example
90-
91-
Extracted from RNGH tests, check `Events.test.tsx` for full implementation.
80+
### getByGestureTestId
9281

9382
```tsx
94-
it('sends events with additional data to handlers', () => {
95-
const panHandlers = mockedEventHandlers();
96-
render(<SingleHandler handlers={panHandlers} treatStartAsUpdate />);
97-
fireGestureHandler<PanGesture>(getByGestureTestId('pan'), [
98-
{ state: State.BEGAN, translationX: 0 },
99-
{ state: State.ACTIVE, translationX: 10 },
100-
{ translationX: 20 },
101-
{ translationX: 20 },
102-
{ state: State.END, translationX: 30 },
103-
]);
104-
105-
expect(panHandlers.active).toHaveBeenCalledTimes(3);
106-
expect(panHandlers.active).toHaveBeenLastCalledWith(
107-
expect.objectContaining({ translationX: 20 })
108-
);
109-
});
83+
getByGestureTestId: (testID: string) => Gesture;
11084
```
11185

112-
## getByGestureTestId(testID)
113-
114-
Returns opaque data type associated with gesture. Gesture is found via `testID` attribute in rendered
115-
components (see [`testID`](/docs/gestures/use-pan-gesture#testid)).
116-
117-
### Arguments
86+
Returns opaque data type associated with gesture. Gesture is found via [`testID`](/docs/gestures/use-pan-gesture#testid) attribute in rendered
87+
components.
11888

119-
#### `testID`
89+
- `testID` - String identifying gesture.
12090

121-
String identifying gesture.
91+
:::warning
92+
`testID` must be unique among components rendered in test.
93+
:::
12294

123-
### Notes
95+
## Example
12496

125-
`testID` must be unique among components rendered in test.
97+
Extracted from RNGH tests, check [`api_v3.test.tsx`](https://github.com/software-mansion/react-native-gesture-handler/blob/main/packages/react-native-gesture-handler/src/__tests__/api_v3.test.tsx) for full implementation.
12698

127-
### Example
99+
```tsx
100+
test('Pan gesture', () => {
101+
const onBegin = jest.fn();
102+
const onStart = jest.fn();
103+
104+
const panGesture = renderHook(() =>
105+
usePanGesture({
106+
disableReanimated: true,
107+
onBegin: (e) => onBegin(e),
108+
onActivate: (e) => onStart(e),
109+
})
110+
).result.current;
111+
112+
fireGestureHandler(panGesture, [
113+
{ oldState: State.UNDETERMINED, state: State.BEGAN },
114+
{ oldState: State.BEGAN, state: State.ACTIVE },
115+
{ oldState: State.ACTIVE, state: State.ACTIVE },
116+
{ oldState: State.ACTIVE, state: State.END },
117+
]);
128118

129-
See above example for `fireGestureHandler`.
119+
expect(onBegin).toHaveBeenCalledTimes(1);
120+
expect(onStart).toHaveBeenCalledTimes(1);
121+
});
122+
```

0 commit comments

Comments
 (0)