@@ -2,7 +2,9 @@ import type { ReactTestInstance } from 'react-test-renderer';
2
2
3
3
import act from '../../act' ;
4
4
import { getEventHandler } from '../../event-handler' ;
5
+ import type { HostTestInstance } from '../../helpers/component-tree' ;
5
6
import { getHostParent , isHostElement } from '../../helpers/component-tree' ;
7
+ import { ErrorWithStack } from '../../helpers/errors' ;
6
8
import { isHostText , isHostTextInput } from '../../helpers/host-component-names' ;
7
9
import { isPointerEventEnabled } from '../../helpers/pointer-events' ;
8
10
import { EventBuilder } from '../event-builder' ;
@@ -19,6 +21,13 @@ export interface PressOptions {
19
21
}
20
22
21
23
export async function press ( this : UserEventInstance , element : ReactTestInstance ) : Promise < void > {
24
+ if ( ! isHostElement ( element ) ) {
25
+ throw new ErrorWithStack (
26
+ `press() works only with host elements. Passed element has type "${ element . type } ".` ,
27
+ press ,
28
+ ) ;
29
+ }
30
+
22
31
await basePress ( this . config , element , {
23
32
type : 'press' ,
24
33
} ) ;
@@ -29,6 +38,13 @@ export async function longPress(
29
38
element : ReactTestInstance ,
30
39
options ?: PressOptions ,
31
40
) : Promise < void > {
41
+ if ( ! isHostElement ( element ) ) {
42
+ throw new ErrorWithStack (
43
+ `longPress() works only with host elements. Passed element has type "${ element . type } ".` ,
44
+ longPress ,
45
+ ) ;
46
+ }
47
+
32
48
await basePress ( this . config , element , {
33
49
type : 'longPress' ,
34
50
duration : options ?. duration ?? DEFAULT_LONG_PRESS_DELAY_MS ,
@@ -42,7 +58,7 @@ interface BasePressOptions {
42
58
43
59
const basePress = async (
44
60
config : UserEventConfig ,
45
- element : ReactTestInstance ,
61
+ element : HostTestInstance ,
46
62
options : BasePressOptions ,
47
63
) : Promise < void > => {
48
64
if ( isEnabledHostElement ( element ) && hasPressEventHandler ( element ) ) {
@@ -63,8 +79,8 @@ const basePress = async (
63
79
await basePress ( config , hostParentElement , options ) ;
64
80
} ;
65
81
66
- function isEnabledHostElement ( element : ReactTestInstance ) {
67
- if ( ! isHostElement ( element ) || ! isPointerEventEnabled ( element ) ) {
82
+ function isEnabledHostElement ( element : HostTestInstance ) {
83
+ if ( ! isPointerEventEnabled ( element ) ) {
68
84
return false ;
69
85
}
70
86
@@ -80,11 +96,11 @@ function isEnabledHostElement(element: ReactTestInstance) {
80
96
return true ;
81
97
}
82
98
83
- function isEnabledTouchResponder ( element : ReactTestInstance ) {
99
+ function isEnabledTouchResponder ( element : HostTestInstance ) {
84
100
return isPointerEventEnabled ( element ) && element . props . onStartShouldSetResponder ?.( ) ;
85
101
}
86
102
87
- function hasPressEventHandler ( element : ReactTestInstance ) {
103
+ function hasPressEventHandler ( element : HostTestInstance ) {
88
104
return (
89
105
getEventHandler ( element , 'press' ) ||
90
106
getEventHandler ( element , 'longPress' ) ||
@@ -98,7 +114,7 @@ function hasPressEventHandler(element: ReactTestInstance) {
98
114
*/
99
115
async function emitDirectPressEvents (
100
116
config : UserEventConfig ,
101
- element : ReactTestInstance ,
117
+ element : HostTestInstance ,
102
118
options : BasePressOptions ,
103
119
) {
104
120
await wait ( config ) ;
@@ -124,7 +140,7 @@ async function emitDirectPressEvents(
124
140
125
141
async function emitPressabilityPressEvents (
126
142
config : UserEventConfig ,
127
- element : ReactTestInstance ,
143
+ element : HostTestInstance ,
128
144
options : BasePressOptions ,
129
145
) {
130
146
await wait ( config ) ;
0 commit comments