Skip to content

fix: fire event should not throw for disabled handler #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/__tests__/fireEvent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,7 @@ test('should not fire on disabled TouchableOpacity', () => {
</TouchableOpacity>
);

expect(() => fireEvent.press(screen.getByText('Trigger'))).toThrow(
'No handler function found for event: "press"'
);
expect(() => fireEvent.press(screen.getByText('Trigger'))).not.toThrow();
expect(handlePress).not.toHaveBeenCalled();
});

Expand All @@ -187,9 +185,7 @@ test('should not fire on disabled Pressable', () => {
</Pressable>
);

expect(() => fireEvent.press(screen.getByText('Trigger'))).toThrow(
'No handler function found for event: "press"'
);
expect(() => fireEvent.press(screen.getByText('Trigger'))).not.toThrow();
expect(handlePress).not.toHaveBeenCalled();
});

Expand Down
42 changes: 30 additions & 12 deletions src/fireEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,48 @@ const findEventHandler = (
element: ReactTestInstance,
eventName: string,
callsite?: any,
nearestHostDescendent?: ReactTestInstance
nearestHostDescendent?: ReactTestInstance,
hasDescendandHandler?: boolean
) => {
const handler = getEventHandler(element, eventName);
const hasHandler = handler != null || hasDescendandHandler;

const isHostComponent = typeof element.type === 'string';
const hostElement = isHostComponent ? element : nearestHostDescendent;
const isEventEnabled =
hostElement?.props.onStartShouldSetResponder?.() !== false;
if (handler && isEventEnabled) return handler;

const eventHandlerName = toEventHandlerName(eventName);
// Do not bubble event to the root element
if (element.parent === null || element.parent.parent === null) {
if (hasHandler) return null;
else
throw new ErrorWithStack(
`No handler function found for event: "${eventName}"`,
callsite || invokeEvent
);
}

return findEventHandler(
element.parent,
eventName,
callsite,
hostElement,
hasHandler
);
};

if (typeof element.props[eventHandlerName] === 'function' && isEventEnabled) {
const getEventHandler = (element: ReactTestInstance, eventName: string) => {
const eventHandlerName = toEventHandlerName(eventName);
if (typeof element.props[eventHandlerName] === 'function') {
return element.props[eventHandlerName];
} else if (typeof element.props[eventName] === 'function' && isEventEnabled) {
return element.props[eventName];
}

// Do not bubble event to the root element
if (element.parent === null || element.parent.parent === null) {
throw new ErrorWithStack(
`No handler function found for event: "${eventName}"`,
callsite || invokeEvent
);
if (typeof element.props[eventName] === 'function') {
return element.props[eventName];
}

return findEventHandler(element.parent, eventName, callsite, hostElement);
return undefined;
};

const invokeEvent = (
Expand Down