| id | testing |
|---|---|
| title | Testing with Jest |
| sidebar_position | 4 |
In order to load mocks provided by RNGH, add the following to your jest config in package.json:
"setupFiles": ["./node_modules/react-native-gesture-handler/jestSetup.js"]Example:
"jest": {
"preset": "react-native",
"setupFiles": ["./node_modules/react-native-gesture-handler/jestSetup.js"]
}RNGH provides APIs, specifically fireGestureHandler and getByGestureTestId, to trigger selected handlers.
import { fireGestureHandler } from 'react-native-gesture-handler/jest-utils';
fireGestureHandler: (componentOrGesture, eventList) => void;Simulates one event stream (i.e. event sequence starting with BEGIN state and ending
with one of END/FAIL/CANCEL states), calling appropriate callbacks associated with given gesture handler.
-
componentOrGesture- Either Gesture Handler component found byJestqueries (e.g.getByTestId) or Gesture found bygetByGestureTestId() -
eventList- Event data passed to appropriate callback. RNGH fills event list if required data is missing using these rules:oldStateis filled using state of the previous event.BEGINevents useUNDETERMINEDvalue as previous event.- Events after first
ACTIVEstate can omitstatefield. - Handler specific data is filled (e.g.
numberOfTouches,xfields) with defaults. - Missing
BEGINandENDevents are added with data copied from first and last passed event, respectively. - If first event doesn't have
statefield, theACTIVEstate is assumed.
Some eventList examples:
const oldStateFilled = [
{ state: State.BEGAN },
{ state: State.ACTIVE },
{ state: State.END },
]; // three events with specified state are fired.
const implicitActiveState = [
{ state: State.BEGAN },
{ state: State.ACTIVE },
{ x: 5 },
{ state: State.END },
]; // 4 events, including two ACTIVE events (second one has overridden additional data).
const implicitBegin = [
{ x: 1, y: 11 },
{ x: 2, y: 12, state: State.FAILED },
]; // 3 events, including implicit BEGAN, one ACTIVE, and one FAILED event with additional data.
const implicitBeginAndEnd = [
{ x: 5, y: 15 },
{ x: 6, y: 16 },
{ x: 7, y: 17 },
]; // 5 events, including 3 ACTIVE events and implicit BEGAN and END events. BEGAN uses first event's additional data, END uses last event's additional data.
const allImplicits = []; // 3 events, one BEGIN, one ACTIVE, one END with defaults.import { getByGestureTestId } from 'react-native-gesture-handler/jest-utils';
getByGestureTestId: (testID: string) => Gesture;Returns opaque data type associated with gesture. Gesture is found via testID attribute in rendered
components.
:::warning
testID must be unique among components rendered in test.
:::
Extracted from RNGH tests, check api_v3.test.tsx for full implementation.
test('Pan gesture', () => {
const onBegin = jest.fn();
const onStart = jest.fn();
const panGesture = renderHook(() =>
usePanGesture({
disableReanimated: true,
onBegin: (e) => onBegin(e),
onActivate: (e) => onStart(e),
})
).result.current;
fireGestureHandler(panGesture, [
{ oldState: State.UNDETERMINED, state: State.BEGAN },
{ oldState: State.BEGAN, state: State.ACTIVE },
{ oldState: State.ACTIVE, state: State.ACTIVE },
{ oldState: State.ACTIVE, state: State.END },
]);
expect(onBegin).toHaveBeenCalledTimes(1);
expect(onStart).toHaveBeenCalledTimes(1);
});