@@ -2,7 +2,9 @@ import type { ReactTestInstance } from 'react-test-renderer';
22
33import act from '../../act' ;
44import { getEventHandler } from '../../event-handler' ;
5+ import type { HostTestInstance } from '../../helpers/component-tree' ;
56import { getHostParent , isHostElement } from '../../helpers/component-tree' ;
7+ import { ErrorWithStack } from '../../helpers/errors' ;
68import { isHostText , isHostTextInput } from '../../helpers/host-component-names' ;
79import { isPointerEventEnabled } from '../../helpers/pointer-events' ;
810import { EventBuilder } from '../event-builder' ;
@@ -19,6 +21,13 @@ export interface PressOptions {
1921}
2022
2123export 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+
2231 await basePress ( this . config , element , {
2332 type : 'press' ,
2433 } ) ;
@@ -29,6 +38,13 @@ export async function longPress(
2938 element : ReactTestInstance ,
3039 options ?: PressOptions ,
3140) : 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+
3248 await basePress ( this . config , element , {
3349 type : 'longPress' ,
3450 duration : options ?. duration ?? DEFAULT_LONG_PRESS_DELAY_MS ,
@@ -42,7 +58,7 @@ interface BasePressOptions {
4258
4359const basePress = async (
4460 config : UserEventConfig ,
45- element : ReactTestInstance ,
61+ element : HostTestInstance ,
4662 options : BasePressOptions ,
4763) : Promise < void > => {
4864 if ( isEnabledHostElement ( element ) && hasPressEventHandler ( element ) ) {
@@ -63,8 +79,8 @@ const basePress = async (
6379 await basePress ( config , hostParentElement , options ) ;
6480} ;
6581
66- function isEnabledHostElement ( element : ReactTestInstance ) {
67- if ( ! isHostElement ( element ) || ! isPointerEventEnabled ( element ) ) {
82+ function isEnabledHostElement ( element : HostTestInstance ) {
83+ if ( ! isPointerEventEnabled ( element ) ) {
6884 return false ;
6985 }
7086
@@ -80,11 +96,11 @@ function isEnabledHostElement(element: ReactTestInstance) {
8096 return true ;
8197}
8298
83- function isEnabledTouchResponder ( element : ReactTestInstance ) {
99+ function isEnabledTouchResponder ( element : HostTestInstance ) {
84100 return isPointerEventEnabled ( element ) && element . props . onStartShouldSetResponder ?.( ) ;
85101}
86102
87- function hasPressEventHandler ( element : ReactTestInstance ) {
103+ function hasPressEventHandler ( element : HostTestInstance ) {
88104 return (
89105 getEventHandler ( element , 'press' ) ||
90106 getEventHandler ( element , 'longPress' ) ||
@@ -98,7 +114,7 @@ function hasPressEventHandler(element: ReactTestInstance) {
98114 */
99115async function emitDirectPressEvents (
100116 config : UserEventConfig ,
101- element : ReactTestInstance ,
117+ element : HostTestInstance ,
102118 options : BasePressOptions ,
103119) {
104120 await wait ( config ) ;
@@ -124,7 +140,7 @@ async function emitDirectPressEvents(
124140
125141async function emitPressabilityPressEvents (
126142 config : UserEventConfig ,
127- element : ReactTestInstance ,
143+ element : HostTestInstance ,
128144 options : BasePressOptions ,
129145) {
130146 await wait ( config ) ;
0 commit comments