Skip to content

Commit 41f5749

Browse files
committed
basci impl
1 parent d21dbb4 commit 41f5749

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

src/fire-event.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import type { ReactTestInstance } from 'react-test-renderer';
99
import act from './act';
1010
import { isElementMounted, isHostElement } from './helpers/component-tree';
1111
import { isHostScrollView, isHostTextInput } from './helpers/host-component-names';
12+
import { logger } from './helpers/logger';
1213
import { isPointerEventEnabled } from './helpers/pointer-events';
1314
import { isEditableTextInput } from './helpers/text-input';
15+
import { formatElement } from './matchers/utils';
1416
import { nativeState } from './native-state';
1517
import type { Point, StringWithAutocomplete } from './types';
1618

@@ -80,7 +82,15 @@ function findEventHandler(
8082
const touchResponder = isTouchResponder(element) ? element : nearestTouchResponder;
8183

8284
const handler = getEventHandler(element, eventName);
83-
if (handler && isEventEnabled(element, eventName, touchResponder)) return handler;
85+
if (handler) {
86+
if (isEventEnabled(element, eventName, touchResponder)) {
87+
return handler;
88+
} else {
89+
logger.warn(
90+
`${formatElement(element, { minimal: true })}: "${eventName}" event is not enabled.`,
91+
);
92+
}
93+
}
8494

8595
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain
8696
if (element.parent === null || element.parent.parent === null) {
@@ -129,6 +139,11 @@ function fireEvent(element: ReactTestInstance, eventName: EventName, ...data: un
129139

130140
const handler = findEventHandler(element, eventName);
131141
if (!handler) {
142+
logger.warn(
143+
`${formatElement(element, {
144+
minimal: true,
145+
})}: no "${eventName}" event handler found on element or any of it's ancestors`,
146+
);
132147
return;
133148
}
134149

src/matchers/utils.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ElementType } from 'react';
12
import type { ReactTestInstance } from 'react-test-renderer';
23
import {
34
EXPECTED_COLOR,
@@ -55,12 +56,20 @@ export function checkHostElement(
5556
}
5657
}
5758

59+
export type FormatElementOptions = {
60+
// Minimize used space.
61+
minimal?: boolean;
62+
};
63+
5864
/***
5965
* Format given element as a pretty-printed string.
6066
*
6167
* @param element Element to format.
6268
*/
63-
export function formatElement(element: ReactTestInstance | null) {
69+
export function formatElement(
70+
element: ReactTestInstance | null,
71+
{ minimal = false }: FormatElementOptions = {},
72+
) {
6473
if (element == null) {
6574
return ' null';
6675
}
@@ -74,7 +83,7 @@ export function formatElement(element: ReactTestInstance | null) {
7483
// This prop is needed persuade the prettyFormat that the element is
7584
// a ReactTestRendererJSON instance, so it is formatted as JSX.
7685
$$typeof: Symbol.for('react.test.json'),
77-
type: element.type,
86+
type: formatElementType(element.type),
7887
props: defaultMapProps(props),
7988
children: childrenToDisplay,
8089
},
@@ -83,18 +92,47 @@ export function formatElement(element: ReactTestInstance | null) {
8392
printFunctionName: false,
8493
printBasicPrototype: false,
8594
highlight: true,
95+
min: minimal,
8696
},
8797
),
8898
2,
8999
);
90100
}
91101

92-
export function formatElementArray(elements: ReactTestInstance[]) {
102+
export function formatElementType(type: ElementType): string {
103+
if (typeof type === 'function') {
104+
return type.displayName ?? type.name;
105+
}
106+
107+
// if (typeof type === 'object') {
108+
// console.log('OBJECT', type);
109+
// }
110+
111+
if (typeof type === 'object' && 'type' in type) {
112+
// @ts-expect-error: despite typing this can happen
113+
const nestedType = formatElementType(type.type);
114+
if (nestedType) {
115+
return nestedType;
116+
}
117+
}
118+
119+
if (typeof type === 'object' && 'render' in type) {
120+
// @ts-expect-error: despite typing this can happen
121+
const nestedType = formatElementType(type.render);
122+
if (nestedType) {
123+
return nestedType;
124+
}
125+
}
126+
127+
return `${type}`;
128+
}
129+
130+
export function formatElementArray(elements: ReactTestInstance[], options?: FormatElementOptions) {
93131
if (elements.length === 0) {
94132
return ' (no elements)';
95133
}
96134

97-
return redent(elements.map(formatElement).join('\n'), 2);
135+
return redent(elements.map((element) => formatElement(element, options)).join('\n'), 2);
98136
}
99137

100138
export function formatMessage(

0 commit comments

Comments
 (0)