Add Fantom test verifying onLayout does not bubble to ancestors (#57444)

Summary:
Pull Request resolved: https://github.com/react/react-native/pull/57444

`onLayout` is a direct (non-bubbling) event, so a layout event fired on a child should not invoke an ancestor's `onLayout` prop handler. Add a Fantom integration test that renders a parent and child `<View>`, both with `onLayout`, then dispatches a layout event on the child and asserts the parent's `onLayout` prop is not called as a result.

The initial layout pass emits a real layout event for every view that has `onLayout`, and those events are flushed lazily on the first dispatched event. The test flushes them up front and snapshots the call counts before dispatching, so the parent's own mount-time layout event is not mistaken for bubbling.

Changelog: [Internal]

Reviewed By: javache

Differential Revision: D110755346

fbshipit-source-id: ac9fa907ac623427192ed81dbfb2d8e55d8bc500
This commit is contained in:
Rubén Norte authored and meta-codesync[bot] committed 2026-07-06 04:54:38 -07:00
1 parent af3a6519b3
commit 2e36e63b64
1 file changed
+44
@@ -1582,5 +1582,49 @@ const {isOSS} = Fantom.getConstants();
expect(parentSpy).toHaveBeenCalledTimes(1);
});
});
describe('direct events (onLayout)', () => {
it('does not bubble onLayout to ancestor views via the onLayout prop', () => {
const root = Fantom.createRoot();
const childRef = React.createRef<React.ElementRef<typeof View>>();
const parentSpy = jest.fn();
const childSpy = jest.fn();
Fantom.runTask(() => {
root.render(
<View onLayout={parentSpy}>
<View ref={childRef} onLayout={childSpy} />
</View>,
);
});
// Flush the layout events emitted for both views during the initial
// layout pass so they are not conflated with the dispatch below.
Fantom.flushAllNativeEvents();
// Both onLayout handlers fired for their own layout during mount.
const childCallsBefore = childSpy.mock.calls.length;
const parentCallsBefore = parentSpy.mock.calls.length;
Fantom.dispatchNativeEvent(
childRef,
'onLayout',
{layout: {x: 0, y: 0, width: 100, height: 50}},
{
category: Fantom.NativeEventCategory.Discrete,
},
);
// Child's onLayout handler fires for the dispatched event
expect(childSpy.mock.calls.length - childCallsBefore).toBeGreaterThan(
0,
);
// Parent's onLayout prop handler does NOT fire from the child's event
// because layout is a direct (non-bubbling) event
expect(parentSpy.mock.calls.length - parentCallsBefore).toBe(0);
});
});
},
);