Skip to content

Commit 9f573ef

Browse files
authored
[General] Update existing events tests (#3944)
## Description Removes failing tests for the V1 API. Given that it has been deprecated for quite a while, and there are plans to remove it, I feel like getting rid of the tests is justified. It will also enable us to run the tests continuously on GitHub Actions. ## Test plan Run the tests
1 parent 28c90b3 commit 9f573ef

File tree

1 file changed

+1
-214
lines changed

1 file changed

+1
-214
lines changed

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

Lines changed: 1 addition & 214 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,16 @@
33
// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/56937
44
import React from 'react';
55
import { render, cleanup } from '@testing-library/react-native';
6-
import { Text, View } from 'react-native';
6+
import { Text } from 'react-native';
77
import {
88
GestureHandlerRootView,
99
PanGestureHandler,
10-
LongPressGestureHandler,
11-
LongPressGestureHandlerGestureEvent,
12-
RotationGestureHandler,
1310
Gesture,
1411
GestureDetector,
1512
State,
1613
PanGesture,
1714
TapGesture,
1815
} from '../index';
19-
import { useAnimatedGestureHandler } from 'react-native-reanimated';
2016
import { fireGestureHandler, getByGestureTestId } from '../jestUtils';
2117

2218
beforeEach(cleanup);
@@ -33,215 +29,6 @@ const mockedEventHandlers = () => {
3329
};
3430
};
3531

