Skip to content

Commit aa96951

Browse files
committed
refactor: code review changes
1 parent 762b2f5 commit aa96951

File tree

7 files changed

+369
-152
lines changed

7 files changed

+369
-152
lines changed

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

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,98 @@ exports[`userEvent.longPress with fake timers calls onLongPress if the delayLong
3434
},
3535
]
3636
`;
37+
38+
exports[`userEvent.longPress with fake timers works on Pressable 1`] = `
39+
[
40+
{
41+
"name": "pressIn",
42+
"payload": {
43+
"currentTarget": {
44+
"measure": [Function],
45+
},
46+
"dispatchConfig": {
47+
"registrationName": "onResponderGrant",
48+
},
49+
"isDefaultPrevented": [Function],
50+
"isPersistent": [Function],
51+
"isPropagationStopped": [Function],
52+
"nativeEvent": {
53+
"changedTouches": [],
54+
"identifier": 0,
55+
"locationX": 0,
56+
"locationY": 0,
57+
"pageX": 0,
58+
"pageY": 0,
59+
"target": 0,
60+
"timestamp": 0,
61+
"touches": [],
62+
},
63+
"persist": [Function],
64+
"preventDefault": [Function],
65+
"stopPropagation": [Function],
66+
"target": {},
67+
"timeStamp": 0,
68+
},
69+
},
70+
{
71+
"name": "longPress",
72+
"payload": {
73+
"currentTarget": {
74+
"measure": [Function],
75+
},
76+
"dispatchConfig": {
77+
"registrationName": "onResponderGrant",
78+
},
79+
"isDefaultPrevented": [Function],
80+
"isPersistent": [Function],
81+
"isPropagationStopped": [Function],
82+
"nativeEvent": {
83+
"changedTouches": [],
84+
"identifier": 0,
85+
"locationX": 0,
86+
"locationY": 0,
87+
"pageX": 0,
88+
"pageY": 0,
89+
"target": 0,
90+
"timestamp": 0,
91+
"touches": [],
92+
},
93+
"persist": [Function],
94+
"preventDefault": [Function],
95+
"stopPropagation": [Function],
96+
"target": {},
97+
"timeStamp": 0,
98+
},
99+
},
100+
{
101+
"name": "pressOut",
102+
"payload": {
103+
"currentTarget": {
104+
"measure": [Function],
105+
},
106+
"dispatchConfig": {
107+
"registrationName": "onResponderRelease",
108+
},
109+
"isDefaultPrevented": [Function],
110+
"isPersistent": [Function],
111+
"isPropagationStopped": [Function],
112+
"nativeEvent": {
113+
"changedTouches": [],
114+
"identifier": 0,
115+
"locationX": 0,
116+
"locationY": 0,
117+
"pageX": 0,
118+
"pageY": 0,
119+
"target": 0,
120+
"timestamp": 500,
121+
"touches": [],
122+
},
123+
"persist": [Function],
124+
"preventDefault": [Function],
125+
"stopPropagation": [Function],
126+
"target": {},
127+
"timeStamp": 0,
128+
},
129+
},
130+
]
131+
`;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`userEvent.press with fake timers calls onPressIn, onPress and onPressOut prop of touchable 1`] = `
3+
exports[`userEvent.press with fake timers works on Pressable 1`] = `
44
[
55
{
66
"name": "pressIn",

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

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
2-
import { Pressable, Text } from 'react-native';
3-
import { render, screen } from '../../../pure';
2+
import { Pressable, Text, TouchableHighlight, TouchableOpacity } from 'react-native';
3+
import { createEventLogger, getEventsNames } from '../../../test-utils';
4+
import { render, screen } from '../../..';
45
import { userEvent } from '../..';
56

67
describe('userEvent.longPress with real timers', () => {
@@ -9,6 +10,68 @@ describe('userEvent.longPress with real timers', () => {
910
jest.restoreAllMocks();
1011
});
1112

13+
test('works on Pressable', async () => {
14+
const { events, logEvent } = createEventLogger();
15+
const user = userEvent.setup();
16+
17+
render(
18+
<Pressable
19+
onPress={logEvent('press')}
20+
onPressIn={logEvent('pressIn')}
21+
onPressOut={logEvent('pressOut')}
22+
onLongPress={logEvent('longPress')}
23+
testID="pressable"
24+
/>,
25+
);
26+
27+
await user.longPress(screen.getByTestId('pressable'));
28+
expect(getEventsNames(events)).toEqual(['pressIn', 'longPress', 'pressOut']);
29+
});
30+
31+
test('works on TouchableOpacity', async () => {
32+
const mockOnPress = jest.fn();
33+
34+
render(
35+
<TouchableOpacity onPress={mockOnPress}>
36+
<Text>press me</Text>
37+
</TouchableOpacity>,
38+
);
39+
40+
await userEvent.longPress(screen.getByText('press me'));
41+
expect(mockOnPress).toHaveBeenCalled();
42+
});
43+
44+
test('works on TouchableHighlight', async () => {
45+
const mockOnPress = jest.fn();
46+
47+
render(
48+
<TouchableHighlight onPress={mockOnPress}>
49+
<Text>press me</Text>
50+
</TouchableHighlight>,
51+
);
52+
53+
await userEvent.longPress(screen.getByText('press me'));
54+
expect(mockOnPress).toHaveBeenCalled();
55+
});
56+
57+
test('works on Text', async () => {
58+
const { events, logEvent } = createEventLogger();
59+
60+
render(
61+
<Text
62+
onPress={logEvent('press')}
63+
onPressIn={logEvent('pressIn')}
64+
onPressOut={logEvent('pressOut')}
65+
onLongPress={logEvent('longPress')}
66+
>
67+
press me
68+
</Text>,
69+
);
70+
71+
await userEvent.longPress(screen.getByText('press me'));
72+
expect(getEventsNames(events)).toEqual(['pressIn', 'longPress', 'pressOut']);
73+
});
74+
1275
test('calls onLongPress if the delayLongPress is the default one', async () => {
1376
const mockOnLongPress = jest.fn();
1477
const user = userEvent.setup();

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

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,78 @@
11
import React from 'react';
2-
import { Pressable, Text } from 'react-native';
3-
import { render, screen } from '../../../pure';
2+
import { Pressable, Text, TouchableHighlight, TouchableOpacity } from 'react-native';
3+
import { createEventLogger, getEventsNames } from '../../../test-utils';
4+
import { render, screen } from '../../..';
45
import { userEvent } from '../..';
5-
import { createEventLogger } from '../../../test-utils';
66

77
describe('userEvent.longPress with fake timers', () => {
88
beforeEach(() => {
99
jest.useFakeTimers();
1010
jest.setSystemTime(0);
1111
});
1212

13+
test('works on Pressable', async () => {
14+
const { events, logEvent } = createEventLogger();
15+
const user = userEvent.setup();
16+
17+
render(
18+
<Pressable
19+
onPress={logEvent('press')}
20+
onPressIn={logEvent('pressIn')}
21+
onPressOut={logEvent('pressOut')}
22+
onLongPress={logEvent('longPress')}
23+
testID="pressable"
24+
/>,
25+
);
26+
27+
await user.longPress(screen.getByTestId('pressable'));
28+
expect(getEventsNames(events)).toEqual(['pressIn', 'longPress', 'pressOut']);
29+
expect(events).toMatchSnapshot();
30+
});
31+
32+
test('works on TouchableOpacity', async () => {
33+
const mockOnPress = jest.fn();
34+
35+
render(
36+
<TouchableOpacity onPress={mockOnPress}>
37+
<Text>press me</Text>
38+
</TouchableOpacity>,
39+
);
40+
41+
await userEvent.longPress(screen.getByText('press me'));
42+
expect(mockOnPress).toHaveBeenCalled();
43+
});
44+
45+
test('works on TouchableHighlight', async () => {
46+
const mockOnPress = jest.fn();
47+
48+
render(
49+
<TouchableHighlight onPress={mockOnPress}>
50+
<Text>press me</Text>
51+
</TouchableHighlight>,
52+
);
53+
54+
await userEvent.longPress(screen.getByText('press me'));
55+
expect(mockOnPress).toHaveBeenCalled();
56+
});
57+
58+
test('works on Text', async () => {
59+
const { events, logEvent } = createEventLogger();
60+
61+
render(
62+
<Text
63+
onPress={logEvent('press')}
64+
onPressIn={logEvent('pressIn')}
65+
onPressOut={logEvent('pressOut')}
66+
onLongPress={logEvent('longPress')}
67+
>
68+
press me
69+
</Text>,
70+
);
71+
72+
await userEvent.longPress(screen.getByText('press me'));
73+
expect(getEventsNames(events)).toEqual(['pressIn', 'longPress', 'pressOut']);
74+
});
75+
1376
test('calls onLongPress if the delayLongPress is the default one', async () => {
1477
const { logEvent, events } = createEventLogger();
1578
const user = userEvent.setup();

0 commit comments

Comments
 (0)