1
1
import { ReactTestInstance } from 'react-test-renderer' ;
2
+ import act from '../../act' ;
2
3
import { getHostParent } from '../../helpers/component-tree' ;
3
4
import { isTextInputEditable } from '../../helpers/text-input' ;
4
5
import { isPointerEventEnabled } from '../../helpers/pointer-events' ;
@@ -18,7 +19,7 @@ export interface PressOptions {
18
19
export async function press ( this : UserEventInstance , element : ReactTestInstance ) : Promise < void > {
19
20
await basePress ( this . config , element , {
20
21
type : 'press' ,
21
- duration : 0 ,
22
+ duration : DEFAULT_MIN_PRESS_DURATION ,
22
23
} ) ;
23
24
}
24
25
@@ -75,14 +76,18 @@ const emitPressablePressEvents = async (
75
76
76
77
dispatchEvent ( element , 'responderGrant' , EventBuilder . Common . responderGrant ( ) ) ;
77
78
78
- // We apply minimum press duration here to ensure that `press` events are emitted after `pressOut`.
79
- // Otherwise, pressables would emit them in the reverse order, which in reality happens only for
80
- // very short presses (< 130ms) and contradicts the React Native docs.
81
- // See: https://reactnative.dev/docs/pressable#onpress
82
- let duration = Math . max ( options . duration , DEFAULT_MIN_PRESS_DURATION ) ;
83
- await wait ( config , duration ) ;
79
+ await wait ( config , options . duration ) ;
84
80
85
81
dispatchEvent ( element , 'responderRelease' , EventBuilder . Common . responderRelease ( ) ) ;
82
+
83
+ // React Native will wait for minimal delay of DEFAULT_MIN_PRESS_DURATION
84
+ // before emitting the `pressOut` event. We need to wait here, so that
85
+ // `press()` function does not return before that.
86
+ if ( DEFAULT_MIN_PRESS_DURATION - options . duration > 0 ) {
87
+ await act ( async ( ) => {
88
+ await wait ( config , DEFAULT_MIN_PRESS_DURATION - options . duration ) ;
89
+ } ) ;
90
+ }
86
91
} ;
87
92
88
93
const isEnabledTouchResponder = ( element : ReactTestInstance ) => {
0 commit comments