Skip to content

Commit ec9b076

Browse files
committed
.
1 parent 9e7e744 commit ec9b076

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

src/fire-event.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function getEventHandlerState(
8585
return { enabled: true };
8686
}
8787

88-
return { enabled: false, reason: 'not a touch responder' };
88+
return { enabled: false, reason: 'not being a touch responder' };
8989
}
9090

9191
function findEventHandler(
@@ -104,8 +104,8 @@ function findEventHandler(
104104
} else {
105105
logger.warn(
106106
`FireEvent: "${eventName}" event handler is disabled on ${formatElement(element, {
107-
minimal: true,
108-
})} (${handlerState.reason}).`,
107+
compact: true,
108+
})} due to ${handlerState.reason}.`,
109109
);
110110
}
111111
}
@@ -159,7 +159,7 @@ function fireEvent(element: ReactTestInstance, eventName: EventName, ...data: un
159159
if (!handler) {
160160
logger.warn(
161161
`FireEvent: no enabled event handler for "${eventName}" found on ${formatElement(element, {
162-
minimal: true,
162+
compact: true,
163163
})} or its ancestors.`,
164164
);
165165
return;

src/helpers/format-element.ts

+39-17
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,48 @@ export function formatElement(
3131
const { children, ...props } = element.props;
3232
const childrenToDisplay = typeof children === 'string' ? [children] : undefined;
3333

34-
return prettyFormat(
35-
{
36-
// This prop is needed persuade the prettyFormat that the element is
37-
// a ReactTestRendererJSON instance, so it is formatted as JSX.
38-
$$typeof: Symbol.for('react.test.json'),
39-
type: `${element.type}`,
40-
props: mapProps ? mapProps(props) : props,
41-
children: childrenToDisplay,
42-
},
43-
// See: https://www.npmjs.com/package/pretty-format#usage-with-options
44-
{
45-
plugins: [plugins.ReactTestComponent, plugins.ReactElement],
46-
printFunctionName: false,
47-
printBasicPrototype: false,
48-
highlight: highlight,
49-
min: compact,
50-
},
34+
return (
35+
(typeof element.type === 'string' ? '' : 'composite ') +
36+
prettyFormat(
37+
{
38+
// This prop is needed persuade the prettyFormat that the element is
39+
// a ReactTestRendererJSON instance, so it is formatted as JSX.
40+
$$typeof: Symbol.for('react.test.json'),
41+
type: formatElementName(element.type),
42+
props: mapProps ? mapProps(props) : props,
43+
children: childrenToDisplay,
44+
},
45+
// See: https://www.npmjs.com/package/pretty-format#usage-with-options
46+
{
47+
plugins: [plugins.ReactTestComponent, plugins.ReactElement],
48+
printFunctionName: false,
49+
printBasicPrototype: false,
50+
highlight: highlight,
51+
min: compact,
52+
},
53+
)
5154
);
5255
}
5356

57+
function formatElementName(type: ReactTestInstance['type']) {
58+
if (typeof type === 'function') {
59+
return type.displayName ?? type.name;
60+
}
61+
62+
if (typeof type === 'object') {
63+
if ('type' in type) {
64+
// @ts-expect-error: despite typing this can happen for React.memo.
65+
return formatElementName(type.type);
66+
}
67+
if ('render' in type) {
68+
// @ts-expect-error: despite typing this can happen for React.forwardRefs.
69+
return formatElementName(type.render);
70+
}
71+
}
72+
73+
return `${type}`;
74+
}
75+
5476
export function formatElementList(elements: ReactTestInstance[], options?: FormatElementOptions) {
5577
if (elements.length === 0) {
5678
return '(no elements)';

0 commit comments

Comments
 (0)