Skip to content

Commit 4b6bdb8

Browse files
rubennortefacebook-github-bot
authored andcommitted
Use public APIs in Fantom tests (#58567)
Summary: Replace eligible React Native deep imports in Fantom tests with package exports, Fantom APIs, DOM globals, and types derived from exported symbols. Keep implementation-specific imports where the public surface does not expose equivalent behavior or compatible Flow types. Changelog: [Internal] Differential Revision: D120517166
1 parent 0e87de0 commit 4b6bdb8

18 files changed

Lines changed: 404 additions & 510 deletions

File tree

packages/react-native/Libraries/Components/View/__tests__/View-nativeCSSParsing-itest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1313

14-
import type {ViewStyleProp} from 'react-native/Libraries/StyleSheet/StyleSheet';
15-
1614
import * as Fantom from '@react-native/fantom';
1715
import * as React from 'react';
1816
import {View} from 'react-native';
1917

18+
type ViewStyleProp = NonNullable<React.PropOf<View, 'style'>>;
19+
2020
// These tests render <View> with string-valued CSS properties. With
2121
// `enableNativeCSSParsing` forced on, the strings are parsed by the C++ CSS
2222
// parsers (color functions, transforms, filters, box shadows, gradients), and

packages/react-native/Libraries/Lists/__tests__/FlatList-itest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
12-
import type {FlatListProps} from 'react-native/Libraries/Lists/FlatList';
12+
import type {FlatListProps} from 'react-native';
1313

1414
import * as Fantom from '@react-native/fantom';
1515
import nullthrows from 'nullthrows';

packages/react-native/Libraries/Lists/__tests__/SectionList-itest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1212

13-
import type {SectionBase} from 'react-native/Libraries/Lists/SectionList';
13+
import type {SectionBase} from 'react-native';
1414

1515
import * as Fantom from '@react-native/fantom';
1616
import nullthrows from 'nullthrows';

packages/react-native/Libraries/ReactNative/__tests__/StaleEventHandlersFromInterruptedRender-itest.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import nullthrows from 'nullthrows';
1717
import * as React from 'react';
1818
import {createRef, startTransition, useDeferredValue, useState} from 'react';
1919
import {View} from 'react-native';
20-
import {NativeEventCategory} from 'react-native/src/private/testing/fantom/specs/NativeFantom';
2120

2221
describe('stale event handlers from interrupted render', () => {
2322
// This test demonstrates a bug where canonical.currentProps (which stores
@@ -80,7 +79,7 @@ describe('stale event handlers from interrupted render', () => {
8079
'onPointerUp',
8180
{x: 0, y: 0},
8281
{
83-
category: NativeEventCategory.Discrete,
82+
category: Fantom.NativeEventCategory.Discrete,
8483
},
8584
);
8685
}

packages/react-native/Libraries/Text/__tests__/Text-itest.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import accessibilityPropsSuite, {
2323
rolePropSuite,
2424
} from 'react-native/src/private/__tests__/utilities/accessibilityPropsSuite';
2525
import {testIDPropSuite} from 'react-native/src/private/__tests__/utilities/commonPropsSuite';
26-
import ReadOnlyElement from 'react-native/src/private/webapis/dom/nodes/ReadOnlyElement';
27-
import ReadOnlyText from 'react-native/src/private/webapis/dom/nodes/ReadOnlyText';
2826

2927
const TEST_TEXT = 'the text';
3028

@@ -661,7 +659,7 @@ describe('<Text>', () => {
661659
const element = nullthrows(elementRef.current);
662660
expect(element.childNodes.length).toBe(1);
663661

664-
const textChild = ensureInstance(element.childNodes[0], ReadOnlyText);
662+
const textChild = ensureInstance(element.childNodes[0], globalThis.Text);
665663
expect(textChild.textContent).toBe(TEST_TEXT);
666664
});
667665

@@ -681,19 +679,16 @@ describe('<Text>', () => {
681679
const element = nullthrows(elementRef.current);
682680
expect(element.childNodes.length).toBe(2);
683681

684-
const firstChild = ensureInstance(element.childNodes[0], ReadOnlyText);
682+
const firstChild = ensureInstance(element.childNodes[0], globalThis.Text);
685683
expect(firstChild.textContent).toBe('Some text ');
686684

687-
const secondChild = ensureInstance(
688-
element.childNodes[1],
689-
ReadOnlyElement,
690-
);
685+
const secondChild = ensureInstance(element.childNodes[1], Element);
691686
expect(secondChild.tagName).toBe('RN:Text');
692687
expect(secondChild.childNodes.length).toBe(1);
693688

694689
const secondChildText = ensureInstance(
695690
secondChild.childNodes[0],
696-
ReadOnlyText,
691+
globalThis.Text,
697692
);
698693
expect(secondChildText.textContent).toBe('also in bold');
699694
});

packages/react-native/Libraries/Utilities/__tests__/BackHandler-itest.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1212

13-
import type {HardwareBackPressEvent} from 'react-native/Libraries/Utilities/BackHandler';
14-
15-
import RCTDeviceEventEmitter from 'react-native/Libraries/EventEmitter/RCTDeviceEventEmitter';
16-
import BackHandler from 'react-native/Libraries/Utilities/BackHandler';
13+
import {BackHandler, DeviceEventEmitter} from 'react-native';
1714
import {HardwareBackPressEvent as HardwareBackPressEventClass} from 'react-native/Libraries/Utilities/HardwareBackPressEvent';
1815

16+
type BackPressHandler = Parameters<typeof BackHandler.addEventListener>[1];
17+
type HardwareBackPressEvent = Parameters<BackPressHandler>[0];
18+
1919
describe('BackHandler', () => {
2020
const subscriptions: Array<{remove: () => void, ...}> = [];
2121

@@ -44,7 +44,7 @@ describe('BackHandler', () => {
4444
BackHandler.addEventListener('hardwareBackPress', handler2),
4545
);
4646

47-
RCTDeviceEventEmitter.emit('hardwareBackPress', {timeStamp: 100});
47+
DeviceEventEmitter.emit('hardwareBackPress', {timeStamp: 100});
4848

4949
expect(callOrder).toEqual(['second']);
5050
});
@@ -67,7 +67,7 @@ describe('BackHandler', () => {
6767
BackHandler.addEventListener('hardwareBackPress', handler2),
6868
);
6969

70-
RCTDeviceEventEmitter.emit('hardwareBackPress', {timeStamp: 100});
70+
DeviceEventEmitter.emit('hardwareBackPress', {timeStamp: 100});
7171

7272
expect(callOrder).toEqual(['second', 'first']);
7373
});
@@ -83,7 +83,7 @@ describe('BackHandler', () => {
8383
BackHandler.addEventListener('hardwareBackPress', handler),
8484
);
8585

86-
RCTDeviceEventEmitter.emit('hardwareBackPress', {timeStamp: 42});
86+
DeviceEventEmitter.emit('hardwareBackPress', {timeStamp: 42});
8787

8888
expect(receivedEvent).toBeInstanceOf(HardwareBackPressEventClass);
8989
});
@@ -99,7 +99,7 @@ describe('BackHandler', () => {
9999
BackHandler.addEventListener('hardwareBackPress', handler),
100100
);
101101

102-
RCTDeviceEventEmitter.emit('hardwareBackPress', {timeStamp: 42});
102+
DeviceEventEmitter.emit('hardwareBackPress', {timeStamp: 42});
103103

104104
expect(receivedEvent?.timeStamp).toBe(42);
105105
});
@@ -116,7 +116,7 @@ describe('BackHandler', () => {
116116
);
117117

118118
const before = performance.now();
119-
RCTDeviceEventEmitter.emit('hardwareBackPress', null);
119+
DeviceEventEmitter.emit('hardwareBackPress', null);
120120
const after = performance.now();
121121

122122
const timeStamp = receivedEvent?.timeStamp;
@@ -137,7 +137,7 @@ describe('BackHandler', () => {
137137
const sub = BackHandler.addEventListener('hardwareBackPress', handler);
138138
sub.remove();
139139

140-
RCTDeviceEventEmitter.emit('hardwareBackPress', {timeStamp: 100});
140+
DeviceEventEmitter.emit('hardwareBackPress', {timeStamp: 100});
141141

142142
expect(called).toBe(false);
143143
});

packages/react-native/Libraries/Utilities/__tests__/HardwareBackPressEvent-itest.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1212

1313
import {HardwareBackPressEvent} from 'react-native/Libraries/Utilities/HardwareBackPressEvent';
14-
import Event from 'react-native/src/private/webapis/dom/events/Event';
1514
import {setEventInitTimeStamp} from 'react-native/src/private/webapis/dom/events/internals/EventInternals';
1615

1716
describe('HardwareBackPressEvent', () => {

packages/react-native/src/private/renderer/core/__tests__/EventTargetDispatching-itest.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212

1313
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1414

15-
import type {
16-
NativePointerEvent,
17-
PointerEvent,
18-
} from 'react-native/Libraries/Types/CoreEventTypes';
19-
import type {ReadOnlyNodeWithEventTarget} from 'react-native/src/private/webapis/dom/nodes/ReadOnlyNode';
15+
import type {NativePointerEvent, PointerEvent} from 'react-native';
2016

2117
import * as Fantom from '@react-native/fantom';
2218
import * as React from 'react';
@@ -25,7 +21,7 @@ import * as ReactNativeFeatureFlags from 'react-native/src/private/featureflags/
2521
import Event from 'react-native/src/private/webapis/dom/events/Event';
2622

2723
// Temporary cast until ReadOnlyNode extends EventTarget ungated.
28-
function asEventTarget(node: ?interface {}): ReadOnlyNodeWithEventTarget {
24+
function asEventTarget(node: ?interface {}): EventTarget {
2925
if (node == null) {
3026
throw new Error('Expected non-null node');
3127
}

packages/react-native/src/private/webapis/dom/events/__tests__/CustomEvent-itest.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1212

13-
import CustomEvent from 'react-native/src/private/webapis/dom/events/CustomEvent';
14-
import Event from 'react-native/src/private/webapis/dom/events/Event';
15-
1613
describe('CustomEvent', () => {
1714
it('extends Event', () => {
1815
const event = new CustomEvent('foo', {

packages/react-native/src/private/webapis/dom/nodes/__tests__/ReactNativeDocument-itest.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {createRef} from 'react';
2121
import {View} from 'react-native';
2222
import ReactNativeDocument from 'react-native/src/private/webapis/dom/nodes/ReactNativeDocument';
2323
import ReactNativeElement from 'react-native/src/private/webapis/dom/nodes/ReactNativeElement';
24-
import ReadOnlyNode from 'react-native/src/private/webapis/dom/nodes/ReadOnlyNode';
2524

2625
describe('ReactNativeDocument', () => {
2726
it('is connected until the surface is destroyed', () => {
@@ -99,7 +98,7 @@ describe('ReactNativeDocument', () => {
9998
const document = ensureInstance(element.ownerDocument, ReactNativeDocument);
10099

101100
expect(document.nodeName).toBe('#document');
102-
expect(document.nodeType).toBe(ReadOnlyNode.DOCUMENT_NODE);
101+
expect(document.nodeType).toBe(Node.DOCUMENT_NODE);
103102
expect(document.nodeValue).toBe(null);
104103
expect(document.textContent).toBe(null);
105104
});
@@ -155,28 +154,22 @@ describe('ReactNativeDocument', () => {
155154
).toBe(0);
156155

157156
expect(document.compareDocumentPosition(documentElement)).toBe(
158-
ReadOnlyNode.DOCUMENT_POSITION_CONTAINED_BY |
159-
ReadOnlyNode.DOCUMENT_POSITION_FOLLOWING,
157+
Node.DOCUMENT_POSITION_CONTAINED_BY | Node.DOCUMENT_POSITION_FOLLOWING,
160158
);
161159
expect(document.compareDocumentPosition(element)).toBe(
162-
ReadOnlyNode.DOCUMENT_POSITION_CONTAINED_BY |
163-
ReadOnlyNode.DOCUMENT_POSITION_FOLLOWING,
160+
Node.DOCUMENT_POSITION_CONTAINED_BY | Node.DOCUMENT_POSITION_FOLLOWING,
164161
);
165162
expect(documentElement.compareDocumentPosition(document)).toBe(
166-
ReadOnlyNode.DOCUMENT_POSITION_CONTAINS |
167-
ReadOnlyNode.DOCUMENT_POSITION_PRECEDING,
163+
Node.DOCUMENT_POSITION_CONTAINS | Node.DOCUMENT_POSITION_PRECEDING,
168164
);
169165
expect(documentElement.compareDocumentPosition(element)).toBe(
170-
ReadOnlyNode.DOCUMENT_POSITION_CONTAINED_BY |
171-
ReadOnlyNode.DOCUMENT_POSITION_FOLLOWING,
166+
Node.DOCUMENT_POSITION_CONTAINED_BY | Node.DOCUMENT_POSITION_FOLLOWING,
172167
);
173168
expect(element.compareDocumentPosition(document)).toBe(
174-
ReadOnlyNode.DOCUMENT_POSITION_CONTAINS |
175-
ReadOnlyNode.DOCUMENT_POSITION_PRECEDING,
169+
Node.DOCUMENT_POSITION_CONTAINS | Node.DOCUMENT_POSITION_PRECEDING,
176170
);
177171
expect(element.compareDocumentPosition(documentElement)).toBe(
178-
ReadOnlyNode.DOCUMENT_POSITION_CONTAINS |
179-
ReadOnlyNode.DOCUMENT_POSITION_PRECEDING,
172+
Node.DOCUMENT_POSITION_CONTAINS | Node.DOCUMENT_POSITION_PRECEDING,
180173
);
181174
});
182175

0 commit comments

Comments
 (0)