36-
interface EventHandlersProps {
37-
eventHandlers: ReturnType<typeof mockedEventHandlers>;
38-
}
39-
40-
describe('Using RNGH v1 base API', () => {
41-
function SingleHandler({ eventHandlers }: EventHandlersProps) {
42-
const handlers = {
43-
onBegan: eventHandlers.begin,
44-
onActivated: eventHandlers.active,
45-
onEnded: eventHandlers.end,
46-
onCancelled: eventHandlers.cancel,
47-
onFailed: eventHandlers.fail,
48-
onGestureEvent: eventHandlers.active,
49-
};
50-
51-
return (
52-
<GestureHandlerRootView>
53-
<PanGestureHandler testID="pan" {...handlers}>
54-
<Text>Pan handler</Text>
55-
</PanGestureHandler>
56-
</GestureHandlerRootView>
57-
);
58-
}
59-
60-
function NestedHandlers({ eventHandlers }: EventHandlersProps) {
61-
const handlers = {
62-
onBegan: eventHandlers.begin,
63-
onActivated: eventHandlers.active,
64-
onEnded: eventHandlers.end,
65-
onCancelled: eventHandlers.cancel,
66-
onFailed: eventHandlers.fail,
67-
onGestureEvent: eventHandlers.active,
68-
};
69-
return (
70-
<GestureHandlerRootView>
71-
<PanGestureHandler testID="pan" {...handlers}>
72-
<View>
73-
<Text>Pan handler</Text>
74-
<RotationGestureHandler testID="rotation" {...handlers}>
75-
<Text>Rotation handler</Text>
76-
</RotationGestureHandler>
77-
</View>
78-
</PanGestureHandler>
79-
</GestureHandlerRootView>
80-
);
81-
}
82-
83-
test('receives events', () => {
84-
const handlers = mockedEventHandlers();
85-
const { getByTestId } = render(<SingleHandler eventHandlers={handlers} />);
86-
fireGestureHandler<PanGestureHandler>(getByTestId('pan'), [
87-
{ oldState: State.UNDETERMINED, state: State.BEGAN },
88-
{ oldState: State.BEGAN, state: State.ACTIVE },
89-
{ oldState: State.ACTIVE, state: State.ACTIVE },
90-
{ oldState: State.ACTIVE, state: State.END },
91-
]);
92-
expect(handlers.begin).toHaveBeenCalled();
93-
expect(handlers.active).toHaveBeenCalled();
94-
expect(handlers.end).toHaveBeenCalled();
95-
expect(handlers.cancel).not.toHaveBeenCalled();
96-
expect(handlers.fail).not.toHaveBeenCalled();
97-
});
98-
99-
test('receives events correct number of times', () => {
100-
const handlers = mockedEventHandlers();
101-
const { getByTestId } = render(<SingleHandler eventHandlers={handlers} />);
102-
fireGestureHandler<PanGestureHandler>(getByTestId('pan'), [
103-
{ oldState: State.UNDETERMINED, state: State.BEGAN },
104-
{ oldState: State.BEGAN, state: State.ACTIVE },
105-
{ oldState: State.ACTIVE, state: State.ACTIVE }, // gesture event
106-
{ oldState: State.ACTIVE, state: State.END },
107-
]);
108-
expect(handlers.begin).toHaveBeenCalledTimes(1);
109-
expect(handlers.active).toHaveBeenCalledTimes(2);
110-
expect(handlers.end).toHaveBeenCalledTimes(1);
111-
expect(handlers.cancel).not.toHaveBeenCalled();
112-
expect(handlers.fail).not.toHaveBeenCalled();
113-
});
114-
115-
test('receives events with correct base fields (state, oldState, numberOfPointers, handlerTag)', () => {
116-
const handlers = mockedEventHandlers();
117-
const { getByTestId } = render(<SingleHandler eventHandlers={handlers} />);
118-
const component = getByTestId('pan');
119-
120-
const COMMON_EVENT_DATA = {
121-
numberOfPointers: 3,
122-
handlerTag: component.props.handlerTag as number,
123-
};
124-
fireGestureHandler<PanGestureHandler>(component, [
125-
{
126-
...COMMON_EVENT_DATA,
127-
oldState: State.UNDETERMINED,
128-
state: State.BEGAN,
129-
}, // BEGIN - state change
130-
{ ...COMMON_EVENT_DATA, oldState: State.BEGAN, state: State.ACTIVE },
131-
{ ...COMMON_EVENT_DATA, state: State.ACTIVE }, // gesture event
132-
]);
133-
134-
// gesture state change
135-
expect(handlers.begin).toHaveBeenLastCalledWith({
136-
nativeEvent: expect.objectContaining({
137-
...COMMON_EVENT_DATA,
138-
oldState: State.UNDETERMINED,
139-
state: State.BEGAN,
140-
}),
141-
});
142-
143-
// last ACTIVE gesture event, without `oldState`
144-
expect(handlers.active).toHaveBeenLastCalledWith({
145-
nativeEvent: expect.objectContaining({
146-
...COMMON_EVENT_DATA,
147-
state: State.ACTIVE,
148-
}),
149-
});
150-
expect(handlers.active).toHaveBeenLastCalledWith({
151-
nativeEvent: expect.not.objectContaining({
152-
oldState: expect.any(Number),
153-
}),
154-
});
155-
});
156-
157-
test.each([
158-
[
159-
'pan',
160-
{
161-
translationY: 800,
162-
velocityY: 2,
163-
},
164-
{
165-
translationX: 100,
166-
},
167-
],
168-
[
169-
'rotation',
170-
{
171-
anchorY: 0,
172-
rotation: 3.14,
173-
},
174-
{ numberOfPointers: 2 },
175-
],
176-
])(
177-
'receives additional properties depending on handler type ("%s")',
178-
(
179-
handlerName: string,
180-
additionalEventData: Record<string, unknown>,
181-
defaultEventData: Record<string, unknown>
182-
) => {
183-
const handlers = mockedEventHandlers();
184-
const { getByTestId } = render(
185-
<NestedHandlers eventHandlers={handlers} />
186-
);
187-
188-
fireGestureHandler(getByTestId(handlerName), [
189-
{
190-
...additionalEventData,
191-
oldState: State.UNDETERMINED,
192-
state: State.BEGAN,
193-
},
194-
{
195-
...additionalEventData,
196-
oldState: State.BEGAN,
197-
state: State.ACTIVE,
198-
},
199-
]);
200-
201-
expect(handlers.begin).toHaveBeenLastCalledWith({
202-
nativeEvent: expect.objectContaining({
203-
...additionalEventData,
204-
...defaultEventData,
205-
}),
206-
});
207-
}
208-
);
209-
});
210-
211-
describe('Using Reanimated 2 useAnimatedGestureHandler hook', () => {
212-
function UseAnimatedGestureHandler({ eventHandlers }: EventHandlersProps) {
213-
const eventHandler =
214-
useAnimatedGestureHandler<LongPressGestureHandlerGestureEvent>({
215-
onStart: eventHandlers.begin,
216-
});
217-
return (
218-
<LongPressGestureHandler
219-
testID="longPress"
220-
onHandlerStateChange={eventHandler}>
221-
<Text>Long press handler</Text>
222-
</LongPressGestureHandler>
223-
);
224-
}
225-
226-
test('calls callback with (event data, context)', () => {
227-
const handlers = mockedEventHandlers();
228-
const { getByTestId } = render(
229-
<UseAnimatedGestureHandler eventHandlers={handlers} />
230-
);
231-
232-
fireGestureHandler<LongPressGestureHandler>(getByTestId('longPress'), [
233-
{ state: State.BEGAN },
234-
{ state: State.ACTIVE },
235-
{ state: State.END },
236-
]);
237-
238-
expect(handlers.begin).toHaveBeenCalledWith(
239-
expect.objectContaining({ state: State.BEGAN }),
240-
expect.any(Object)
241-
);
242-
});
243-
});
244-
24532
describe('Using RNGH v2 gesture API', () => {
24633
interface SingleHandlerProps {
24734
handlers: ReturnType<typeof mockedEventHandlers>;

0 commit comments

Comments
 (0)