Skip to content

Commit e99bb8e

Browse files
author
pierrezimmermann
committed
refactor: also test payload of events for press and longpress
1 parent 83989b1 commit e99bb8e

File tree

2 files changed

+151
-4
lines changed

2 files changed

+151
-4
lines changed

src/user-event/press/__tests__/longPress.test.tsx

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,73 @@ import React from 'react';
22
import { Pressable, Text } from 'react-native';
33
import { render, screen } from '../../../pure';
44
import { userEvent } from '../..';
5+
import { createEventLogger } from '../../../test-utils';
56

67
describe('userEvent.longPress with fake timers', () => {
78
beforeEach(() => {
89
jest.useFakeTimers();
10+
jest.setSystemTime(0);
911
});
1012

1113
test('calls onLongPress if the delayLongPress is the default one', async () => {
12-
const mockOnLongPress = jest.fn();
14+
const { logEvent, events } = createEventLogger();
1315
const user = userEvent.setup();
1416

1517
render(
16-
<Pressable onLongPress={mockOnLongPress}>
18+
<Pressable onLongPress={logEvent('longPress')}>
1719
<Text>press me</Text>
1820
</Pressable>
1921
);
2022
await user.longPress(screen.getByText('press me'));
2123

22-
expect(mockOnLongPress).toHaveBeenCalled();
24+
expect(events).toMatchInlineSnapshot(`
25+
[
26+
{
27+
"name": "longPress",
28+
"payload": {
29+
"currentTarget": {
30+
"measure": [MockFunction] {
31+
"calls": [
32+
[
33+
[Function],
34+
],
35+
[
36+
[Function],
37+
],
38+
],
39+
"results": [
40+
{
41+
"type": "return",
42+
"value": undefined,
43+
},
44+
{
45+
"type": "return",
46+
"value": undefined,
47+
},
48+
],
49+
},
50+
},
51+
"dispatchConfig": {
52+
"registrationName": "onResponderGrant",
53+
},
54+
"nativeEvent": {
55+
"timestamp": 500,
56+
},
57+
"persist": [MockFunction] {
58+
"calls": [
59+
[],
60+
],
61+
"results": [
62+
{
63+
"type": "return",
64+
"value": undefined,
65+
},
66+
],
67+
},
68+
},
69+
},
70+
]
71+
`);
2372
});
2473

2574
test('calls onLongPress when duration is greater than specified onLongPressDelay', async () => {

src/user-event/press/__tests__/press.test.tsx

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { userEvent } from '../..';
1414
describe('userEvent.press with fake timers', () => {
1515
beforeEach(() => {
1616
jest.useFakeTimers();
17+
jest.setSystemTime(0);
1718
});
1819

1920
test('calls onPressIn, onPress and onPressOut prop of touchable', async () => {
@@ -30,7 +31,104 @@ describe('userEvent.press with fake timers', () => {
3031
);
3132
await user.press(screen.getByTestId('pressable'));
3233

33-
expect(getEventsName(events)).toEqual(['pressIn', 'press', 'pressOut']);
34+
expect(events).toMatchInlineSnapshot(`
35+
[
36+
{
37+
"name": "pressIn",
38+
"payload": {
39+
"currentTarget": {
40+
"measure": [MockFunction] {
41+
"calls": [
42+
[
43+
[Function],
44+
],
45+
[
46+
[Function],
47+
],
48+
],
49+
"results": [
50+
{
51+
"type": "return",
52+
"value": undefined,
53+
},
54+
{
55+
"type": "return",
56+
"value": undefined,
57+
},
58+
],
59+
},
60+
},
61+
"dispatchConfig": {
62+
"registrationName": "onResponderGrant",
63+
},
64+
"nativeEvent": {
65+
"timestamp": 0,
66+
},
67+
"persist": [MockFunction] {
68+
"calls": [
69+
[],
70+
],
71+
"results": [
72+
{
73+
"type": "return",
74+
"value": undefined,
75+
},
76+
],
77+
},
78+
},
79+
},
80+
{
81+
"name": "press",
82+
"payload": {
83+
"currentTarget": {
84+
"measure": [MockFunction],
85+
},
86+
"dispatchConfig": {
87+
"registrationName": "onResponderRelease",
88+
},
89+
"nativeEvent": {
90+
"timestamp": 0,
91+
},
92+
"persist": [MockFunction] {
93+
"calls": [
94+
[],
95+
],
96+
"results": [
97+
{
98+
"type": "return",
99+
"value": undefined,
100+
},
101+
],
102+
},
103+
},
104+
},
105+
{
106+
"name": "pressOut",
107+
"payload": {
108+
"currentTarget": {
109+
"measure": [MockFunction],
110+
},
111+
"dispatchConfig": {
112+
"registrationName": "onResponderRelease",
113+
},
114+
"nativeEvent": {
115+
"timestamp": 0,
116+
},
117+
"persist": [MockFunction] {
118+
"calls": [
119+
[],
120+
],
121+
"results": [
122+
{
123+
"type": "return",
124+
"value": undefined,
125+
},
126+
],
127+
},
128+
},
129+
},
130+
]
131+
`);
34132
});
35133

36134
test('does not trigger event when pressable is disabled', async () => {

0 commit comments

Comments
 (0)