Add tests for timestamps in events (#55799)

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

Changelog: [internal]

This adds Fantom tests for 2 recent changes in event dispatching in the React renderer:
- Verifies that event objects are exposed in the global scope during event dispatch.
- Verifies that event timestamps are read from native and use performance.now as fallback.

Reviewed By: huntie

Differential Revision: D85143175

fbshipit-source-id: 6fcc133239e0e510bbdec0b709e30ec7a350a85b
This commit is contained in:
Rubén Norte authored and meta-codesync[bot] committed 2026-02-27 08:21:44 -08:00
1 parent 7d4e5ef567
commit f7091b437e
1 file changed
+140 -1
@@ -14,12 +14,151 @@ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
import * as Fantom from '@react-native/fantom';
import nullthrows from 'nullthrows';
import * as React from 'react';
import {View} from 'react-native';
import {type PointerEvent, View} from 'react-native';
import * as FabricUIManager from 'react-native/Libraries/ReactNative/FabricUIManager';
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();
const ref = React.createRef<React.ElementRef<typeof View>>();
const onPointerUp = jest.fn((e: PointerEvent) => {
e.persist();
});
Fantom.runTask(() => {
root.render(<View ref={ref} onPointerUp={onPointerUp} />);
});
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,
);
});
it('provides the native event timestamp (lower case) when available', () => {
const root = Fantom.createRoot();
const ref = React.createRef<React.ElementRef<typeof View>>();
const onPointerUp = jest.fn((e: PointerEvent) => {
e.persist();
});
Fantom.runTask(() => {
root.render(<View ref={ref} onPointerUp={onPointerUp} />);
});
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,
);
});
it('provides a default timeStamp when the native event timeStamp is NOT available', () => {
const root = Fantom.createRoot();
const ref = React.createRef<React.ElementRef<typeof View>>();
const onPointerUp = jest.fn((e: PointerEvent) => {
e.persist();
});
Fantom.runTask(() => {
root.render(<View ref={ref} onPointerUp={onPointerUp} />);
});
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<React.ElementRef<typeof View>>();
let globalEventIsDispatchedEvent: boolean = false;
const onPointerUp = jest.fn((e: PointerEvent) => {
globalEventIsDispatchedEvent = global.event === e;
});
Fantom.runTask(() => {
root.render(<View ref={ref} onPointerUp={onPointerUp} />);
});
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();