@@ -11,35 +11,48 @@ import { UserEventConfig, UserEventInstance } from '../setup';
1111import  {  dispatchEvent ,  wait ,  warnAboutRealTimersIfNeeded  }  from  '../utils' ; 
1212import  {  DEFAULT_MIN_PRESS_DURATION  }  from  './constants' ; 
1313
14- export  type  PressOptions   =  { 
15-   duration : number ; 
16- } ; 
14+ export  interface  PressOptions  { 
15+   duration ? : number ; 
16+ } 
1717
1818export  async  function  press ( 
1919  this : UserEventInstance , 
2020  element : ReactTestInstance 
2121) : Promise < void >  { 
22-   await  basePress ( this . config ,  element ) ; 
22+   await  basePress ( this . config ,  element ,  { 
23+     type : 'press' , 
24+     duration : 0 , 
25+   } ) ; 
2326} 
2427
2528export  async  function  longPress ( 
2629  this : UserEventInstance , 
2730  element : ReactTestInstance , 
28-   options : PressOptions   =   {   duration :  500   } 
31+   options ? : PressOptions 
2932) : 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 ; 
3142} 
3243
3344const  basePress  =  async  ( 
3445  config : UserEventConfig , 
3546  element : ReactTestInstance , 
36-   options : PressOptions   =   {   duration :  0   } 
47+   options : BasePressOptions 
3748) : 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 ) ; 
4356    return ; 
4457  } 
4558
@@ -59,7 +72,7 @@ const basePress = async (
5972const  emitPressablePressEvents  =  async  ( 
6073  config : UserEventConfig , 
6174  element : ReactTestInstance , 
62-   options : PressOptions   =   {   duration :  0   } 
75+   options : BasePressOptions 
6376)  =>  { 
6477  warnAboutRealTimersIfNeeded ( ) ; 
6578
@@ -97,11 +110,18 @@ const isEnabledTouchResponder = (element: ReactTestInstance) => {
97110} ; 
98111
99112const  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+ 
100120  return  ( 
101121    isHostText ( element )  && 
102122    isPointerEventEnabled ( element )  && 
103123    ! element . props . disabled  && 
104-     element . props . onPress 
124+     hasPressEventHandler 
105125  ) ; 
106126} ; 
107127
@@ -114,18 +134,35 @@ const isEnabledTextInput = (element: ReactTestInstance) => {
114134} ; 
115135
116136/** 
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. 
119156 */ 
120- async  function  emitBasicPressEvents ( 
157+ async  function  emitTextInputPressEvents ( 
121158  config : UserEventConfig , 
122159  element : ReactTestInstance , 
123-   options : PressOptions   =   {   duration :  0   } 
160+   options : BasePressOptions 
124161)  { 
125162  await  wait ( config ) ; 
126163  dispatchEvent ( element ,  'pressIn' ,  EventBuilder . Common . touch ( ) ) ; 
127164
128-   dispatchEvent ( element ,   'press' ,   EventBuilder . Common . touch ( ) ) ; 
165+   // Note: TextInput does not have `onPress`/`onLongPress` props. 
129166
130167  await  wait ( config ,  options . duration ) ; 
131168  dispatchEvent ( element ,  'pressOut' ,  EventBuilder . Common . touch ( ) ) ; 
0 commit comments