diff --git a/packages/react-native/src/private/renderer/core/__tests__/EventDispatching-benchmark-itest.js b/packages/react-native/src/private/renderer/core/__tests__/EventDispatching-benchmark-itest.js index 2b2614bca94..c5955a43bd8 100644 --- a/packages/react-native/src/private/renderer/core/__tests__/EventDispatching-benchmark-itest.js +++ b/packages/react-native/src/private/renderer/core/__tests__/EventDispatching-benchmark-itest.js @@ -62,188 +62,26 @@ function createNestedViewsWithLayout( ); } -const {isOSS} = Fantom.getConstants(); - -if (isOSS) { - it('is not supported in OSS yet', () => { - expect(true).toBe(true); - }); -} else { - Fantom.unstable_benchmark - .suite('Event Dispatching') - .test( - 'dispatch event, flat (1 handler)', - () => { - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); +Fantom.unstable_benchmark + .suite('Event Dispatching') + .test( + 'dispatch event, flat (1 handler)', + () => { + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + }, + { + beforeAll: () => { + ref = React.createRef(); }, - { - beforeAll: () => { - ref = React.createRef(); - }, - beforeEach: () => { - root = Fantom.createRoot(); - Fantom.runTask(() => { - root.render( - {}} - style={{width: 10, height: 10}} - />, - ); - }); - }, - afterEach: () => { - root.destroy(); - }, - }, - ) - .test( - 'dispatch event, nested 10 deep (bubbling)', - () => { - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - }, - { - beforeAll: () => { - ref = React.createRef(); - }, - beforeEach: () => { - root = Fantom.createRoot(); - Fantom.runTask(() => { - root.render(createNestedViews(10, ref)); - }); - }, - afterEach: () => { - root.destroy(); - }, - }, - ) - .test( - 'dispatch event, nested 50 deep (bubbling)', - () => { - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - }, - { - beforeAll: () => { - ref = React.createRef(); - }, - beforeEach: () => { - root = Fantom.createRoot(); - Fantom.runTask(() => { - root.render(createNestedViews(50, ref)); - }); - }, - afterEach: () => { - root.destroy(); - }, - }, - ) - .test( - 'dispatch event, nested 10 deep (no handlers on ancestors)', - () => { - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - }, - { - beforeAll: () => { - ref = React.createRef(); - }, - beforeEach: () => { - root = Fantom.createRoot(); - Fantom.runTask(() => { - let views: React.MixedElement = ( - {}} - style={{width: 10, height: 10}} - /> - ); - for (let i = 0; i < 10; i++) { - views = {views}; - } - root.render(views); - }); - }, - afterEach: () => { - root.destroy(); - }, - }, - ) - .test( - 'dispatch event with stopPropagation, nested 10 deep', - () => { - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - }, - { - beforeAll: () => { - ref = React.createRef(); - }, - beforeEach: () => { - root = Fantom.createRoot(); - Fantom.runTask(() => { - let views: React.MixedElement = ( - { - e.stopPropagation(); - }} - style={{width: 10, height: 10}} - /> - ); - for (let i = 0; i < 10; i++) { - views = ( - {}}> - {views} - - ); - } - root.render(views); - }); - }, - afterEach: () => { - root.destroy(); - }, - }, - ) - .test( - 'render + dispatch, flat (handler update cost)', - () => { + beforeEach: () => { + root = Fantom.createRoot(); Fantom.runTask(() => { root.render( , ); }); - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); }, - { - beforeAll: () => { - ref = React.createRef(); + afterEach: () => { + root.destroy(); + }, + }, + ) + .test( + 'dispatch event, nested 10 deep (bubbling)', + () => { + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, }, - beforeEach: () => { - root = Fantom.createRoot(); - Fantom.runTask(() => { - root.render( - {}} - style={{width: 10, height: 10}} - />, + ); + }, + { + beforeAll: () => { + ref = React.createRef(); + }, + beforeEach: () => { + root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render(createNestedViews(10, ref)); + }); + }, + afterEach: () => { + root.destroy(); + }, + }, + ) + .test( + 'dispatch event, nested 50 deep (bubbling)', + () => { + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + }, + { + beforeAll: () => { + ref = React.createRef(); + }, + beforeEach: () => { + root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render(createNestedViews(50, ref)); + }); + }, + afterEach: () => { + root.destroy(); + }, + }, + ) + .test( + 'dispatch event, nested 10 deep (no handlers on ancestors)', + () => { + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + }, + { + beforeAll: () => { + ref = React.createRef(); + }, + beforeEach: () => { + root = Fantom.createRoot(); + Fantom.runTask(() => { + let views: React.MixedElement = ( + {}} + style={{width: 10, height: 10}} + /> + ); + for (let i = 0; i < 10; i++) { + views = {views}; + } + root.render(views); + }); + }, + afterEach: () => { + root.destroy(); + }, + }, + ) + .test( + 'dispatch event with stopPropagation, nested 10 deep', + () => { + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + }, + { + beforeAll: () => { + ref = React.createRef(); + }, + beforeEach: () => { + root = Fantom.createRoot(); + Fantom.runTask(() => { + let views: React.MixedElement = ( + { + e.stopPropagation(); + }} + style={{width: 10, height: 10}} + /> + ); + for (let i = 0; i < 10; i++) { + views = ( + {}}> + {views} + ); - }); - }, - afterEach: () => { - root.destroy(); - }, + } + root.render(views); + }); }, - ) - .test( - 'dispatch event, nested 50 deep (bubbling), stable tree', - () => { - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, + afterEach: () => { + root.destroy(); + }, + }, + ) + .test( + 'render + dispatch, flat (handler update cost)', + () => { + Fantom.runTask(() => { + root.render( + {}} + style={{width: 10, height: 10}} + />, ); - }, - { - beforeAll: () => { - ref = React.createRef(); - root = Fantom.createRoot(); - Fantom.runTask(() => { - root.render(createNestedViews(50, ref)); - }); - }, - afterAll: () => { - root.destroy(); + }); + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, }, + ); + }, + { + beforeAll: () => { + ref = React.createRef(); }, - ) - .test( - 'dispatch event, nested 50 deep (no handlers on ancestors), stable tree', - () => { - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); + beforeEach: () => { + root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render( + {}} + style={{width: 10, height: 10}} + />, + ); + }); }, - { - beforeAll: () => { - ref = React.createRef(); - root = Fantom.createRoot(); - Fantom.runTask(() => { - let views: React.MixedElement = ( - {}} - style={{width: 10, height: 10}} - /> - ); - for (let i = 0; i < 50; i++) { - views = {views}; - } - root.render(views); - }); - }, - afterAll: () => { - root.destroy(); - }, + afterEach: () => { + root.destroy(); }, - ) - .test( - 'dispatch onLayout event, flat (1 handler)', - () => { - Fantom.dispatchNativeEvent( - ref, - 'onLayout', - {layout: {x: 0, y: 0, width: 100, height: 50}}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - }, - { - beforeAll: () => { - ref = React.createRef(); - root = Fantom.createRoot(); - Fantom.runTask(() => { - root.render( - {}} - style={{width: 10, height: 10}} - />, - ); - }); - // Flush the layout events emitted during the initial layout pass so - // they are not measured as part of the dispatched event below. - Fantom.flushAllNativeEvents(); - }, - afterAll: () => { - root.destroy(); + }, + ) + .test( + 'dispatch event, nested 50 deep (bubbling), stable tree', + () => { + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, }, + ); + }, + { + beforeAll: () => { + ref = React.createRef(); + root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render(createNestedViews(50, ref)); + }); }, - ) - .test( - 'dispatch onLayout event, nested 10 deep (direct, no bubbling)', - () => { - Fantom.dispatchNativeEvent( - ref, - 'onLayout', - {layout: {x: 0, y: 0, width: 100, height: 50}}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); + afterAll: () => { + root.destroy(); }, - { - beforeAll: () => { - ref = React.createRef(); - root = Fantom.createRoot(); - Fantom.runTask(() => { - root.render(createNestedViewsWithLayout(10, ref)); - }); - Fantom.flushAllNativeEvents(); - }, - afterAll: () => { - root.destroy(); + }, + ) + .test( + 'dispatch event, nested 50 deep (no handlers on ancestors), stable tree', + () => { + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, }, + ); + }, + { + beforeAll: () => { + ref = React.createRef(); + root = Fantom.createRoot(); + Fantom.runTask(() => { + let views: React.MixedElement = ( + {}} + style={{width: 10, height: 10}} + /> + ); + for (let i = 0; i < 50; i++) { + views = {views}; + } + root.render(views); + }); }, - ) - .test( - 'dispatch onLayout event, nested 50 deep (direct, no bubbling)', - () => { - Fantom.dispatchNativeEvent( - ref, - 'onLayout', - {layout: {x: 0, y: 0, width: 100, height: 50}}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); + afterAll: () => { + root.destroy(); }, - { - beforeAll: () => { - ref = React.createRef(); - root = Fantom.createRoot(); - Fantom.runTask(() => { - root.render(createNestedViewsWithLayout(50, ref)); - }); - Fantom.flushAllNativeEvents(); - }, - afterAll: () => { - root.destroy(); + }, + ) + .test( + 'dispatch onLayout event, flat (1 handler)', + () => { + Fantom.dispatchNativeEvent( + ref, + 'onLayout', + {layout: {x: 0, y: 0, width: 100, height: 50}}, + { + category: Fantom.NativeEventCategory.Discrete, }, + ); + }, + { + beforeAll: () => { + ref = React.createRef(); + root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render( + {}} + style={{width: 10, height: 10}} + />, + ); + }); + // Flush the layout events emitted during the initial layout pass so + // they are not measured as part of the dispatched event below. + Fantom.flushAllNativeEvents(); }, - ); -} + afterAll: () => { + root.destroy(); + }, + }, + ) + .test( + 'dispatch onLayout event, nested 10 deep (direct, no bubbling)', + () => { + Fantom.dispatchNativeEvent( + ref, + 'onLayout', + {layout: {x: 0, y: 0, width: 100, height: 50}}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + }, + { + beforeAll: () => { + ref = React.createRef(); + root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render(createNestedViewsWithLayout(10, ref)); + }); + Fantom.flushAllNativeEvents(); + }, + afterAll: () => { + root.destroy(); + }, + }, + ) + .test( + 'dispatch onLayout event, nested 50 deep (direct, no bubbling)', + () => { + Fantom.dispatchNativeEvent( + ref, + 'onLayout', + {layout: {x: 0, y: 0, width: 100, height: 50}}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + }, + { + beforeAll: () => { + ref = React.createRef(); + root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render(createNestedViewsWithLayout(50, ref)); + }); + Fantom.flushAllNativeEvents(); + }, + afterAll: () => { + root.destroy(); + }, + }, + ); diff --git a/packages/react-native/src/private/renderer/core/__tests__/EventDispatching-itest.js b/packages/react-native/src/private/renderer/core/__tests__/EventDispatching-itest.js index 46339e19f51..39eaf48e52d 100644 --- a/packages/react-native/src/private/renderer/core/__tests__/EventDispatching-itest.js +++ b/packages/react-native/src/private/renderer/core/__tests__/EventDispatching-itest.js @@ -20,112 +20,106 @@ import * as FabricUIManager from 'react-native/Libraries/ReactNative/FabricUIMan const UIManager = nullthrows(FabricUIManager.getFabricUIManager()); describe('Event Dispatching', () => { - // The OSS renderer hasn't been synced yet to have these changes - // TODO(next-major) Remove this condition - if (!Fantom.getConstants().isOSS) { - it('provides the native event timeStamp (camel case) when available', () => { - const root = Fantom.createRoot(); + it('provides the native event timeStamp (camel case) when available', () => { + const root = Fantom.createRoot(); - const ref = React.createRef>(); + const ref = React.createRef>(); - const onPointerUp = jest.fn((e: PointerEvent) => { - e.persist(); - }); - - Fantom.runTask(() => { - root.render(); - }); - - expect(onPointerUp).toHaveBeenCalledTimes(0); - - const NATIVE_EVENT_TIMESTAMP = 1234; - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0, timeStamp: NATIVE_EVENT_TIMESTAMP}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(onPointerUp).toHaveBeenCalledTimes(1); - expect(onPointerUp.mock.calls[0][0].timeStamp).toBe( - NATIVE_EVENT_TIMESTAMP, - ); + const onPointerUp = jest.fn((e: PointerEvent) => { + e.persist(); }); - it('provides a default timeStamp when the native event timeStamp is NOT available', () => { - const root = Fantom.createRoot(); - - const ref = React.createRef>(); - - const onPointerUp = jest.fn((e: PointerEvent) => { - e.persist(); - }); - - Fantom.runTask(() => { - root.render(); - }); - - expect(onPointerUp).toHaveBeenCalledTimes(0); - - const lowerBound = performance.now(); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - const upperBound = performance.now(); - - expect(onPointerUp).toHaveBeenCalledTimes(1); - expect(onPointerUp.mock.calls[0][0].timeStamp).toBeGreaterThanOrEqual( - lowerBound, - ); - expect(onPointerUp.mock.calls[0][0].timeStamp).toBeLessThanOrEqual( - upperBound, - ); + Fantom.runTask(() => { + root.render(); }); - it('exposes dispatched events in the global scope', () => { - const root = Fantom.createRoot(); + expect(onPointerUp).toHaveBeenCalledTimes(0); - const ref = React.createRef>(); + const NATIVE_EVENT_TIMESTAMP = 1234; - let globalEventIsDispatchedEvent: boolean = false; + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0, timeStamp: NATIVE_EVENT_TIMESTAMP}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); - const onPointerUp = jest.fn((e: PointerEvent) => { - globalEventIsDispatchedEvent = global.event === e; - }); + expect(onPointerUp).toHaveBeenCalledTimes(1); + expect(onPointerUp.mock.calls[0][0].timeStamp).toBe(NATIVE_EVENT_TIMESTAMP); + }); - Fantom.runTask(() => { - root.render(); - }); + it('provides a default timeStamp when the native event timeStamp is NOT available', () => { + const root = Fantom.createRoot(); - const globalEventValueBeforeDispatch = {type: 'some-event'}; - global.event = globalEventValueBeforeDispatch; + const ref = React.createRef>(); - expect(onPointerUp).toHaveBeenCalledTimes(0); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(onPointerUp).toHaveBeenCalledTimes(1); - expect(globalEventIsDispatchedEvent).toBe(true); - expect(global.event).toBe(globalEventValueBeforeDispatch); + const onPointerUp = jest.fn((e: PointerEvent) => { + e.persist(); }); - } + + Fantom.runTask(() => { + root.render(); + }); + + expect(onPointerUp).toHaveBeenCalledTimes(0); + + const lowerBound = performance.now(); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + const upperBound = performance.now(); + + expect(onPointerUp).toHaveBeenCalledTimes(1); + expect(onPointerUp.mock.calls[0][0].timeStamp).toBeGreaterThanOrEqual( + lowerBound, + ); + expect(onPointerUp.mock.calls[0][0].timeStamp).toBeLessThanOrEqual( + upperBound, + ); + }); + + it('exposes dispatched events in the global scope', () => { + const root = Fantom.createRoot(); + + const ref = React.createRef>(); + + let globalEventIsDispatchedEvent: boolean = false; + + const onPointerUp = jest.fn((e: PointerEvent) => { + globalEventIsDispatchedEvent = global.event === e; + }); + + Fantom.runTask(() => { + root.render(); + }); + + const globalEventValueBeforeDispatch = {type: 'some-event'}; + global.event = globalEventValueBeforeDispatch; + + expect(onPointerUp).toHaveBeenCalledTimes(0); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(onPointerUp).toHaveBeenCalledTimes(1); + expect(globalEventIsDispatchedEvent).toBe(true); + expect(global.event).toBe(globalEventValueBeforeDispatch); + }); it('dispatches events with discrete priority', () => { const root = Fantom.createRoot(); diff --git a/packages/react-native/src/private/renderer/core/__tests__/EventTargetDispatching-itest.js b/packages/react-native/src/private/renderer/core/__tests__/EventTargetDispatching-itest.js index 9f3fd6984f4..6f29ab14d71 100644 --- a/packages/react-native/src/private/renderer/core/__tests__/EventTargetDispatching-itest.js +++ b/packages/react-native/src/private/renderer/core/__tests__/EventTargetDispatching-itest.js @@ -56,48 +56,532 @@ function createNestedViewsWithLayout( ); } -const {isOSS} = Fantom.getConstants(); +describe('EventTarget-based Event Dispatching', () => { + it('dispatches basic press event to handler', () => { + const root = Fantom.createRoot(); + const ref = React.createRef>(); + const onPointerUp = jest.fn(); -(isOSS ? describe.skip : describe)( - 'EventTarget-based Event Dispatching', - () => { - it('dispatches basic press event to handler', () => { + Fantom.runTask(() => { + root.render(); + }); + + expect(onPointerUp).toHaveBeenCalledTimes(0); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 10, y: 20}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(onPointerUp).toHaveBeenCalledTimes(1); + }); + + it('event bubbles from child to parent', () => { + const root = Fantom.createRoot(); + const childRef = React.createRef>(); + const parentHandler = jest.fn(); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + expect(parentHandler).toHaveBeenCalledTimes(0); + + Fantom.dispatchNativeEvent( + childRef, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(parentHandler).toHaveBeenCalledTimes(1); + }); + + it('capture phase fires before bubble phase', () => { + const root = Fantom.createRoot(); + const childRef = React.createRef>(); + const order: Array = []; + + Fantom.runTask(() => { + root.render( + { + order.push('parent-capture'); + }} + onPointerUp={() => { + order.push('parent-bubble'); + }}> + { + order.push('child-capture'); + }} + onPointerUp={() => { + order.push('child-bubble'); + }} + /> + , + ); + }); + + Fantom.dispatchNativeEvent( + childRef, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(order).toEqual([ + 'parent-capture', + 'child-capture', + 'child-bubble', + 'parent-bubble', + ]); + }); + + it('stopPropagation prevents parent handler from firing', () => { + const root = Fantom.createRoot(); + const childRef = React.createRef>(); + const parentHandler = jest.fn(); + const childHandler = jest.fn((e: PointerEvent) => { + e.stopPropagation(); + }); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + Fantom.dispatchNativeEvent( + childRef, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(childHandler).toHaveBeenCalledTimes(1); + expect(parentHandler).toHaveBeenCalledTimes(0); + }); + + it('event object has correct nativeEvent property', () => { + const root = Fantom.createRoot(); + const ref = React.createRef>(); + let capturedNativeEvent: NativePointerEvent | null = null; + + const onPointerUp = jest.fn((e: PointerEvent) => { + // Capture nativeEvent inside the handler because legacy SyntheticEvent + // nullifies properties after dispatch. + capturedNativeEvent = e.nativeEvent; + }); + + Fantom.runTask(() => { + root.render(); + }); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 42, y: 99}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(onPointerUp).toHaveBeenCalledTimes(1); + expect(capturedNativeEvent?.x).toBe(42); + expect(capturedNativeEvent?.y).toBe(99); + }); + + it('handler updates correctly when prop changes', () => { + const root = Fantom.createRoot(); + const ref = React.createRef>(); + const firstHandler = jest.fn(); + const secondHandler = jest.fn(); + + Fantom.runTask(() => { + root.render(); + }); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(firstHandler).toHaveBeenCalledTimes(1); + expect(secondHandler).toHaveBeenCalledTimes(0); + + // Re-render with a new handler + Fantom.runTask(() => { + root.render(); + }); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(firstHandler).toHaveBeenCalledTimes(1); + expect(secondHandler).toHaveBeenCalledTimes(1); + }); + + it('handler removal stops event dispatch', () => { + const root = Fantom.createRoot(); + const ref = React.createRef>(); + const handler = jest.fn(); + + Fantom.runTask(() => { + root.render(); + }); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(handler).toHaveBeenCalledTimes(1); + + // Re-render without the handler + Fantom.runTask(() => { + root.render(); + }); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(handler).toHaveBeenCalledTimes(1); + }); + + it('multiple event types on the same element dispatch correctly', () => { + const root = Fantom.createRoot(); + const ref = React.createRef>(); + const onPointerUp = jest.fn(); + const onPointerMove = jest.fn(); + + Fantom.runTask(() => { + root.render( + , + ); + }); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(onPointerUp).toHaveBeenCalledTimes(1); + expect(onPointerMove).toHaveBeenCalledTimes(0); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerMove', + {x: 1, y: 1}, + { + category: Fantom.NativeEventCategory.Continuous, + }, + ); + + expect(onPointerUp).toHaveBeenCalledTimes(1); + expect(onPointerMove).toHaveBeenCalledTimes(1); + }); + + it('preventDefault sets defaultPrevented to true', () => { + const root = Fantom.createRoot(); + const ref = React.createRef>(); + let defaultPrevented: ?boolean = false; + + const handler = jest.fn((e: PointerEvent) => { + e.preventDefault(); + defaultPrevented = e.defaultPrevented; + }); + + Fantom.runTask(() => { + root.render(); + }); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(handler).toHaveBeenCalledTimes(1); + expect(defaultPrevented).toBe(true); + }); + + it('isDefaultPrevented() returns true after preventDefault()', () => { + const root = Fantom.createRoot(); + const ref = React.createRef>(); + let result = false; + + const handler = jest.fn((e: PointerEvent) => { + e.preventDefault(); + result = e.isDefaultPrevented(); + }); + + Fantom.runTask(() => { + root.render(); + }); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(handler).toHaveBeenCalledTimes(1); + expect(result).toBe(true); + }); + + it('isDefaultPrevented() returns false when preventDefault() was not called', () => { + const root = Fantom.createRoot(); + const ref = React.createRef>(); + let result = true; + + const handler = jest.fn((e: PointerEvent) => { + result = e.isDefaultPrevented(); + }); + + Fantom.runTask(() => { + root.render(); + }); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(handler).toHaveBeenCalledTimes(1); + expect(result).toBe(false); + }); + + it('isPropagationStopped() returns true after stopPropagation()', () => { + const root = Fantom.createRoot(); + const ref = React.createRef>(); + let result = false; + + const handler = jest.fn((e: PointerEvent) => { + e.stopPropagation(); + result = e.isPropagationStopped(); + }); + + Fantom.runTask(() => { + root.render(); + }); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(handler).toHaveBeenCalledTimes(1); + expect(result).toBe(true); + }); + + it('isPropagationStopped() returns false when stopPropagation() was not called', () => { + const root = Fantom.createRoot(); + const ref = React.createRef>(); + let result = true; + + const handler = jest.fn((e: PointerEvent) => { + result = e.isPropagationStopped(); + }); + + Fantom.runTask(() => { + root.render(); + }); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(handler).toHaveBeenCalledTimes(1); + expect(result).toBe(false); + }); + + it('persist() is callable and does not throw', () => { + const root = Fantom.createRoot(); + const ref = React.createRef>(); + + const handler = jest.fn((e: PointerEvent) => { + e.persist(); + }); + + Fantom.runTask(() => { + root.render(); + }); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(handler).toHaveBeenCalledTimes(1); + }); + + // --- addEventListener / removeEventListener on refs --- + // These tests require both `enableNativeEventTargetEventDispatching` and + // `enableImperativeEvents` to be enabled, since the public `addEventListener` + // API on element refs is only available when both flags are on. They are + // skipped for the other flag combinations. + + (ReactNativeFeatureFlags.enableNativeEventTargetEventDispatching() && + ReactNativeFeatureFlags.enableImperativeEvents() + ? describe + : describe.skip)('addEventListener / removeEventListener', () => { + it('addEventListener on a ref receives dispatched events', () => { const root = Fantom.createRoot(); const ref = React.createRef>(); - const onPointerUp = jest.fn(); + const handler = jest.fn(); Fantom.runTask(() => { - root.render(); + root.render(); }); - expect(onPointerUp).toHaveBeenCalledTimes(0); + // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag + asEventTarget(ref.current).addEventListener('pointerup', handler); Fantom.dispatchNativeEvent( ref, 'onPointerUp', - {x: 10, y: 20}, + {x: 0, y: 0}, { category: Fantom.NativeEventCategory.Discrete, }, ); - expect(onPointerUp).toHaveBeenCalledTimes(1); + expect(handler).toHaveBeenCalledTimes(1); }); - it('event bubbles from child to parent', () => { + it('removeEventListener stops receiving events', () => { const root = Fantom.createRoot(); + const ref = React.createRef>(); + const handler = jest.fn(); + + Fantom.runTask(() => { + root.render(); + }); + + // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag + asEventTarget(ref.current).addEventListener('pointerup', handler); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(handler).toHaveBeenCalledTimes(1); + + asEventTarget(ref.current).removeEventListener('pointerup', handler); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(handler).toHaveBeenCalledTimes(1); + }); + + it('addEventListener with capture option fires during capture phase', () => { + const root = Fantom.createRoot(); + const parentRef = React.createRef>(); const childRef = React.createRef>(); - const parentHandler = jest.fn(); + const order: Array = []; Fantom.runTask(() => { root.render( - - + + { + order.push('child-bubble'); + }} + /> , ); }); - expect(parentHandler).toHaveBeenCalledTimes(0); + // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag + asEventTarget(parentRef.current).addEventListener( + 'pointerup', + () => { + order.push('parent-capture'); + }, + {capture: true}, + ); Fantom.dispatchNativeEvent( childRef, @@ -108,36 +592,233 @@ const {isOSS} = Fantom.getConstants(); }, ); - expect(parentHandler).toHaveBeenCalledTimes(1); + expect(order).toEqual(['parent-capture', 'child-bubble']); }); - it('capture phase fires before bubble phase', () => { + it('addEventListener receives events that bubble from children', () => { const root = Fantom.createRoot(); + const parentRef = React.createRef>(); + const childRef = React.createRef>(); + const handler = jest.fn(); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag + asEventTarget(parentRef.current).addEventListener('pointerup', handler); + + Fantom.dispatchNativeEvent( + childRef, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(handler).toHaveBeenCalledTimes(1); + }); + + // --- Declarative (prop) vs imperative (addEventListener) ordering --- + + it('declarative prop handler fires before imperative addEventListener listener', () => { + const root = Fantom.createRoot(); + const ref = React.createRef>(); + const order: Array = []; + + Fantom.runTask(() => { + root.render( + { + order.push('prop'); + }} + />, + ); + }); + + // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag + asEventTarget(ref.current).addEventListener('pointerup', () => { + order.push('addEventListener'); + }); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(order).toEqual(['prop', 'addEventListener']); + }); + + it('declarative capture prop fires before imperative capture addEventListener', () => { + const root = Fantom.createRoot(); + const parentRef = React.createRef>(); const childRef = React.createRef>(); const order: Array = []; Fantom.runTask(() => { root.render( { - order.push('parent-capture'); + order.push('parent-prop-capture'); + }}> + + , + ); + }); + + // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag + asEventTarget(parentRef.current).addEventListener( + 'pointerup', + () => { + order.push('parent-imperative-capture'); + }, + {capture: true}, + ); + + Fantom.dispatchNativeEvent( + childRef, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(order).toEqual([ + 'parent-prop-capture', + 'parent-imperative-capture', + ]); + }); + + it('stopImmediatePropagation in prop handler prevents addEventListener listeners', () => { + const root = Fantom.createRoot(); + const ref = React.createRef>(); + const imperativeHandler = jest.fn(); + + Fantom.runTask(() => { + root.render( + { + e.stopImmediatePropagation(); + }} + />, + ); + }); + + // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag + asEventTarget(ref.current).addEventListener( + 'pointerup', + imperativeHandler, + ); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(imperativeHandler).toHaveBeenCalledTimes(0); + }); + + it('stopImmediatePropagation in addEventListener does not affect prop handler', () => { + const root = Fantom.createRoot(); + const ref = React.createRef>(); + const propHandler = jest.fn(); + + Fantom.runTask(() => { + root.render(); + }); + + // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag + asEventTarget(ref.current).addEventListener( + 'pointerup', + (e: $FlowFixMe) => { + e.stopImmediatePropagation(); + }, + ); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + // Prop handler fires first, so it is not affected + expect(propHandler).toHaveBeenCalledTimes(1); + }); + + it('full dispatch order: capture props, capture imperative, bubble props, bubble imperative', () => { + const root = Fantom.createRoot(); + const parentRef = React.createRef>(); + const childRef = React.createRef>(); + const order: Array = []; + + Fantom.runTask(() => { + root.render( + { + order.push('parent-prop-capture'); }} onPointerUp={() => { - order.push('parent-bubble'); + order.push('parent-prop-bubble'); }}> { - order.push('child-capture'); + order.push('child-prop-capture'); }} onPointerUp={() => { - order.push('child-bubble'); + order.push('child-prop-bubble'); }} /> , ); }); + // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag + asEventTarget(parentRef.current).addEventListener( + 'pointerup', + () => { + order.push('parent-imperative-capture'); + }, + {capture: true}, + ); + // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag + asEventTarget(parentRef.current).addEventListener('pointerup', () => { + order.push('parent-imperative-bubble'); + }); + // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag + asEventTarget(childRef.current).addEventListener( + 'pointerup', + () => { + order.push('child-imperative-capture'); + }, + {capture: true}, + ); + // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag + asEventTarget(childRef.current).addEventListener('pointerup', () => { + order.push('child-imperative-bubble'); + }); + Fantom.dispatchNativeEvent( childRef, 'onPointerUp', @@ -148,1189 +829,84 @@ const {isOSS} = Fantom.getConstants(); ); expect(order).toEqual([ - 'parent-capture', - 'child-capture', - 'child-bubble', - 'parent-bubble', + 'parent-prop-capture', + 'parent-imperative-capture', + 'child-prop-capture', + 'child-imperative-capture', + 'child-prop-bubble', + 'child-imperative-bubble', + 'parent-prop-bubble', + 'parent-imperative-bubble', ]); }); + }); - it('stopPropagation prevents parent handler from firing', () => { + it('event has type and bubbles properties when using EventTarget dispatching', () => { + const root = Fantom.createRoot(); + const ref = React.createRef>(); + let eventType: unknown = null; + let eventBubbles: unknown = null; + + const handler = jest.fn((e: PointerEvent) => { + eventType = e.type; + eventBubbles = e.bubbles; + }); + + Fantom.runTask(() => { + root.render(); + }); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(handler).toHaveBeenCalledTimes(1); + // The legacy SyntheticEvent does not set type/bubbles as standard + // DOM Event properties. The new EventTarget-based path does. + if (eventType != null) { + expect(eventType).toBe('pointerup'); + expect(eventBubbles).toBe(true); + } + }); + + (ReactNativeFeatureFlags.enableNativeEventTargetEventDispatching() + ? it + : it.skip)( + 'event.target points to the original target and event.currentTarget changes at each step', + () => { const root = Fantom.createRoot(); + const parentRef = React.createRef>(); const childRef = React.createRef>(); - const parentHandler = jest.fn(); - const childHandler = jest.fn((e: PointerEvent) => { - e.stopPropagation(); - }); - - Fantom.runTask(() => { - root.render( - - - , - ); - }); - - Fantom.dispatchNativeEvent( - childRef, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(childHandler).toHaveBeenCalledTimes(1); - expect(parentHandler).toHaveBeenCalledTimes(0); - }); - - it('event object has correct nativeEvent property', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - let capturedNativeEvent: NativePointerEvent | null = null; - - const onPointerUp = jest.fn((e: PointerEvent) => { - // Capture nativeEvent inside the handler because legacy SyntheticEvent - // nullifies properties after dispatch. - capturedNativeEvent = e.nativeEvent; - }); - - Fantom.runTask(() => { - root.render(); - }); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 42, y: 99}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(onPointerUp).toHaveBeenCalledTimes(1); - expect(capturedNativeEvent?.x).toBe(42); - expect(capturedNativeEvent?.y).toBe(99); - }); - - it('handler updates correctly when prop changes', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - const firstHandler = jest.fn(); - const secondHandler = jest.fn(); - - Fantom.runTask(() => { - root.render(); - }); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(firstHandler).toHaveBeenCalledTimes(1); - expect(secondHandler).toHaveBeenCalledTimes(0); - - // Re-render with a new handler - Fantom.runTask(() => { - root.render(); - }); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(firstHandler).toHaveBeenCalledTimes(1); - expect(secondHandler).toHaveBeenCalledTimes(1); - }); - - it('handler removal stops event dispatch', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - const handler = jest.fn(); - - Fantom.runTask(() => { - root.render(); - }); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(handler).toHaveBeenCalledTimes(1); - - // Re-render without the handler - Fantom.runTask(() => { - root.render(); - }); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(handler).toHaveBeenCalledTimes(1); - }); - - it('multiple event types on the same element dispatch correctly', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - const onPointerUp = jest.fn(); - const onPointerMove = jest.fn(); + const targets: Array<{target: unknown, currentTarget: unknown}> = []; Fantom.runTask(() => { root.render( , - ); - }); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(onPointerUp).toHaveBeenCalledTimes(1); - expect(onPointerMove).toHaveBeenCalledTimes(0); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerMove', - {x: 1, y: 1}, - { - category: Fantom.NativeEventCategory.Continuous, - }, - ); - - expect(onPointerUp).toHaveBeenCalledTimes(1); - expect(onPointerMove).toHaveBeenCalledTimes(1); - }); - - it('preventDefault sets defaultPrevented to true', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - let defaultPrevented: ?boolean = false; - - const handler = jest.fn((e: PointerEvent) => { - e.preventDefault(); - defaultPrevented = e.defaultPrevented; - }); - - Fantom.runTask(() => { - root.render(); - }); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(handler).toHaveBeenCalledTimes(1); - expect(defaultPrevented).toBe(true); - }); - - it('isDefaultPrevented() returns true after preventDefault()', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - let result = false; - - const handler = jest.fn((e: PointerEvent) => { - e.preventDefault(); - result = e.isDefaultPrevented(); - }); - - Fantom.runTask(() => { - root.render(); - }); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(handler).toHaveBeenCalledTimes(1); - expect(result).toBe(true); - }); - - it('isDefaultPrevented() returns false when preventDefault() was not called', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - let result = true; - - const handler = jest.fn((e: PointerEvent) => { - result = e.isDefaultPrevented(); - }); - - Fantom.runTask(() => { - root.render(); - }); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(handler).toHaveBeenCalledTimes(1); - expect(result).toBe(false); - }); - - it('isPropagationStopped() returns true after stopPropagation()', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - let result = false; - - const handler = jest.fn((e: PointerEvent) => { - e.stopPropagation(); - result = e.isPropagationStopped(); - }); - - Fantom.runTask(() => { - root.render(); - }); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(handler).toHaveBeenCalledTimes(1); - expect(result).toBe(true); - }); - - it('isPropagationStopped() returns false when stopPropagation() was not called', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - let result = true; - - const handler = jest.fn((e: PointerEvent) => { - result = e.isPropagationStopped(); - }); - - Fantom.runTask(() => { - root.render(); - }); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(handler).toHaveBeenCalledTimes(1); - expect(result).toBe(false); - }); - - it('persist() is callable and does not throw', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - - const handler = jest.fn((e: PointerEvent) => { - e.persist(); - }); - - Fantom.runTask(() => { - root.render(); - }); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(handler).toHaveBeenCalledTimes(1); - }); - - // --- addEventListener / removeEventListener on refs --- - // These tests require both `enableNativeEventTargetEventDispatching` and - // `enableImperativeEvents` to be enabled, since the public `addEventListener` - // API on element refs is only available when both flags are on. They are - // skipped for the other flag combinations. - - (ReactNativeFeatureFlags.enableNativeEventTargetEventDispatching() && - ReactNativeFeatureFlags.enableImperativeEvents() - ? describe - : describe.skip)('addEventListener / removeEventListener', () => { - it('addEventListener on a ref receives dispatched events', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - const handler = jest.fn(); - - Fantom.runTask(() => { - root.render(); - }); - - // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag - asEventTarget(ref.current).addEventListener('pointerup', handler); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(handler).toHaveBeenCalledTimes(1); - }); - - it('removeEventListener stops receiving events', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - const handler = jest.fn(); - - Fantom.runTask(() => { - root.render(); - }); - - // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag - asEventTarget(ref.current).addEventListener('pointerup', handler); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(handler).toHaveBeenCalledTimes(1); - - asEventTarget(ref.current).removeEventListener('pointerup', handler); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(handler).toHaveBeenCalledTimes(1); - }); - - it('addEventListener with capture option fires during capture phase', () => { - const root = Fantom.createRoot(); - const parentRef = React.createRef>(); - const childRef = React.createRef>(); - const order: Array = []; - - Fantom.runTask(() => { - root.render( - - { - order.push('child-bubble'); - }} - /> - , - ); - }); - - // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag - asEventTarget(parentRef.current).addEventListener( - 'pointerup', - () => { - order.push('parent-capture'); - }, - {capture: true}, - ); - - Fantom.dispatchNativeEvent( - childRef, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(order).toEqual(['parent-capture', 'child-bubble']); - }); - - it('addEventListener receives events that bubble from children', () => { - const root = Fantom.createRoot(); - const parentRef = React.createRef>(); - const childRef = React.createRef>(); - const handler = jest.fn(); - - Fantom.runTask(() => { - root.render( - - - , - ); - }); - - // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag - asEventTarget(parentRef.current).addEventListener('pointerup', handler); - - Fantom.dispatchNativeEvent( - childRef, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(handler).toHaveBeenCalledTimes(1); - }); - - // --- Declarative (prop) vs imperative (addEventListener) ordering --- - - it('declarative prop handler fires before imperative addEventListener listener', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - const order: Array = []; - - Fantom.runTask(() => { - root.render( - { - order.push('prop'); - }} - />, - ); - }); - - // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag - asEventTarget(ref.current).addEventListener('pointerup', () => { - order.push('addEventListener'); - }); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(order).toEqual(['prop', 'addEventListener']); - }); - - it('declarative capture prop fires before imperative capture addEventListener', () => { - const root = Fantom.createRoot(); - const parentRef = React.createRef>(); - const childRef = React.createRef>(); - const order: Array = []; - - Fantom.runTask(() => { - root.render( - { - order.push('parent-prop-capture'); - }}> - - , - ); - }); - - // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag - asEventTarget(parentRef.current).addEventListener( - 'pointerup', - () => { - order.push('parent-imperative-capture'); - }, - {capture: true}, - ); - - Fantom.dispatchNativeEvent( - childRef, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(order).toEqual([ - 'parent-prop-capture', - 'parent-imperative-capture', - ]); - }); - - it('stopImmediatePropagation in prop handler prevents addEventListener listeners', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - const imperativeHandler = jest.fn(); - - Fantom.runTask(() => { - root.render( - { - e.stopImmediatePropagation(); - }} - />, - ); - }); - - // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag - asEventTarget(ref.current).addEventListener( - 'pointerup', - imperativeHandler, - ); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(imperativeHandler).toHaveBeenCalledTimes(0); - }); - - it('stopImmediatePropagation in addEventListener does not affect prop handler', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - const propHandler = jest.fn(); - - Fantom.runTask(() => { - root.render(); - }); - - // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag - asEventTarget(ref.current).addEventListener( - 'pointerup', - (e: $FlowFixMe) => { - e.stopImmediatePropagation(); - }, - ); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - // Prop handler fires first, so it is not affected - expect(propHandler).toHaveBeenCalledTimes(1); - }); - - it('full dispatch order: capture props, capture imperative, bubble props, bubble imperative', () => { - const root = Fantom.createRoot(); - const parentRef = React.createRef>(); - const childRef = React.createRef>(); - const order: Array = []; - - Fantom.runTask(() => { - root.render( - { - order.push('parent-prop-capture'); - }} - onPointerUp={() => { - order.push('parent-prop-bubble'); - }}> - { - order.push('child-prop-capture'); - }} - onPointerUp={() => { - order.push('child-prop-bubble'); - }} - /> - , - ); - }); - - // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag - asEventTarget(parentRef.current).addEventListener( - 'pointerup', - () => { - order.push('parent-imperative-capture'); - }, - {capture: true}, - ); - // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag - asEventTarget(parentRef.current).addEventListener('pointerup', () => { - order.push('parent-imperative-bubble'); - }); - // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag - asEventTarget(childRef.current).addEventListener( - 'pointerup', - () => { - order.push('child-imperative-capture'); - }, - {capture: true}, - ); - // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag - asEventTarget(childRef.current).addEventListener('pointerup', () => { - order.push('child-imperative-bubble'); - }); - - Fantom.dispatchNativeEvent( - childRef, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(order).toEqual([ - 'parent-prop-capture', - 'parent-imperative-capture', - 'child-prop-capture', - 'child-imperative-capture', - 'child-prop-bubble', - 'child-imperative-bubble', - 'parent-prop-bubble', - 'parent-imperative-bubble', - ]); - }); - }); - - it('event has type and bubbles properties when using EventTarget dispatching', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - let eventType: unknown = null; - let eventBubbles: unknown = null; - - const handler = jest.fn((e: PointerEvent) => { - eventType = e.type; - eventBubbles = e.bubbles; - }); - - Fantom.runTask(() => { - root.render(); - }); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(handler).toHaveBeenCalledTimes(1); - // The legacy SyntheticEvent does not set type/bubbles as standard - // DOM Event properties. The new EventTarget-based path does. - if (eventType != null) { - expect(eventType).toBe('pointerup'); - expect(eventBubbles).toBe(true); - } - }); - - (ReactNativeFeatureFlags.enableNativeEventTargetEventDispatching() - ? it - : it.skip)( - 'event.target points to the original target and event.currentTarget changes at each step', - () => { - const root = Fantom.createRoot(); - const parentRef = React.createRef>(); - const childRef = React.createRef>(); - const targets: Array<{target: unknown, currentTarget: unknown}> = []; - - Fantom.runTask(() => { - root.render( - { - targets.push({ - target: e.target, - currentTarget: e.currentTarget, - }); - }} - onPointerUp={(e: $FlowFixMe) => { - targets.push({ - target: e.target, - currentTarget: e.currentTarget, - }); - }}> - { - targets.push({ - target: e.target, - currentTarget: e.currentTarget, - }); - }} - /> - , - ); - }); - - Fantom.dispatchNativeEvent( - childRef, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(targets).toHaveLength(3); - - // event.target is always the original target element - expect(targets[0].target).toBe(childRef.current); - expect(targets[1].target).toBe(childRef.current); - expect(targets[2].target).toBe(childRef.current); - - // event.currentTarget changes at each propagation step - // Capture: parent - expect(targets[0].currentTarget).toBe(parentRef.current); - // Bubble: child, then parent - expect(targets[1].currentTarget).toBe(childRef.current); - expect(targets[2].currentTarget).toBe(parentRef.current); - }, - ); - - (ReactNativeFeatureFlags.enableNativeEventTargetEventDispatching() && - ReactNativeFeatureFlags.enableImperativeEvents() - ? it - : it.skip)( - 'direct (non-bubbling) events do not propagate via addEventListener', - () => { - const root = Fantom.createRoot(); - const parentRef = React.createRef>(); - const childRef = React.createRef>(); - const childHandler = jest.fn(); - const parentImperativeHandler = jest.fn(); - - Fantom.runTask(() => { - root.render( - - - , - ); - }); - - // Add an imperative listener on the parent for the 'layout' event. - // Since 'layout' is a direct (non-bubbling) event, this should NOT - // fire when we dispatch onLayout on the child. - // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag - asEventTarget(parentRef.current).addEventListener( - 'layout', - parentImperativeHandler, - ); - - const childCallsBefore = childHandler.mock.calls.length; - - Fantom.dispatchNativeEvent( - childRef, - 'onLayout', - {layout: {x: 0, y: 0, width: 100, height: 50}}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - // Child handler fires - expect( - childHandler.mock.calls.length - childCallsBefore, - ).toBeGreaterThan(0); - // Parent's addEventListener listener does NOT fire because layout - // is a non-bubbling (direct) event - expect(parentImperativeHandler).toHaveBeenCalledTimes(0); - }, - ); - - (ReactNativeFeatureFlags.enableNativeEventTargetEventDispatching() && - ReactNativeFeatureFlags.enableImperativeEvents() - ? describe - : describe.skip)('bubbling to document element and document', () => { - it('event bubbles from child up to the document element', () => { - const root = Fantom.createRoot(); - const childRef = React.createRef>(); - const documentElementHandler = jest.fn(); - - Fantom.runTask(() => { - root.render(); - }); - - asEventTarget(root.document.documentElement).addEventListener( - 'pointerup', - documentElementHandler, - ); - - Fantom.dispatchNativeEvent( - childRef, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(documentElementHandler).toHaveBeenCalledTimes(1); - }); - - it('event bubbles from child up to the document', () => { - const root = Fantom.createRoot(); - const childRef = React.createRef>(); - const documentHandler = jest.fn(); - - Fantom.runTask(() => { - root.render(); - }); - - asEventTarget(root.document).addEventListener( - 'pointerup', - documentHandler, - ); - - Fantom.dispatchNativeEvent( - childRef, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(documentHandler).toHaveBeenCalledTimes(1); - }); - - it('event bubbles from a deeply nested child up to document element and document', () => { - const root = Fantom.createRoot(); - const childRef = React.createRef>(); - const documentElementHandler = jest.fn(); - const documentHandler = jest.fn(); - - Fantom.runTask(() => { - root.render( - - - - - - - , - ); - }); - - asEventTarget(root.document.documentElement).addEventListener( - 'pointerup', - documentElementHandler, - ); - asEventTarget(root.document).addEventListener( - 'pointerup', - documentHandler, - ); - - Fantom.dispatchNativeEvent( - childRef, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(documentElementHandler).toHaveBeenCalledTimes(1); - expect(documentHandler).toHaveBeenCalledTimes(1); - }); - - it('capture phase on document fires before capture phase on document element', () => { - const root = Fantom.createRoot(); - const childRef = React.createRef>(); - const order: Array = []; - - Fantom.runTask(() => { - root.render( - { - order.push('child-capture'); - }} - onPointerUp={() => { - order.push('child-bubble'); - }} - />, - ); - }); - - asEventTarget(root.document).addEventListener( - 'pointerup', - () => { - order.push('document-capture'); - }, - {capture: true}, - ); - asEventTarget(root.document.documentElement).addEventListener( - 'pointerup', - () => { - order.push('documentElement-capture'); - }, - {capture: true}, - ); - asEventTarget(root.document.documentElement).addEventListener( - 'pointerup', - () => { - order.push('documentElement-bubble'); - }, - ); - asEventTarget(root.document).addEventListener('pointerup', () => { - order.push('document-bubble'); - }); - - Fantom.dispatchNativeEvent( - childRef, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(order).toEqual([ - 'document-capture', - 'documentElement-capture', - 'child-capture', - 'child-bubble', - 'documentElement-bubble', - 'document-bubble', - ]); - }); - - it('event.target points to the original child and event.currentTarget transitions through document element and document', () => { - const root = Fantom.createRoot(); - const childRef = React.createRef>(); - const targets: Array<{target: unknown, currentTarget: unknown}> = []; - - Fantom.runTask(() => { - root.render(); - }); - - asEventTarget(root.document.documentElement).addEventListener( - 'pointerup', - (e: $FlowFixMe) => { - targets.push({target: e.target, currentTarget: e.currentTarget}); - }, - ); - asEventTarget(root.document).addEventListener( - 'pointerup', - (e: $FlowFixMe) => { - targets.push({target: e.target, currentTarget: e.currentTarget}); - }, - ); - - Fantom.dispatchNativeEvent( - childRef, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(targets).toHaveLength(2); - - // event.target is always the original target element - expect(targets[0].target).toBe(childRef.current); - expect(targets[1].target).toBe(childRef.current); - - // event.currentTarget changes at each propagation step - expect(targets[0].currentTarget).toBe(root.document.documentElement); - expect(targets[1].currentTarget).toBe(root.document); - }); - - it('stopPropagation on document element prevents document handler from firing', () => { - const root = Fantom.createRoot(); - const childRef = React.createRef>(); - const documentHandler = jest.fn(); - const documentElementHandler = jest.fn((e: $FlowFixMe) => { - e.stopPropagation(); - }); - - Fantom.runTask(() => { - root.render(); - }); - - asEventTarget(root.document.documentElement).addEventListener( - 'pointerup', - documentElementHandler, - ); - asEventTarget(root.document).addEventListener( - 'pointerup', - documentHandler, - ); - - Fantom.dispatchNativeEvent( - childRef, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(documentElementHandler).toHaveBeenCalledTimes(1); - expect(documentHandler).toHaveBeenCalledTimes(0); - }); - - it('removeEventListener on document element stops events from being received', () => { - const root = Fantom.createRoot(); - const childRef = React.createRef>(); - const documentElementHandler = jest.fn(); - - Fantom.runTask(() => { - root.render(); - }); - - asEventTarget(root.document.documentElement).addEventListener( - 'pointerup', - documentElementHandler, - ); - - Fantom.dispatchNativeEvent( - childRef, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(documentElementHandler).toHaveBeenCalledTimes(1); - - asEventTarget(root.document.documentElement).removeEventListener( - 'pointerup', - documentElementHandler, - ); - - Fantom.dispatchNativeEvent( - childRef, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(documentElementHandler).toHaveBeenCalledTimes(1); - }); - - it('direct (non-bubbling) events do not reach the document element or document', () => { - const root = Fantom.createRoot(); - const childRef = React.createRef>(); - const childHandler = jest.fn(); - const documentElementHandler = jest.fn(); - const documentHandler = jest.fn(); - - Fantom.runTask(() => { - root.render(); - }); - - asEventTarget(root.document.documentElement).addEventListener( - 'layout', - documentElementHandler, - ); - asEventTarget(root.document).addEventListener( - 'layout', - documentHandler, - ); - - const childCallsBefore = childHandler.mock.calls.length; - - Fantom.dispatchNativeEvent( - childRef, - 'onLayout', - {layout: {x: 0, y: 0, width: 100, height: 50}}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - // Child handler fires - expect( - childHandler.mock.calls.length - childCallsBefore, - ).toBeGreaterThan(0); - // Non-bubbling events don't reach the document element or the document - expect(documentElementHandler).toHaveBeenCalledTimes(0); - expect(documentHandler).toHaveBeenCalledTimes(0); - }); - }); - - it('stopPropagation in capture phase prevents all bubble-phase handlers', () => { - const root = Fantom.createRoot(); - const childRef = React.createRef>(); - const order: Array = []; - - Fantom.runTask(() => { - root.render( - { - order.push('parent-capture'); - e.stopPropagation(); + ref={parentRef} + onPointerUpCapture={(e: $FlowFixMe) => { + targets.push({ + target: e.target, + currentTarget: e.currentTarget, + }); }} - onPointerUp={() => { - order.push('parent-bubble'); + onPointerUp={(e: $FlowFixMe) => { + targets.push({ + target: e.target, + currentTarget: e.currentTarget, + }); }}> { - order.push('child-capture'); - }} - onPointerUp={() => { - order.push('child-bubble'); + onPointerUp={(e: $FlowFixMe) => { + targets.push({ + target: e.target, + currentTarget: e.currentTarget, + }); }} /> , @@ -1346,434 +922,846 @@ const {isOSS} = Fantom.getConstants(); }, ); - // Only the parent capture handler should fire; everything else is stopped - expect(order).toEqual(['parent-capture']); + expect(targets).toHaveLength(3); + + // event.target is always the original target element + expect(targets[0].target).toBe(childRef.current); + expect(targets[1].target).toBe(childRef.current); + expect(targets[2].target).toBe(childRef.current); + + // event.currentTarget changes at each propagation step + // Capture: parent + expect(targets[0].currentTarget).toBe(parentRef.current); + // Bubble: child, then parent + expect(targets[1].currentTarget).toBe(childRef.current); + expect(targets[2].currentTarget).toBe(parentRef.current); + }, + ); + + (ReactNativeFeatureFlags.enableNativeEventTargetEventDispatching() && + ReactNativeFeatureFlags.enableImperativeEvents() + ? it + : it.skip)( + 'direct (non-bubbling) events do not propagate via addEventListener', + () => { + const root = Fantom.createRoot(); + const parentRef = React.createRef>(); + const childRef = React.createRef>(); + const childHandler = jest.fn(); + const parentImperativeHandler = jest.fn(); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + // Add an imperative listener on the parent for the 'layout' event. + // Since 'layout' is a direct (non-bubbling) event, this should NOT + // fire when we dispatch onLayout on the child. + // Temporary: ReadOnlyNode extends EventTarget at runtime behind feature flag + asEventTarget(parentRef.current).addEventListener( + 'layout', + parentImperativeHandler, + ); + + const childCallsBefore = childHandler.mock.calls.length; + + Fantom.dispatchNativeEvent( + childRef, + 'onLayout', + {layout: {x: 0, y: 0, width: 100, height: 50}}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + // Child handler fires + expect(childHandler.mock.calls.length - childCallsBefore).toBeGreaterThan( + 0, + ); + // Parent's addEventListener listener does NOT fire because layout + // is a non-bubbling (direct) event + expect(parentImperativeHandler).toHaveBeenCalledTimes(0); + }, + ); + + (ReactNativeFeatureFlags.enableNativeEventTargetEventDispatching() && + ReactNativeFeatureFlags.enableImperativeEvents() + ? describe + : describe.skip)('bubbling to document element and document', () => { + it('event bubbles from child up to the document element', () => { + const root = Fantom.createRoot(); + const childRef = React.createRef>(); + const documentElementHandler = jest.fn(); + + Fantom.runTask(() => { + root.render(); + }); + + asEventTarget(root.document.documentElement).addEventListener( + 'pointerup', + documentElementHandler, + ); + + Fantom.dispatchNativeEvent( + childRef, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(documentElementHandler).toHaveBeenCalledTimes(1); }); - describe('error handling', () => { - it('error in event handler does not break dispatch to subsequent listeners', () => { - const root = Fantom.createRoot(); - const childRef = React.createRef>(); - const parentHandler = jest.fn(); + it('event bubbles from child up to the document', () => { + const root = Fantom.createRoot(); + const childRef = React.createRef>(); + const documentHandler = jest.fn(); - Fantom.runTask(() => { - root.render( - - { - throw new Error('handler error'); - }} - /> - , - ); - }); - - const dispatch = () => - Fantom.dispatchNativeEvent( - childRef, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(dispatch).toThrow('handler error'); - - // The parent bubble handler should still fire despite child's error - expect(parentHandler).toHaveBeenCalledTimes(1); + Fantom.runTask(() => { + root.render(); }); + + asEventTarget(root.document).addEventListener( + 'pointerup', + documentHandler, + ); + + Fantom.dispatchNativeEvent( + childRef, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(documentHandler).toHaveBeenCalledTimes(1); }); - (ReactNativeFeatureFlags.enableNativeEventTargetEventDispatching() - ? describe - : describe.skip)('event timestamps', () => { - it('event preserves native timestamp from nativeEvent.timeStamp', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - let eventTimeStamp: unknown = null; + it('event bubbles from a deeply nested child up to document element and document', () => { + const root = Fantom.createRoot(); + const childRef = React.createRef>(); + const documentElementHandler = jest.fn(); + const documentHandler = jest.fn(); - Fantom.runTask(() => { - root.render( - { - eventTimeStamp = e.timeStamp; - }} - />, - ); - }); - - const nativeTimestamp = 12345.678; - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0, timeStamp: nativeTimestamp}, - {category: Fantom.NativeEventCategory.Discrete}, - ); - - expect(eventTimeStamp).toBe(nativeTimestamp); - }); - }); - - // --- dispatchConfig --- - - describe('dispatchConfig', () => { - it('includes phasedRegistrationNames on bubbling events', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - let capturedDispatchConfig: $FlowFixMe = null; - - Fantom.runTask(() => { - root.render( - { - capturedDispatchConfig = e.dispatchConfig; - }} - />, - ); - }); - - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - {category: Fantom.NativeEventCategory.Discrete}, - ); - - expect(capturedDispatchConfig).not.toBeNull(); - expect(capturedDispatchConfig.phasedRegistrationNames.bubbled).toBe( - 'onPointerUp', - ); - expect(capturedDispatchConfig.phasedRegistrationNames.captured).toBe( - 'onPointerUpCapture', - ); - }); - - it('includes registrationName on direct events', () => { - const root = Fantom.createRoot(); - const ref = React.createRef>(); - let capturedDispatchConfig: $FlowFixMe = null; - - Fantom.runTask(() => { - root.render( - { - capturedDispatchConfig = e.dispatchConfig; - }} - />, - ); - }); - - Fantom.dispatchNativeEvent( - ref, - 'onLayout', - {x: 0, y: 0, width: 100, height: 50}, - {category: Fantom.NativeEventCategory.Discrete}, - ); - - expect(capturedDispatchConfig).not.toBeNull(); - expect(capturedDispatchConfig.registrationName).toBe('onLayout'); - }); - }); - - // --- skipBubbling --- - - describe('skipBubbling (pointerenter / pointerleave)', () => { - it('does not bubble onPointerEnter to ancestor views', () => { - const root = Fantom.createRoot(); - - const childRef = React.createRef>(); - - const parentSpy = jest.fn((_e: PointerEvent) => {}); - const childSpy = jest.fn((_e: PointerEvent) => {}); - - Fantom.runTask(() => { - root.render( - - - , - ); - }); - - Fantom.dispatchNativeEvent( - childRef, - 'onPointerEnter', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.ContinuousStart, - }, - ); - - expect(childSpy).toHaveBeenCalledTimes(1); - expect(parentSpy).toHaveBeenCalledTimes(0); - }); - - it('does not bubble onPointerLeave to ancestor views', () => { - const root = Fantom.createRoot(); - - const childRef = React.createRef>(); - - const parentSpy = jest.fn((_e: PointerEvent) => {}); - const childSpy = jest.fn((_e: PointerEvent) => {}); - - Fantom.runTask(() => { - root.render( - - - , - ); - }); - - Fantom.dispatchNativeEvent( - childRef, - 'onPointerLeave', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.ContinuousEnd, - }, - ); - - expect(childSpy).toHaveBeenCalledTimes(1); - expect(parentSpy).toHaveBeenCalledTimes(0); - }); - - it('still fires onPointerEnterCapture on ancestors during the capture phase', () => { - const root = Fantom.createRoot(); - - const childRef = React.createRef>(); - - const callOrder: Array = []; - const parentCaptureSpy = jest.fn((_e: PointerEvent) => { - callOrder.push('parentCapture'); - }); - const childSpy = jest.fn((_e: PointerEvent) => { - callOrder.push('child'); - }); - - Fantom.runTask(() => { - root.render( - - - , - ); - }); - - Fantom.dispatchNativeEvent( - childRef, - 'onPointerEnter', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.ContinuousStart, - }, - ); - - expect(parentCaptureSpy).toHaveBeenCalledTimes(1); - expect(childSpy).toHaveBeenCalledTimes(1); - expect(callOrder).toEqual(['parentCapture', 'child']); - }); - - it('still bubbles non-skipBubbling events (onPointerDown) to ancestor views', () => { - const root = Fantom.createRoot(); - - const childRef = React.createRef>(); - - const parentSpy = jest.fn((_e: PointerEvent) => {}); - const childSpy = jest.fn((_e: PointerEvent) => {}); - - Fantom.runTask(() => { - root.render( - - - , - ); - }); - - Fantom.dispatchNativeEvent( - childRef, - 'onPointerDown', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); - - expect(childSpy).toHaveBeenCalledTimes(1); - 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>(); - - const parentSpy = jest.fn(); - const childSpy = jest.fn(); - - Fantom.runTask(() => { - root.render( - - - , - ); - }); - - // 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); - }); - }); - - describe('direct events (rnIsDirect) — Event construction validation', () => { - it('allows constructing a direct event that does not bubble', () => { - const event = new Event('layout', {rnIsDirect: true}); - expect(event.rnIsDirect).toBe(true); - expect(event.bubbles).toBe(false); - }); - - it('defaults rnIsDirect to false', () => { - const event = new Event('layout'); - expect(event.rnIsDirect).toBe(false); - }); - - it('throws when rnIsDirect and bubbles are both true', () => { - expect(() => { - // eslint-disable-next-line no-new - new Event('layout', {rnIsDirect: true, bubbles: true}); - }).toThrow( - "Failed to construct 'Event': 'rnIsDirect' cannot be true when 'bubbles' is also true.", - ); - }); - }); - - // The dispatch-behavior tests use `addEventListener` on refs and the - // document element, which is only available when both - // `enableNativeEventTargetEventDispatching` and `enableImperativeEvents` - // are enabled. - (ReactNativeFeatureFlags.enableNativeEventTargetEventDispatching() && - ReactNativeFeatureFlags.enableImperativeEvents() - ? describe - : describe.skip)('direct events (rnIsDirect) — dispatch behavior', () => { - it('dispatches onLayout to the target but never to ancestor onLayout props or bubble listeners', () => { - const root = Fantom.createRoot(); - const childRef = React.createRef>(); - const childHandler = jest.fn(); - const parentPropHandler = jest.fn(); - const parentImperativeBubble = jest.fn(); - - Fantom.runTask(() => { - root.render( - + Fantom.runTask(() => { + root.render( + + - - - + - , - ); - }); - - asEventTarget(root.document.documentElement).addEventListener( - 'layout', - parentImperativeBubble, - ); - - // Drain mount-time layout events. - Fantom.flushAllNativeEvents(); - - const childBefore = childHandler.mock.calls.length; - const parentBefore = parentPropHandler.mock.calls.length; - const parentBubbleBefore = parentImperativeBubble.mock.calls.length; - - Fantom.dispatchNativeEvent( - childRef, - 'onLayout', - {layout: {x: 0, y: 0, width: 100, height: 50}}, - {category: Fantom.NativeEventCategory.Discrete}, - ); - - // The target's own onLayout fires exactly once. - expect(childHandler.mock.calls.length - childBefore).toBe(1); - // A direct (non-bubbling) event never reaches an ancestor's onLayout - // prop or an ancestor's bubble-phase listener, regardless of the fast - // path. - expect(parentPropHandler.mock.calls.length - parentBefore).toBe(0); - expect( - parentImperativeBubble.mock.calls.length - parentBubbleBefore, - ).toBe(0); - }); - - it('restricts the event path to the target (AT_TARGET only, no ancestor capture listeners)', () => { - const root = Fantom.createRoot(); - const childRef = React.createRef>(); - const ancestorCapture = jest.fn(); - let observedPhase: number | null = null; - let observedPathLength: number | null = null; - let observedCurrentIsTarget: boolean | null = null; - - const handler = jest.fn((e: $FlowFixMe) => { - observedPhase = e.eventPhase; - observedPathLength = e.composedPath().length; - observedCurrentIsTarget = e.currentTarget === childRef.current; - }); - - Fantom.runTask(() => { - root.render(createNestedViewsWithLayout(5, childRef, () => {})); - }); - - asEventTarget(childRef.current).addEventListener('layout', handler); - asEventTarget(root.document.documentElement).addEventListener( - 'layout', - ancestorCapture, - {capture: true}, - ); - Fantom.flushAllNativeEvents(); - - const ancestorCaptureBefore = ancestorCapture.mock.calls.length; - - Fantom.dispatchNativeEvent( - childRef, - 'onLayout', - {layout: {x: 0, y: 0, width: 100, height: 50}}, - {category: Fantom.NativeEventCategory.Discrete}, - ); - - expect(handler).toHaveBeenCalled(); - expect(observedPhase).toBe(Event.AT_TARGET); - // Event path is just the target node. - expect(observedPathLength).toBe(1); - expect(observedCurrentIsTarget).toBe(true); - // With the fast path, the capture phase does not traverse ancestors. - expect(ancestorCapture.mock.calls.length - ancestorCaptureBefore).toBe( - 0, + + , ); }); + + asEventTarget(root.document.documentElement).addEventListener( + 'pointerup', + documentElementHandler, + ); + asEventTarget(root.document).addEventListener( + 'pointerup', + documentHandler, + ); + + Fantom.dispatchNativeEvent( + childRef, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(documentElementHandler).toHaveBeenCalledTimes(1); + expect(documentHandler).toHaveBeenCalledTimes(1); }); - }, -); + + it('capture phase on document fires before capture phase on document element', () => { + const root = Fantom.createRoot(); + const childRef = React.createRef>(); + const order: Array = []; + + Fantom.runTask(() => { + root.render( + { + order.push('child-capture'); + }} + onPointerUp={() => { + order.push('child-bubble'); + }} + />, + ); + }); + + asEventTarget(root.document).addEventListener( + 'pointerup', + () => { + order.push('document-capture'); + }, + {capture: true}, + ); + asEventTarget(root.document.documentElement).addEventListener( + 'pointerup', + () => { + order.push('documentElement-capture'); + }, + {capture: true}, + ); + asEventTarget(root.document.documentElement).addEventListener( + 'pointerup', + () => { + order.push('documentElement-bubble'); + }, + ); + asEventTarget(root.document).addEventListener('pointerup', () => { + order.push('document-bubble'); + }); + + Fantom.dispatchNativeEvent( + childRef, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(order).toEqual([ + 'document-capture', + 'documentElement-capture', + 'child-capture', + 'child-bubble', + 'documentElement-bubble', + 'document-bubble', + ]); + }); + + it('event.target points to the original child and event.currentTarget transitions through document element and document', () => { + const root = Fantom.createRoot(); + const childRef = React.createRef>(); + const targets: Array<{target: unknown, currentTarget: unknown}> = []; + + Fantom.runTask(() => { + root.render(); + }); + + asEventTarget(root.document.documentElement).addEventListener( + 'pointerup', + (e: $FlowFixMe) => { + targets.push({target: e.target, currentTarget: e.currentTarget}); + }, + ); + asEventTarget(root.document).addEventListener( + 'pointerup', + (e: $FlowFixMe) => { + targets.push({target: e.target, currentTarget: e.currentTarget}); + }, + ); + + Fantom.dispatchNativeEvent( + childRef, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(targets).toHaveLength(2); + + // event.target is always the original target element + expect(targets[0].target).toBe(childRef.current); + expect(targets[1].target).toBe(childRef.current); + + // event.currentTarget changes at each propagation step + expect(targets[0].currentTarget).toBe(root.document.documentElement); + expect(targets[1].currentTarget).toBe(root.document); + }); + + it('stopPropagation on document element prevents document handler from firing', () => { + const root = Fantom.createRoot(); + const childRef = React.createRef>(); + const documentHandler = jest.fn(); + const documentElementHandler = jest.fn((e: $FlowFixMe) => { + e.stopPropagation(); + }); + + Fantom.runTask(() => { + root.render(); + }); + + asEventTarget(root.document.documentElement).addEventListener( + 'pointerup', + documentElementHandler, + ); + asEventTarget(root.document).addEventListener( + 'pointerup', + documentHandler, + ); + + Fantom.dispatchNativeEvent( + childRef, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(documentElementHandler).toHaveBeenCalledTimes(1); + expect(documentHandler).toHaveBeenCalledTimes(0); + }); + + it('removeEventListener on document element stops events from being received', () => { + const root = Fantom.createRoot(); + const childRef = React.createRef>(); + const documentElementHandler = jest.fn(); + + Fantom.runTask(() => { + root.render(); + }); + + asEventTarget(root.document.documentElement).addEventListener( + 'pointerup', + documentElementHandler, + ); + + Fantom.dispatchNativeEvent( + childRef, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(documentElementHandler).toHaveBeenCalledTimes(1); + + asEventTarget(root.document.documentElement).removeEventListener( + 'pointerup', + documentElementHandler, + ); + + Fantom.dispatchNativeEvent( + childRef, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(documentElementHandler).toHaveBeenCalledTimes(1); + }); + + it('direct (non-bubbling) events do not reach the document element or document', () => { + const root = Fantom.createRoot(); + const childRef = React.createRef>(); + const childHandler = jest.fn(); + const documentElementHandler = jest.fn(); + const documentHandler = jest.fn(); + + Fantom.runTask(() => { + root.render(); + }); + + asEventTarget(root.document.documentElement).addEventListener( + 'layout', + documentElementHandler, + ); + asEventTarget(root.document).addEventListener('layout', documentHandler); + + const childCallsBefore = childHandler.mock.calls.length; + + Fantom.dispatchNativeEvent( + childRef, + 'onLayout', + {layout: {x: 0, y: 0, width: 100, height: 50}}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + // Child handler fires + expect(childHandler.mock.calls.length - childCallsBefore).toBeGreaterThan( + 0, + ); + // Non-bubbling events don't reach the document element or the document + expect(documentElementHandler).toHaveBeenCalledTimes(0); + expect(documentHandler).toHaveBeenCalledTimes(0); + }); + }); + + it('stopPropagation in capture phase prevents all bubble-phase handlers', () => { + const root = Fantom.createRoot(); + const childRef = React.createRef>(); + const order: Array = []; + + Fantom.runTask(() => { + root.render( + { + order.push('parent-capture'); + e.stopPropagation(); + }} + onPointerUp={() => { + order.push('parent-bubble'); + }}> + { + order.push('child-capture'); + }} + onPointerUp={() => { + order.push('child-bubble'); + }} + /> + , + ); + }); + + Fantom.dispatchNativeEvent( + childRef, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + // Only the parent capture handler should fire; everything else is stopped + expect(order).toEqual(['parent-capture']); + }); + + describe('error handling', () => { + it('error in event handler does not break dispatch to subsequent listeners', () => { + const root = Fantom.createRoot(); + const childRef = React.createRef>(); + const parentHandler = jest.fn(); + + Fantom.runTask(() => { + root.render( + + { + throw new Error('handler error'); + }} + /> + , + ); + }); + + const dispatch = () => + Fantom.dispatchNativeEvent( + childRef, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(dispatch).toThrow('handler error'); + + // The parent bubble handler should still fire despite child's error + expect(parentHandler).toHaveBeenCalledTimes(1); + }); + }); + + (ReactNativeFeatureFlags.enableNativeEventTargetEventDispatching() + ? describe + : describe.skip)('event timestamps', () => { + it('event preserves native timestamp from nativeEvent.timeStamp', () => { + const root = Fantom.createRoot(); + const ref = React.createRef>(); + let eventTimeStamp: unknown = null; + + Fantom.runTask(() => { + root.render( + { + eventTimeStamp = e.timeStamp; + }} + />, + ); + }); + + const nativeTimestamp = 12345.678; + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0, timeStamp: nativeTimestamp}, + {category: Fantom.NativeEventCategory.Discrete}, + ); + + expect(eventTimeStamp).toBe(nativeTimestamp); + }); + }); + + // --- dispatchConfig --- + + describe('dispatchConfig', () => { + it('includes phasedRegistrationNames on bubbling events', () => { + const root = Fantom.createRoot(); + const ref = React.createRef>(); + let capturedDispatchConfig: $FlowFixMe = null; + + Fantom.runTask(() => { + root.render( + { + capturedDispatchConfig = e.dispatchConfig; + }} + />, + ); + }); + + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + {category: Fantom.NativeEventCategory.Discrete}, + ); + + expect(capturedDispatchConfig).not.toBeNull(); + expect(capturedDispatchConfig.phasedRegistrationNames.bubbled).toBe( + 'onPointerUp', + ); + expect(capturedDispatchConfig.phasedRegistrationNames.captured).toBe( + 'onPointerUpCapture', + ); + }); + + it('includes registrationName on direct events', () => { + const root = Fantom.createRoot(); + const ref = React.createRef>(); + let capturedDispatchConfig: $FlowFixMe = null; + + Fantom.runTask(() => { + root.render( + { + capturedDispatchConfig = e.dispatchConfig; + }} + />, + ); + }); + + Fantom.dispatchNativeEvent( + ref, + 'onLayout', + {x: 0, y: 0, width: 100, height: 50}, + {category: Fantom.NativeEventCategory.Discrete}, + ); + + expect(capturedDispatchConfig).not.toBeNull(); + expect(capturedDispatchConfig.registrationName).toBe('onLayout'); + }); + }); + + // --- skipBubbling --- + + describe('skipBubbling (pointerenter / pointerleave)', () => { + it('does not bubble onPointerEnter to ancestor views', () => { + const root = Fantom.createRoot(); + + const childRef = React.createRef>(); + + const parentSpy = jest.fn((_e: PointerEvent) => {}); + const childSpy = jest.fn((_e: PointerEvent) => {}); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + Fantom.dispatchNativeEvent( + childRef, + 'onPointerEnter', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.ContinuousStart, + }, + ); + + expect(childSpy).toHaveBeenCalledTimes(1); + expect(parentSpy).toHaveBeenCalledTimes(0); + }); + + it('does not bubble onPointerLeave to ancestor views', () => { + const root = Fantom.createRoot(); + + const childRef = React.createRef>(); + + const parentSpy = jest.fn((_e: PointerEvent) => {}); + const childSpy = jest.fn((_e: PointerEvent) => {}); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + Fantom.dispatchNativeEvent( + childRef, + 'onPointerLeave', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.ContinuousEnd, + }, + ); + + expect(childSpy).toHaveBeenCalledTimes(1); + expect(parentSpy).toHaveBeenCalledTimes(0); + }); + + it('still fires onPointerEnterCapture on ancestors during the capture phase', () => { + const root = Fantom.createRoot(); + + const childRef = React.createRef>(); + + const callOrder: Array = []; + const parentCaptureSpy = jest.fn((_e: PointerEvent) => { + callOrder.push('parentCapture'); + }); + const childSpy = jest.fn((_e: PointerEvent) => { + callOrder.push('child'); + }); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + Fantom.dispatchNativeEvent( + childRef, + 'onPointerEnter', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.ContinuousStart, + }, + ); + + expect(parentCaptureSpy).toHaveBeenCalledTimes(1); + expect(childSpy).toHaveBeenCalledTimes(1); + expect(callOrder).toEqual(['parentCapture', 'child']); + }); + + it('still bubbles non-skipBubbling events (onPointerDown) to ancestor views', () => { + const root = Fantom.createRoot(); + + const childRef = React.createRef>(); + + const parentSpy = jest.fn((_e: PointerEvent) => {}); + const childSpy = jest.fn((_e: PointerEvent) => {}); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + Fantom.dispatchNativeEvent( + childRef, + 'onPointerDown', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); + + expect(childSpy).toHaveBeenCalledTimes(1); + 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>(); + + const parentSpy = jest.fn(); + const childSpy = jest.fn(); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + // 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); + }); + }); + + describe('direct events (rnIsDirect) — Event construction validation', () => { + it('allows constructing a direct event that does not bubble', () => { + const event = new Event('layout', {rnIsDirect: true}); + expect(event.rnIsDirect).toBe(true); + expect(event.bubbles).toBe(false); + }); + + it('defaults rnIsDirect to false', () => { + const event = new Event('layout'); + expect(event.rnIsDirect).toBe(false); + }); + + it('throws when rnIsDirect and bubbles are both true', () => { + expect(() => { + // eslint-disable-next-line no-new + new Event('layout', {rnIsDirect: true, bubbles: true}); + }).toThrow( + "Failed to construct 'Event': 'rnIsDirect' cannot be true when 'bubbles' is also true.", + ); + }); + }); + + // The dispatch-behavior tests use `addEventListener` on refs and the + // document element, which is only available when both + // `enableNativeEventTargetEventDispatching` and `enableImperativeEvents` + // are enabled. + (ReactNativeFeatureFlags.enableNativeEventTargetEventDispatching() && + ReactNativeFeatureFlags.enableImperativeEvents() + ? describe + : describe.skip)('direct events (rnIsDirect) — dispatch behavior', () => { + it('dispatches onLayout to the target but never to ancestor onLayout props or bubble listeners', () => { + const root = Fantom.createRoot(); + const childRef = React.createRef>(); + const childHandler = jest.fn(); + const parentPropHandler = jest.fn(); + const parentImperativeBubble = jest.fn(); + + Fantom.runTask(() => { + root.render( + + + + + + + , + ); + }); + + asEventTarget(root.document.documentElement).addEventListener( + 'layout', + parentImperativeBubble, + ); + + // Drain mount-time layout events. + Fantom.flushAllNativeEvents(); + + const childBefore = childHandler.mock.calls.length; + const parentBefore = parentPropHandler.mock.calls.length; + const parentBubbleBefore = parentImperativeBubble.mock.calls.length; + + Fantom.dispatchNativeEvent( + childRef, + 'onLayout', + {layout: {x: 0, y: 0, width: 100, height: 50}}, + {category: Fantom.NativeEventCategory.Discrete}, + ); + + // The target's own onLayout fires exactly once. + expect(childHandler.mock.calls.length - childBefore).toBe(1); + // A direct (non-bubbling) event never reaches an ancestor's onLayout + // prop or an ancestor's bubble-phase listener, regardless of the fast + // path. + expect(parentPropHandler.mock.calls.length - parentBefore).toBe(0); + expect( + parentImperativeBubble.mock.calls.length - parentBubbleBefore, + ).toBe(0); + }); + + it('restricts the event path to the target (AT_TARGET only, no ancestor capture listeners)', () => { + const root = Fantom.createRoot(); + const childRef = React.createRef>(); + const ancestorCapture = jest.fn(); + let observedPhase: number | null = null; + let observedPathLength: number | null = null; + let observedCurrentIsTarget: boolean | null = null; + + const handler = jest.fn((e: $FlowFixMe) => { + observedPhase = e.eventPhase; + observedPathLength = e.composedPath().length; + observedCurrentIsTarget = e.currentTarget === childRef.current; + }); + + Fantom.runTask(() => { + root.render(createNestedViewsWithLayout(5, childRef, () => {})); + }); + + asEventTarget(childRef.current).addEventListener('layout', handler); + asEventTarget(root.document.documentElement).addEventListener( + 'layout', + ancestorCapture, + {capture: true}, + ); + Fantom.flushAllNativeEvents(); + + const ancestorCaptureBefore = ancestorCapture.mock.calls.length; + + Fantom.dispatchNativeEvent( + childRef, + 'onLayout', + {layout: {x: 0, y: 0, width: 100, height: 50}}, + {category: Fantom.NativeEventCategory.Discrete}, + ); + + expect(handler).toHaveBeenCalled(); + expect(observedPhase).toBe(Event.AT_TARGET); + // Event path is just the target node. + expect(observedPathLength).toBe(1); + expect(observedCurrentIsTarget).toBe(true); + // With the fast path, the capture phase does not traverse ancestors. + expect(ancestorCapture.mock.calls.length - ancestorCaptureBefore).toBe(0); + }); + }); +}); diff --git a/packages/react-native/src/private/renderer/core/__tests__/ResponderEventTarget-itest.js b/packages/react-native/src/private/renderer/core/__tests__/ResponderEventTarget-itest.js index 58672b16e95..605d90317e3 100644 --- a/packages/react-native/src/private/renderer/core/__tests__/ResponderEventTarget-itest.js +++ b/packages/react-native/src/private/renderer/core/__tests__/ResponderEventTarget-itest.js @@ -46,9 +46,7 @@ function touchEnd( }; } -const {isOSS} = Fantom.getConstants(); - -(isOSS ? describe.skip : describe)('Responder System', () => { +describe('Responder System', () => { // --- Basic Grant / Release --- it('grants responder on touch start when onStartShouldSetResponder returns true', () => { diff --git a/packages/react-native/src/private/webapis/dom/nodes/__tests__/ReactNativeElement-itest.js b/packages/react-native/src/private/webapis/dom/nodes/__tests__/ReactNativeElement-itest.js index 63770292a84..b3b59967177 100644 --- a/packages/react-native/src/private/webapis/dom/nodes/__tests__/ReactNativeElement-itest.js +++ b/packages/react-native/src/private/webapis/dom/nodes/__tests__/ReactNativeElement-itest.js @@ -1698,8 +1698,6 @@ describe('ReactNativeElement', () => { // EventTarget API is gated behind `enableImperativeEvents`: when it is off // the methods are removed from this final class, when it is on they are // available. - const {isOSS} = Fantom.getConstants(); - if (!ReactNativeFeatureFlags.enableImperativeEvents()) { describe('when `enableImperativeEvents` is off (default)', () => { it('removes the public EventTarget methods', () => { @@ -1720,31 +1718,28 @@ describe('ReactNativeElement', () => { // Removing the public API must not affect native/prop event delivery, // which goes through the internal (symbol-keyed) dispatch path. - (isOSS ? it.skip : it)( - 'still delivers native events to prop handlers', - () => { - const ref = createRef(); - const onPointerUp = jest.fn(); - const root = Fantom.createRoot(); + it('still delivers native events to prop handlers', () => { + const ref = createRef(); + const onPointerUp = jest.fn(); + const root = Fantom.createRoot(); - Fantom.runTask(() => { - root.render(); - }); + Fantom.runTask(() => { + root.render(); + }); - expect(onPointerUp).toHaveBeenCalledTimes(0); + expect(onPointerUp).toHaveBeenCalledTimes(0); - Fantom.dispatchNativeEvent( - ref, - 'onPointerUp', - {x: 0, y: 0}, - { - category: Fantom.NativeEventCategory.Discrete, - }, - ); + Fantom.dispatchNativeEvent( + ref, + 'onPointerUp', + {x: 0, y: 0}, + { + category: Fantom.NativeEventCategory.Discrete, + }, + ); - expect(onPointerUp).toHaveBeenCalledTimes(1); - }, - ); + expect(onPointerUp).toHaveBeenCalledTimes(1); + }); }); }