mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Use global DOM types in Fantom tests (#58569)
Summary: Pull Request resolved: https://github.com/react/react-native/pull/58569 Replace Fantom test imports of internal Event, EventTarget, AbortController, and feature flag modules with supported exports and global DOM APIs. Add the missing static Event phase constants to the DOM Flow declarations so constructor constants remain typed, and route trusted-event coverage through `dispatchNativeEvent`. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D120528709 fbshipit-source-id: 9d991904dbe1f4cb061a9629026a6fc3cbc3dd16
This commit is contained in:
1 parent
7a6314090d
commit
652d50111c
19 files changed
+69
-52
No files matched your search
@@ -620,7 +620,7 @@ type EventInit = Event$Init;
|
||||
|
||||
// https://dom.spec.whatwg.org/#interface-event
|
||||
declare class Event {
|
||||
constructor(type: string, eventInitDict?: Event$Init): void;
|
||||
constructor(type: string, eventInitDict?: ?Event$Init): void;
|
||||
/**
|
||||
* Returns the type of event, e.g. "click", "hashchange", or "submit".
|
||||
*/
|
||||
@@ -713,10 +713,17 @@ declare class Event {
|
||||
/** Non-standard. See Event.prototype.composed */
|
||||
readonly scoped: boolean;
|
||||
|
||||
cancelBubble: boolean;
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
initEvent(type: string, bubbles: boolean, cancelable: boolean): void;
|
||||
|
||||
static readonly NONE: 0;
|
||||
static readonly CAPTURING_PHASE: 1;
|
||||
static readonly AT_TARGET: 2;
|
||||
static readonly BUBBLING_PHASE: 3;
|
||||
}
|
||||
|
||||
type CustomEvent$Init = {...Event$Init, detail?: any, ...};
|
||||
|
||||
Vendored
+1
-1
@@ -15,7 +15,7 @@ import {createShadowNodeReferenceGetterRef} from '../ShadowNodeRevisionGetter';
|
||||
import * as Fantom from '@react-native/fantom';
|
||||
import * as React from 'react';
|
||||
import {ScrollView, View} from 'react-native';
|
||||
import * as ReactNativeFeatureFlags from 'react-native/src/private/featureflags/ReactNativeFeatureFlags';
|
||||
import {ReactNativeFeatureFlags} from 'react-native/react-private-interface';
|
||||
|
||||
test('base case when cloning results in revision +1', () => {
|
||||
const root = Fantom.createRoot();
|
||||
|
||||
+4
-2
@@ -17,8 +17,7 @@ import type {NativePointerEvent, PointerEvent} from 'react-native';
|
||||
import * as Fantom from '@react-native/fantom';
|
||||
import * as React from 'react';
|
||||
import {View} from 'react-native';
|
||||
import * as ReactNativeFeatureFlags from 'react-native/src/private/featureflags/ReactNativeFeatureFlags';
|
||||
import Event from 'react-native/src/private/webapis/dom/events/Event';
|
||||
import {ReactNativeFeatureFlags} from 'react-native/react-private-interface';
|
||||
|
||||
// Temporary cast until ReadOnlyNode extends EventTarget ungated.
|
||||
function asEventTarget(node: ?interface {}): EventTarget {
|
||||
@@ -1640,12 +1639,14 @@ describe('EventTarget-based Event Dispatching', () => {
|
||||
describe('direct events (rnIsDirect) — Event construction validation', () => {
|
||||
it('allows constructing a direct event that does not bubble', () => {
|
||||
const event = new Event('layout', {rnIsDirect: true});
|
||||
// $FlowFixMe[prop-missing] React Native-specific Event field.
|
||||
expect(event.rnIsDirect).toBe(true);
|
||||
expect(event.bubbles).toBe(false);
|
||||
});
|
||||
|
||||
it('defaults rnIsDirect to false', () => {
|
||||
const event = new Event('layout');
|
||||
// $FlowFixMe[prop-missing] React Native-specific Event field.
|
||||
expect(event.rnIsDirect).toBe(false);
|
||||
});
|
||||
|
||||
@@ -1752,6 +1753,7 @@ describe('EventTarget-based Event Dispatching', () => {
|
||||
);
|
||||
|
||||
expect(handler).toHaveBeenCalled();
|
||||
// $FlowFixMe[prop-missing] Flow's global Event type omits static constants.
|
||||
expect(observedPhase).toBe(Event.AT_TARGET);
|
||||
// Event path is just the target node.
|
||||
expect(observedPathLength).toBe(1);
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
* @format
|
||||
*/
|
||||
|
||||
import type EventTarget from '../../webapis/dom/events/EventTarget';
|
||||
import type InternalEventTarget from '../../webapis/dom/events/EventTarget';
|
||||
|
||||
import {
|
||||
customBubblingEventTypes,
|
||||
@@ -41,8 +41,11 @@ export default function dispatchNativeEvent(
|
||||
type: string,
|
||||
payload: {[string]: unknown},
|
||||
): void {
|
||||
// $FlowFixMe[incompatible-type] The global is backed by this implementation.
|
||||
const internalTarget = target as InternalEventTarget;
|
||||
|
||||
// Process responder events before normal event dispatch.
|
||||
processResponderEvent(type, target, payload);
|
||||
processResponderEvent(type, internalTarget, payload);
|
||||
|
||||
try {
|
||||
// Normal EventTarget dispatch
|
||||
@@ -117,7 +120,7 @@ export default function dispatchNativeEvent(
|
||||
// rethrown synchronously (matching the legacy plugin path) rather than
|
||||
// deferred to a new task, keeping it catchable by React error boundaries
|
||||
// and the native event call.
|
||||
dispatchTrustedEvent(target, syntheticEvent, true);
|
||||
dispatchTrustedEvent(internalTarget, syntheticEvent, true);
|
||||
}
|
||||
} finally {
|
||||
// Rethrow the first error caught during responder lifecycle dispatch,
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@
|
||||
|
||||
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
|
||||
|
||||
import * as ReactNativeFeatureFlags from 'react-native/src/private/featureflags/ReactNativeFeatureFlags';
|
||||
import {ReactNativeFeatureFlags} from 'react-native/react-private-interface';
|
||||
|
||||
declare var IntersectionObserverEntry: unknown;
|
||||
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@
|
||||
|
||||
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
|
||||
|
||||
import * as ReactNativeFeatureFlags from 'react-native/src/private/featureflags/ReactNativeFeatureFlags';
|
||||
import {ReactNativeFeatureFlags} from 'react-native/react-private-interface';
|
||||
|
||||
declare var MutationRecord: unknown;
|
||||
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@
|
||||
|
||||
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
|
||||
|
||||
import * as ReactNativeFeatureFlags from 'react-native/src/private/featureflags/ReactNativeFeatureFlags';
|
||||
import {ReactNativeFeatureFlags} from 'react-native/react-private-interface';
|
||||
|
||||
declare var ResizeObserverEntry: unknown;
|
||||
declare var ResizeObserverSize: unknown;
|
||||
|
||||
-4
@@ -13,11 +13,7 @@ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
|
||||
import type {TimerMock} from '@react-native/fantom';
|
||||
|
||||
import DOMException from '../../../errors/DOMException';
|
||||
import {AbortController} from '../AbortController';
|
||||
import {AbortSignal_public as AbortSignal} from '../AbortSignal';
|
||||
import * as Fantom from '@react-native/fantom';
|
||||
import Event from 'react-native/src/private/webapis/dom/events/Event';
|
||||
import EventTarget from 'react-native/src/private/webapis/dom/events/EventTarget';
|
||||
|
||||
let listenerCallOrder = 0;
|
||||
|
||||
|
||||
+10
-6
@@ -10,12 +10,15 @@
|
||||
|
||||
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
|
||||
|
||||
import Event from 'react-native/src/private/webapis/dom/events/Event';
|
||||
import {
|
||||
setEventInitTimeStamp,
|
||||
setInPassiveListenerFlag,
|
||||
} from 'react-native/src/private/webapis/dom/events/internals/EventInternals';
|
||||
|
||||
type EventOptions = EventInit & {
|
||||
rnIsDirect?: boolean,
|
||||
};
|
||||
|
||||
describe('Event', () => {
|
||||
it('provides read-only constants for event phases', () => {
|
||||
'use strict';
|
||||
@@ -109,6 +112,7 @@ describe('Event', () => {
|
||||
);
|
||||
|
||||
expect(() => {
|
||||
// $FlowFixMe[incompatible-type] The React Native implementation accepts null.
|
||||
return new Event('custom', null);
|
||||
}).not.toThrow();
|
||||
|
||||
@@ -121,13 +125,12 @@ describe('Event', () => {
|
||||
}).not.toThrow();
|
||||
|
||||
expect(() => {
|
||||
// $FlowExpectedError[incompatible-exact]
|
||||
// $FlowExpectedError[prop-missing]
|
||||
// $FlowExpectedError[incompatible-type]
|
||||
return new Event('custom', class {});
|
||||
}).not.toThrow();
|
||||
|
||||
expect(() => {
|
||||
// $FlowExpectedError[incompatible-exact]
|
||||
// $FlowExpectedError[incompatible-type]
|
||||
return new Event('custom', () => {});
|
||||
}).not.toThrow();
|
||||
});
|
||||
@@ -238,7 +241,7 @@ describe('Event', () => {
|
||||
|
||||
it('should use a custom timestamp when set via setEventInitTimeStamp', () => {
|
||||
const customTimestamp = 12345.678;
|
||||
const options = {};
|
||||
const options: EventOptions = {};
|
||||
setEventInitTimeStamp(options, customTimestamp);
|
||||
const event = new Event('custom', options);
|
||||
|
||||
@@ -246,7 +249,7 @@ describe('Event', () => {
|
||||
});
|
||||
|
||||
it('should accept zero as a valid custom timestamp', () => {
|
||||
const options = {};
|
||||
const options: EventOptions = {};
|
||||
setEventInitTimeStamp(options, 0);
|
||||
const event = new Event('custom', options);
|
||||
|
||||
@@ -300,6 +303,7 @@ describe('Event', () => {
|
||||
|
||||
expect(event.defaultPrevented).toBe(false);
|
||||
|
||||
// $FlowFixMe[incompatible-type] The global is backed by this implementation.
|
||||
setInPassiveListenerFlag(event, true);
|
||||
|
||||
event.preventDefault();
|
||||
|
||||
Vendored
+4
-4
@@ -12,21 +12,21 @@
|
||||
|
||||
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
|
||||
|
||||
import type {EventCallback} from 'react-native/src/private/webapis/dom/events/EventTarget';
|
||||
|
||||
import Event from 'react-native/src/private/webapis/dom/events/Event';
|
||||
import {
|
||||
getEventHandlerAttribute,
|
||||
setEventHandlerAttribute,
|
||||
} from 'react-native/src/private/webapis/dom/events/EventHandlerAttributes';
|
||||
import EventTarget from 'react-native/src/private/webapis/dom/events/EventTarget';
|
||||
|
||||
type EventCallback = (event: Event) => void;
|
||||
|
||||
class EventTargetSubclass extends EventTarget {
|
||||
get oncustomevent(): EventCallback | null {
|
||||
// $FlowFixMe[incompatible-type] The globals are backed by these implementations.
|
||||
return getEventHandlerAttribute(this, 'customEvent');
|
||||
}
|
||||
|
||||
set oncustomevent(listener: ?EventCallback) {
|
||||
// $FlowFixMe[incompatible-type] The globals are backed by these implementations.
|
||||
setEventHandlerAttribute(this, 'customEvent', listener);
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
-2
@@ -13,8 +13,6 @@ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
|
||||
|
||||
import createEventTargetHierarchyWithDepth from './createEventTargetHierarchyWithDepth';
|
||||
import {unstable_benchmark} from '@react-native/fantom';
|
||||
import Event from 'react-native/src/private/webapis/dom/events/Event';
|
||||
import EventTarget from 'react-native/src/private/webapis/dom/events/EventTarget';
|
||||
import {EVENT_TARGET_GET_DECLARATIVE_LISTENER_KEY} from 'react-native/src/private/webapis/dom/events/internals/EventTargetInternals';
|
||||
|
||||
let event: Event;
|
||||
|
||||
+27
-14
@@ -11,9 +11,13 @@
|
||||
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
|
||||
|
||||
import createEventTargetHierarchyWithDepth from './createEventTargetHierarchyWithDepth';
|
||||
import Event from 'react-native/src/private/webapis/dom/events/Event';
|
||||
import EventTarget from 'react-native/src/private/webapis/dom/events/EventTarget';
|
||||
import {dispatchTrustedEvent} from 'react-native/src/private/webapis/dom/events/internals/EventTargetInternals';
|
||||
import {
|
||||
ReactNativeViewConfigRegistry,
|
||||
dispatchNativeEvent,
|
||||
} from 'react-native/react-private-interface';
|
||||
|
||||
const NATIVE_CUSTOM_EVENT_TYPE = 'topEventTargetTest';
|
||||
const CUSTOM_EVENT_TYPE = 'eventtargettest';
|
||||
|
||||
let listenerCallOrder = 0;
|
||||
|
||||
@@ -54,6 +58,18 @@ function createListener(
|
||||
}
|
||||
|
||||
describe('EventTarget', () => {
|
||||
beforeAll(() => {
|
||||
ReactNativeViewConfigRegistry.customDirectEventTypes[
|
||||
NATIVE_CUSTOM_EVENT_TYPE
|
||||
] = {registrationName: 'onEventTargetTest'};
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
delete ReactNativeViewConfigRegistry.customDirectEventTypes[
|
||||
NATIVE_CUSTOM_EVENT_TYPE
|
||||
];
|
||||
});
|
||||
|
||||
it('should have 3 enumerable methods', () => {
|
||||
const methods = new Set([
|
||||
'addEventListener',
|
||||
@@ -261,18 +277,17 @@ describe('EventTarget', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('internal `dispatchTrustedEvent`', () => {
|
||||
describe('internal `dispatchNativeEvent`', () => {
|
||||
it('should set the `isTrusted` flag to `true`', () => {
|
||||
const eventTarget = new EventTarget();
|
||||
|
||||
const listener = createListener();
|
||||
|
||||
eventTarget.addEventListener('custom', listener);
|
||||
eventTarget.addEventListener(CUSTOM_EVENT_TYPE, listener);
|
||||
|
||||
const event = new Event('custom');
|
||||
|
||||
dispatchTrustedEvent(eventTarget, event);
|
||||
dispatchNativeEvent(eventTarget, NATIVE_CUSTOM_EVENT_TYPE, {});
|
||||
|
||||
const event = listener.mock.lastCall[0];
|
||||
expect(event.isTrusted).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -321,6 +336,7 @@ describe('EventTarget', () => {
|
||||
};
|
||||
|
||||
eventTarget.addEventListener('custom', listenerFunction);
|
||||
// $FlowFixMe[incompatible-type] This test intentionally uses an object listener.
|
||||
eventTarget.addEventListener('custom', listenerObject);
|
||||
|
||||
const event = new Event('custom');
|
||||
@@ -399,14 +415,11 @@ describe('EventTarget', () => {
|
||||
|
||||
const listener = createListener();
|
||||
|
||||
eventTarget.addEventListener('custom', listener);
|
||||
eventTarget.addEventListener(CUSTOM_EVENT_TYPE, listener);
|
||||
|
||||
const event = new Event('custom');
|
||||
|
||||
expect(event.isTrusted).toBe(false);
|
||||
|
||||
dispatchTrustedEvent(eventTarget, event);
|
||||
dispatchNativeEvent(eventTarget, NATIVE_CUSTOM_EVENT_TYPE, {});
|
||||
|
||||
const event = listener.mock.lastCall[0];
|
||||
expect(event.isTrusted).toBe(true);
|
||||
|
||||
eventTarget.dispatchEvent(event);
|
||||
|
||||
-1
@@ -8,7 +8,6 @@
|
||||
* @format
|
||||
*/
|
||||
|
||||
import EventTarget from '../EventTarget';
|
||||
import {EVENT_TARGET_GET_THE_PARENT_KEY} from '../internals/EventTargetInternals';
|
||||
|
||||
/**
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ import {
|
||||
unstable_NativeText as NativeText,
|
||||
} from 'react-native';
|
||||
import {NativeVirtualText} from 'react-native/Libraries/Text/TextNativeComponent';
|
||||
import * as ReactNativeFeatureFlags from 'react-native/src/private/featureflags/ReactNativeFeatureFlags';
|
||||
import {ReactNativeFeatureFlags} from 'react-native/react-private-interface';
|
||||
|
||||
// The public imperative EventTarget API is not part of the static type of this
|
||||
// final class (it is only present at runtime, gated by feature flags), so we
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ import nullthrows from 'nullthrows';
|
||||
import * as React from 'react';
|
||||
import {createRef} from 'react';
|
||||
import {unstable_NativeText as NativeText} from 'react-native';
|
||||
import * as ReactNativeFeatureFlags from 'react-native/src/private/featureflags/ReactNativeFeatureFlags';
|
||||
import {ReactNativeFeatureFlags} from 'react-native/react-private-interface';
|
||||
import ReadOnlyText from 'react-native/src/private/webapis/dom/nodes/ReadOnlyText';
|
||||
|
||||
function ensureReadOnlyText(value: unknown): ReadOnlyText {
|
||||
|
||||
+1
-3
@@ -4,6 +4,7 @@
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @fantom_flags enableIntersectionObserverByDefault:true
|
||||
* @flow strict-local
|
||||
* @format
|
||||
*/
|
||||
@@ -19,12 +20,9 @@ import nullthrows from 'nullthrows';
|
||||
import * as React from 'react';
|
||||
import {createRef} from 'react';
|
||||
import {ScrollView, View} from 'react-native';
|
||||
import setUpIntersectionObserver from 'react-native/src/private/setup/setUpIntersectionObserver';
|
||||
|
||||
declare const IntersectionObserver: Class<IntersectionObserverType>;
|
||||
|
||||
setUpIntersectionObserver();
|
||||
|
||||
const nodeRef = createRef<HostInstance>();
|
||||
let node: HostInstance;
|
||||
const rootRef = createRef<HostInstance>();
|
||||
|
||||
+1
-3
@@ -4,6 +4,7 @@
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @fantom_flags enableIntersectionObserverByDefault:true
|
||||
* @flow strict-local
|
||||
* @format
|
||||
*/
|
||||
@@ -20,13 +21,10 @@ import nullthrows from 'nullthrows';
|
||||
import * as React from 'react';
|
||||
import {createRef, useState} from 'react';
|
||||
import {ScrollView, View} from 'react-native';
|
||||
import setUpIntersectionObserver from 'react-native/src/private/setup/setUpIntersectionObserver';
|
||||
|
||||
declare const IntersectionObserver: Class<IntersectionObserverType>;
|
||||
declare const IntersectionObserverEntry: Class<IntersectionObserverEntryType>;
|
||||
|
||||
setUpIntersectionObserver();
|
||||
|
||||
export function expectRectEquals(
|
||||
rect: DOMRectReadOnly,
|
||||
expected: {x: number, y: number, width: number, height: number},
|
||||
|
||||
Vendored
+1
-2
@@ -4,6 +4,7 @@
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @fantom_flags enableIntersectionObserverByDefault:true
|
||||
* @flow strict-local
|
||||
* @format
|
||||
*/
|
||||
@@ -18,7 +19,6 @@ import nullthrows from 'nullthrows';
|
||||
import * as React from 'react';
|
||||
import {createRef} from 'react';
|
||||
import {View} from 'react-native';
|
||||
import setUpIntersectionObserver from 'react-native/src/private/setup/setUpIntersectionObserver';
|
||||
import setUpMutationObserver from 'react-native/src/private/setup/setUpMutationObserver';
|
||||
import DOMException from 'react-native/src/private/webapis/errors/DOMException';
|
||||
import IntersectionObserver from 'react-native/src/private/webapis/intersectionobserver/IntersectionObserver';
|
||||
@@ -26,7 +26,6 @@ import IntersectionObserverEntry from 'react-native/src/private/webapis/intersec
|
||||
import MutationObserver from 'react-native/src/private/webapis/mutationobserver/MutationObserver';
|
||||
import structuredClone from 'react-native/src/private/webapis/structuredClone/structuredClone';
|
||||
|
||||
setUpIntersectionObserver();
|
||||
setUpMutationObserver();
|
||||
|
||||
function expectDataCloneError(fn: () => unknown) {
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
* @format
|
||||
*/
|
||||
|
||||
import * as ReactNativeFeatureFlags from 'react-native/src/private/featureflags/ReactNativeFeatureFlags';
|
||||
import {ReactNativeFeatureFlags} from 'react-native/react-private-interface';
|
||||
|
||||
describe('FantomFeatureFlags', () => {
|
||||
it('allows overridding common feature flags', () => {
|
||||
|
||||
Reference in new issue
Block a user