Skip to content

Commit

Permalink
improve debug details
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjastrzebski committed Jan 8, 2025
1 parent 2391450 commit 6c7f3e0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
44 changes: 30 additions & 14 deletions src/fire-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,43 @@ const textInputEventsIgnoringEditableProp = new Set([
'onScroll',
]);

export function isEventEnabled(
type EventHandlerState = {
enabled: boolean;
reason?: string;
};

function getEventHandlerState(
element: ReactTestInstance,
eventName: string,
nearestTouchResponder?: ReactTestInstance,
) {
): EventHandlerState {
if (nearestTouchResponder != null && isHostTextInput(nearestTouchResponder)) {
return (
isEditableTextInput(nearestTouchResponder) ||
textInputEventsIgnoringEditableProp.has(eventName)
);
if (isEditableTextInput(nearestTouchResponder)) {
return { enabled: true };
}

if (textInputEventsIgnoringEditableProp.has(eventName)) {
return { enabled: true };
}

return { enabled: false, reason: '"editable" prop' };

Check warning on line 71 in src/fire-event.ts

View check run for this annotation

Codecov / codecov/patch

src/fire-event.ts#L66-L71

Added lines #L66 - L71 were not covered by tests
}

if (eventsAffectedByPointerEventsProp.has(eventName) && !isPointerEventEnabled(element)) {
return false;
return { enabled: false, reason: '"pointerEvents" prop' };
}

const touchStart = nearestTouchResponder?.props.onStartShouldSetResponder?.();
const touchMove = nearestTouchResponder?.props.onMoveShouldSetResponder?.();
if (touchStart || touchMove) {
return true;
return { enabled: true };
}

return touchStart === undefined && touchMove === undefined;
if (touchStart === undefined && touchMove === undefined) {
return { enabled: true };
}

return { enabled: false, reason: 'not a touch responder' };
}

function findEventHandler(
Expand All @@ -83,13 +97,15 @@ function findEventHandler(

const handler = getEventHandler(element, eventName);
if (handler) {
if (isEventEnabled(element, eventName, touchResponder)) {
const handlerState = getEventHandlerState(element, eventName, touchResponder);

if (handlerState.enabled) {
return handler;
} else {
logger.warn(
`FireEvent "${eventName}": event handler is disabled on ${formatElement(element, {
`FireEvent: "${eventName}" event handler is disabled on ${formatElement(element, {
minimal: true,
})}`,
})} (${handlerState.reason}).`,
);
}
}
Expand Down Expand Up @@ -142,9 +158,9 @@ function fireEvent(element: ReactTestInstance, eventName: EventName, ...data: un
const handler = findEventHandler(element, eventName);
if (!handler) {
logger.warn(
`FireEvent "${eventName}": no event handler found on ${formatElement(element, {
`FireEvent: no enabled event handler for "${eventName}" found on ${formatElement(element, {
minimal: true,
})} or its ancestors`,
})} or its ancestors.`,
);
return;
}
Expand Down
2 changes: 2 additions & 0 deletions src/helpers/format-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ const propsToDisplay = [
'aria-valuenow',
'aria-valuetext',
'defaultValue',
'editable',
'importantForAccessibility',
'nativeID',
'placeholder',
'pointerEvents',
'role',
'testID',
'title',
Expand Down
1 change: 0 additions & 1 deletion src/helpers/format-element.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ElementType } from 'react';
import { ReactTestInstance } from 'react-test-renderer';
import prettyFormat, { plugins } from 'pretty-format';
import redent from 'redent';
import { defaultMapProps } from './format-default';

export type FormatElementOptions = {
Expand Down
7 changes: 7 additions & 0 deletions src/user-event/utils/dispatch-event.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ReactTestInstance } from 'react-test-renderer';
import act from '../../act';
import { isElementMounted } from '../../helpers/component-tree';
import { formatElement } from '../../helpers/format-element';
import { logger } from '../../helpers/logger';

/**
* Basic dispatch event function used by User Event module.
Expand All @@ -16,6 +18,11 @@ export function dispatchEvent(element: ReactTestInstance, eventName: string, ...

const handler = getEventHandler(element, eventName);
if (!handler) {
logger.debug(
`User Event: no event handler for "${eventName}" found on ${formatElement(element, {
minimal: true,
})}`,
);
return;
}

Expand Down

0 comments on commit 6c7f3e0

Please sign in to comment.