Skip to content

Commit 7ed56a2

Browse files
committed
basci impl
1 parent f8a0d44 commit 7ed56a2

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

src/fire-event.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { isPointerEventEnabled } from './helpers/pointer-events';
1313
import { isEditableTextInput } from './helpers/text-input';
1414
import { Point, StringWithAutocomplete } from './types';
1515
import { nativeState } from './native-state';
16+
import { formatElement } from './matchers/utils';
1617

1718
type EventHandler = (...args: unknown[]) => unknown;
1819

@@ -80,7 +81,15 @@ function findEventHandler(
8081
const touchResponder = isTouchResponder(element) ? element : nearestTouchResponder;
8182

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

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

130139
const handler = findEventHandler(element, eventName);
131140
if (!handler) {
141+
console.warn(
142+
`${formatElement(element, {
143+
minimal: true,
144+
})}: no "${eventName}" event handler found on element or any of it's ancestors`,
145+
);
132146
return;
133147
}
134148

src/matchers/utils.ts

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ReactTestInstance } from 'react-test-renderer';
1+
import { ReactTestInstance, ReactTestRendererJSON } from 'react-test-renderer';
22
import {
33
EXPECTED_COLOR,
44
RECEIVED_COLOR,
@@ -11,6 +11,7 @@ import prettyFormat, { plugins } from 'pretty-format';
1111
import redent from 'redent';
1212
import { isHostElement } from '../helpers/component-tree';
1313
import { defaultMapProps } from '../helpers/format-default';
14+
import { ElementType } from 'react';
1415

1516
class HostElementTypeError extends Error {
1617
constructor(received: unknown, matcherFn: jest.CustomMatcher, context: jest.MatcherContext) {
@@ -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
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
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)