@@ -3,9 +3,8 @@ import type { ReactTestInstance } from 'react-test-renderer';
3
3
import act from '../../act' ;
4
4
import { getEventHandler } from '../../event-handler' ;
5
5
import { getHostParent , isHostElement } from '../../helpers/component-tree' ;
6
- import { isHostTextInput } from '../../helpers/host-component-names' ;
6
+ import { isHostText , isHostTextInput } from '../../helpers/host-component-names' ;
7
7
import { isPointerEventEnabled } from '../../helpers/pointer-events' ;
8
- import { isEditableTextInput } from '../../helpers/text-input' ;
9
8
import { EventBuilder } from '../event-builder' ;
10
9
import type { UserEventConfig , UserEventInstance } from '../setup' ;
11
10
import { dispatchEvent , wait } from '../utils' ;
@@ -46,18 +45,13 @@ const basePress = async (
46
45
element : ReactTestInstance ,
47
46
options : BasePressOptions ,
48
47
) : Promise < void > => {
49
- if ( isEditableTextInput ( element ) && isPointerEventEnabled ( element ) ) {
50
- await emitTextInputPressEvents ( config , element , options ) ;
51
- return ;
52
- }
53
-
54
- if ( ! isHostTextInput ( element ) && isHostPressable ( element ) ) {
55
- await emitTextPressEvents ( config , element , options ) ;
48
+ if ( isEnabledHostElement ( element ) && hasPressEventHandler ( element ) ) {
49
+ await emitDirectPressEvents ( config , element , options ) ;
56
50
return ;
57
51
}
58
52
59
53
if ( isEnabledTouchResponder ( element ) ) {
60
- await emitPressablePressEvents ( config , element , options ) ;
54
+ await emitPressabilityPressEvents ( config , element , options ) ;
61
55
return ;
62
56
}
63
57
@@ -69,53 +63,40 @@ const basePress = async (
69
63
await basePress ( config , hostParentElement , options ) ;
70
64
} ;
71
65
72
- const emitPressablePressEvents = async (
73
- config : UserEventConfig ,
74
- element : ReactTestInstance ,
75
- options : BasePressOptions ,
76
- ) => {
77
- await wait ( config ) ;
78
-
79
- dispatchEvent ( element , 'responderGrant' , EventBuilder . Common . responderGrant ( ) ) ;
80
-
81
- const duration = options . duration ?? DEFAULT_MIN_PRESS_DURATION ;
82
- await wait ( config , duration ) ;
66
+ function isEnabledHostElement ( element : ReactTestInstance ) {
67
+ if ( ! isHostElement ( element ) || ! isPointerEventEnabled ( element ) ) {
68
+ return false ;
69
+ }
83
70
84
- dispatchEvent ( element , 'responderRelease' , EventBuilder . Common . responderRelease ( ) ) ;
71
+ if ( isHostText ( element ) ) {
72
+ return element . props . disabled !== true ;
73
+ }
85
74
86
- // React Native will wait for minimal delay of DEFAULT_MIN_PRESS_DURATION
87
- // before emitting the `pressOut` event. We need to wait here, so that
88
- // `press()` function does not return before that.
89
- if ( DEFAULT_MIN_PRESS_DURATION - duration > 0 ) {
90
- await act ( async ( ) => {
91
- await wait ( config , DEFAULT_MIN_PRESS_DURATION - duration ) ;
92
- } ) ;
75
+ if ( isHostTextInput ( element ) ) {
76
+ // @ts -expect-error - workaround incorrect ReactTestInstance type
77
+ return element . props . editable !== false ;
93
78
}
94
- } ;
95
79
96
- const isEnabledTouchResponder = ( element : ReactTestInstance ) => {
80
+ return true ;
81
+ }
82
+
83
+ function isEnabledTouchResponder ( element : ReactTestInstance ) {
97
84
return isPointerEventEnabled ( element ) && element . props . onStartShouldSetResponder ?.( ) ;
98
- } ;
85
+ }
99
86
100
- const isHostPressable = ( element : ReactTestInstance ) => {
101
- const hasPressEventHandler =
87
+ function hasPressEventHandler ( element : ReactTestInstance ) {
88
+ return (
102
89
getEventHandler ( element , 'press' ) ||
103
90
getEventHandler ( element , 'longPress' ) ||
104
91
getEventHandler ( element , 'pressIn' ) ||
105
- getEventHandler ( element , 'pressOut' ) ;
106
-
107
- return (
108
- isHostElement ( element ) &&
109
- isPointerEventEnabled ( element ) &&
110
- ! element . props . disabled &&
111
- hasPressEventHandler
92
+ getEventHandler ( element , 'pressOut' )
112
93
) ;
113
- } ;
94
+ }
114
95
115
96
/**
116
- * Dispatches a press event sequence for Text .
97
+ * Dispatches a press event sequence for host elements that have `onPress*` event handlers .
117
98
*/
118
- async function emitTextPressEvents (
99
+ async function emitDirectPressEvents (
119
100
config : UserEventConfig ,
120
101
element : ReactTestInstance ,
121
102
options : BasePressOptions ,
@@ -141,19 +122,26 @@ async function emitTextPressEvents(
141
122
}
142
123
}
143
124
144
- /**
145
- * Dispatches a press event sequence for TextInput.
146
- */
147
- async function emitTextInputPressEvents (
125
+ async function emitPressabilityPressEvents (
148
126
config : UserEventConfig ,
149
127
element : ReactTestInstance ,
150
128
options : BasePressOptions ,
151
129
) {
152
130
await wait ( config ) ;
153
- dispatchEvent ( element , 'pressIn' , EventBuilder . Common . touch ( ) ) ;
154
131
155
- // Note: TextInput does not have `onPress`/`onLongPress` props.
132
+ dispatchEvent ( element , 'responderGrant' , EventBuilder . Common . responderGrant ( ) ) ;
156
133
157
- await wait ( config , options . duration ) ;
158
- dispatchEvent ( element , 'pressOut' , EventBuilder . Common . touch ( ) ) ;
134
+ const duration = options . duration ?? DEFAULT_MIN_PRESS_DURATION ;
135
+ await wait ( config , duration ) ;
136
+
137
+ dispatchEvent ( element , 'responderRelease' , EventBuilder . Common . responderRelease ( ) ) ;
138
+
139
+ // React Native will wait for minimal delay of DEFAULT_MIN_PRESS_DURATION
140
+ // before emitting the `pressOut` event. We need to wait here, so that
141
+ // `press()` function does not return before that.
142
+ if ( DEFAULT_MIN_PRESS_DURATION - duration > 0 ) {
143
+ await act ( async ( ) => {
144
+ await wait ( config , DEFAULT_MIN_PRESS_DURATION - duration ) ;
145
+ } ) ;
146
+ }
159
147
}
0 commit comments