Skip to content

Commit 8510d6d

Browse files
hontasPontus Lundinthymikee
authored
feat(breaking): do not throw because event handler was not found (#691)
* do not throw when event handler was not found * remove unnecessary hasDescendandHandler Co-authored-by: Pontus Lundin <pontus.lundin@klarna.com> Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
1 parent 62ccd08 commit 8510d6d

File tree

2 files changed

+9
-26
lines changed

2 files changed

+9
-26
lines changed

src/__tests__/fireEvent.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ describe('fireEvent', () => {
6262
expect(onPressMock).toHaveBeenCalled();
6363
});
6464

65-
test('should throw an Error when event handler was not found', () => {
65+
test('should not fire if the press handler is not passed to children', () => {
66+
const onPressMock = jest.fn();
6667
const { getByText } = render(
67-
<WithoutEventComponent onPress={() => 'this is not passed to children'} />
68-
);
69-
70-
expect(() => fireEvent(getByText('Without event'), 'press')).toThrow(
71-
'No handler function found for event: "press"'
68+
// TODO: this functionality is buggy, i.e. it will fail if we wrap this component with a View.
69+
<WithoutEventComponent onPress={onPressMock} />
7270
);
71+
fireEvent(getByText('Without event'), 'press');
72+
expect(onPressMock).not.toHaveBeenCalled();
7373
});
7474

7575
test('should invoke event with custom name', () => {

src/fireEvent.js

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// @flow
22
import act from './act';
3-
import { ErrorWithStack } from './helpers/errors';
43

54
const isHostElement = (element?: ReactTestInstance) => {
65
return typeof element?.type === 'string';
@@ -53,8 +52,7 @@ const findEventHandler = (
5352
element: ReactTestInstance,
5453
eventName: string,
5554
callsite?: any,
56-
nearestTouchResponder?: ReactTestInstance,
57-
hasDescendandHandler?: boolean
55+
nearestTouchResponder?: ReactTestInstance
5856
) => {
5957
const touchResponder = isTouchResponder(element)
6058
? element
@@ -63,26 +61,11 @@ const findEventHandler = (
6361
const handler = getEventHandler(element, eventName);
6462
if (handler && isEventEnabled(element, touchResponder)) return handler;
6563

66-
// Do not bubble event to the root element
67-
const hasHandler = handler != null || hasDescendandHandler;
6864
if (element.parent === null || element.parent.parent === null) {
69-
if (hasHandler) {
70-
return null;
71-
} else {
72-
throw new ErrorWithStack(
73-
`No handler function found for event: "${eventName}"`,
74-
callsite || invokeEvent
75-
);
76-
}
65+
return null;
7766
}
7867

79-
return findEventHandler(
80-
element.parent,
81-
eventName,
82-
callsite,
83-
touchResponder,
84-
hasHandler
85-
);
68+
return findEventHandler(element.parent, eventName, callsite, touchResponder);
8669
};
8770

8871
const getEventHandler = (element: ReactTestInstance, eventName: string) => {

0 commit comments

Comments
 (0)