mirror of
https://github.com/callstack/react-native-testing-library.git
synced 2026-09-18 23:09:04 +08:00
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>
This commit is contained in:
@@ -62,14 +62,14 @@ describe('fireEvent', () => {
|
||||
expect(onPressMock).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('should throw an Error when event handler was not found', () => {
|
||||
test('should not fire if the press handler is not passed to children', () => {
|
||||
const onPressMock = jest.fn();
|
||||
const { getByText } = render(
|
||||
<WithoutEventComponent onPress={() => 'this is not passed to children'} />
|
||||
);
|
||||
|
||||
expect(() => fireEvent(getByText('Without event'), 'press')).toThrow(
|
||||
'No handler function found for event: "press"'
|
||||
// TODO: this functionality is buggy, i.e. it will fail if we wrap this component with a View.
|
||||
<WithoutEventComponent onPress={onPressMock} />
|
||||
);
|
||||
fireEvent(getByText('Without event'), 'press');
|
||||
expect(onPressMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('should invoke event with custom name', () => {
|
||||
|
||||
+3
-20
@@ -1,6 +1,5 @@
|
||||
// @flow
|
||||
import act from './act';
|
||||
import { ErrorWithStack } from './helpers/errors';
|
||||
|
||||
const isHostElement = (element?: ReactTestInstance) => {
|
||||
return typeof element?.type === 'string';
|
||||
@@ -53,8 +52,7 @@ const findEventHandler = (
|
||||
element: ReactTestInstance,
|
||||
eventName: string,
|
||||
callsite?: any,
|
||||
nearestTouchResponder?: ReactTestInstance,
|
||||
hasDescendandHandler?: boolean
|
||||
nearestTouchResponder?: ReactTestInstance
|
||||
) => {
|
||||
const touchResponder = isTouchResponder(element)
|
||||
? element
|
||||
@@ -63,26 +61,11 @@ const findEventHandler = (
|
||||
const handler = getEventHandler(element, eventName);
|
||||
if (handler && isEventEnabled(element, touchResponder)) return handler;
|
||||
|
||||
// Do not bubble event to the root element
|
||||
const hasHandler = handler != null || hasDescendandHandler;
|
||||
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 null;
|
||||
}
|
||||
|
||||
return findEventHandler(
|
||||
element.parent,
|
||||
eventName,
|
||||
callsite,
|
||||
touchResponder,
|
||||
hasHandler
|
||||
);
|
||||
return findEventHandler(element.parent, eventName, callsite, touchResponder);
|
||||
};
|
||||
|
||||
const getEventHandler = (element: ReactTestInstance, eventName: string) => {
|
||||
|
||||
Reference in New Issue
Block a user