@@ -11,35 +11,48 @@ import { UserEventConfig, UserEventInstance } from '../setup';
11
11
import { dispatchEvent , wait , warnAboutRealTimersIfNeeded } from '../utils' ;
12
12
import { DEFAULT_MIN_PRESS_DURATION } from './constants' ;
13
13
14
- export type PressOptions = {
15
- duration : number ;
16
- } ;
14
+ export interface PressOptions {
15
+ duration ? : number ;
16
+ }
17
17
18
18
export async function press (
19
19
this : UserEventInstance ,
20
20
element : ReactTestInstance
21
21
) : Promise < void > {
22
- await basePress ( this . config , element ) ;
22
+ await basePress ( this . config , element , {
23
+ type : 'press' ,
24
+ duration : 0 ,
25
+ } ) ;
23
26
}
24
27
25
28
export async function longPress (
26
29
this : UserEventInstance ,
27
30
element : ReactTestInstance ,
28
- options : PressOptions = { duration : 500 }
31
+ options ? : PressOptions
29
32
) : Promise < void > {
30
- await basePress ( this . config , element , options ) ;
33
+ await basePress ( this . config , element , {
34
+ type : 'longPress' ,
35
+ duration : options ?. duration ?? 500 ,
36
+ } ) ;
37
+ }
38
+
39
+ interface BasePressOptions {
40
+ type : 'press' | 'longPress' ;
41
+ duration : number ;
31
42
}
32
43
33
44
const basePress = async (
34
45
config : UserEventConfig ,
35
46
element : ReactTestInstance ,
36
- options : PressOptions = { duration : 0 }
47
+ options : BasePressOptions
37
48
) : Promise < void > => {
38
- // Text and TextInput components are mocked in React Native preset so the mock
39
- // doesn't implement the pressability class
40
- // Thus we need to call the props directly on the host component
41
- if ( isPressableText ( element ) || isEnabledTextInput ( element ) ) {
42
- await emitBasicPressEvents ( config , element , options ) ;
49
+ if ( isPressableText ( element ) ) {
50
+ await emitTextPressEvents ( config , element , options ) ;
51
+ return ;
52
+ }
53
+
54
+ if ( isEnabledTextInput ( element ) ) {
55
+ await emitTextInputPressEvents ( config , element , options ) ;
43
56
return ;
44
57
}
45
58
@@ -59,7 +72,7 @@ const basePress = async (
59
72
const emitPressablePressEvents = async (
60
73
config : UserEventConfig ,
61
74
element : ReactTestInstance ,
62
- options : PressOptions = { duration : 0 }
75
+ options : BasePressOptions
63
76
) => {
64
77
warnAboutRealTimersIfNeeded ( ) ;
65
78
@@ -97,11 +110,18 @@ const isEnabledTouchResponder = (element: ReactTestInstance) => {
97
110
} ;
98
111
99
112
const isPressableText = ( element : ReactTestInstance ) => {
113
+ const hasPressEventHandler = Boolean (
114
+ element . props . onPress ||
115
+ element . props . onLongPress ||
116
+ element . props . onPressIn ||
117
+ element . props . onPressOut
118
+ ) ;
119
+
100
120
return (
101
121
isHostText ( element ) &&
102
122
isPointerEventEnabled ( element ) &&
103
123
! element . props . disabled &&
104
- element . props . onPress
124
+ hasPressEventHandler
105
125
) ;
106
126
} ;
107
127
@@ -114,18 +134,35 @@ const isEnabledTextInput = (element: ReactTestInstance) => {
114
134
} ;
115
135
116
136
/**
117
- * Dispatches a basic press event sequence on non-Pressable component,
118
- * e.g. Text or TextInput.
137
+ * Dispatches a press event sequence for Text.
138
+ */
139
+ async function emitTextPressEvents (
140
+ config : UserEventConfig ,
141
+ element : ReactTestInstance ,
142
+ options : BasePressOptions
143
+ ) {
144
+ await wait ( config ) ;
145
+ dispatchEvent ( element , 'pressIn' , EventBuilder . Common . touch ( ) ) ;
146
+
147
+ // Emit either `press` or `longPress`.
148
+ dispatchEvent ( element , options . type , EventBuilder . Common . touch ( ) ) ;
149
+
150
+ await wait ( config , options . duration ) ;
151
+ dispatchEvent ( element , 'pressOut' , EventBuilder . Common . touch ( ) ) ;
152
+ }
153
+
154
+ /**
155
+ * Dispatches a press event sequence for TextInput.
119
156
*/
120
- async function emitBasicPressEvents (
157
+ async function emitTextInputPressEvents (
121
158
config : UserEventConfig ,
122
159
element : ReactTestInstance ,
123
- options : PressOptions = { duration : 0 }
160
+ options : BasePressOptions
124
161
) {
125
162
await wait ( config ) ;
126
163
dispatchEvent ( element , 'pressIn' , EventBuilder . Common . touch ( ) ) ;
127
164
128
- dispatchEvent ( element , 'press' , EventBuilder . Common . touch ( ) ) ;
165
+ // Note: TextInput does not have `onPress`/`onLongPress` props.
129
166
130
167
await wait ( config , options . duration ) ;
131
168
dispatchEvent ( element , 'pressOut' , EventBuilder . Common . touch ( ) ) ;
0 commit comments