Skip to content

Commit ccc086d

Browse files
rubennortefacebook-github-bot
authored andcommitted
Complete remaining deep import migration
Summary: Migrate remaining React Native consumer deep imports to supported root and secondary entrypoints. Keep Jest preset package-internal type imports on OSS-exported implementation paths while preserving runtime mock behavior. Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D121588775
1 parent 2915fe0 commit ccc086d

9 files changed

Lines changed: 61 additions & 18 deletions

File tree

‎packages/jest-preset/jest/RefreshControlMock.js‎

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

1111
'use strict';
1212

13-
import type {HostComponent} from 'react-native/src/private/types/HostComponent';
13+
import type {HostComponent} from 'react-native';
14+
import typeof * as TReactNative from 'react-native';
1415

1516
import * as React from 'react';
16-
import requireNativeComponent from 'react-native/Libraries/ReactNative/requireNativeComponent';
17+
18+
const {requireNativeComponent} =
19+
jest.requireActual<TReactNative>('react-native');
1720

1821
const RCTRefreshControl: HostComponent<{}> = requireNativeComponent<{}>(
1922
'RCTRefreshControl',

‎packages/jest-preset/jest/__tests__/setup-test.js‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
* @format
99
*/
1010

11-
import NativeExceptionsManager from 'react-native/Libraries/Core/NativeExceptionsManager';
11+
const NativeExceptionsManager = jest.requireMock(
12+
'react-native/Libraries/Core/NativeExceptionsManager',
13+
).default;
1214

1315
test('NativeAnimatedHelper is a mock', () => {
1416
const consoleWarn = jest.spyOn(console, 'warn').mockImplementation(() => {});
@@ -40,3 +42,15 @@ test('NativeExceptionsManager is a mock', () => {
4042
true,
4143
);
4244
});
45+
46+
test('native component mocks work with a partial react-native mock', () => {
47+
jest.isolateModules(() => {
48+
jest.doMock('react-native', () => ({}));
49+
50+
expect(() => {
51+
jest.requireActual<unknown>('../RefreshControlMock');
52+
jest.requireActual<unknown>('../mocks/RefreshControl');
53+
jest.requireActual<unknown>('../mocks/ScrollView');
54+
}).not.toThrow();
55+
});
56+
});

‎packages/jest-preset/jest/mockNativeComponent.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow strict
7+
* @flow strict-local
88
* @format
99
*/
1010

11-
import type {HostInstance} from 'react-native/src/private/types/HostInstance';
11+
import type {HostInstance} from 'react-native';
1212

1313
import * as React from 'react';
1414
import {createElement} from 'react';

‎packages/jest-preset/jest/mocks/NativeComponentRegistry.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow strict
7+
* @flow strict-local
88
* @format
99
*/
1010

‎packages/jest-preset/jest/mocks/RefreshControl.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
* @format
99
*/
1010

11-
import type {RefreshControlProps} from 'react-native';
12-
import type {HostComponent} from 'react-native/src/private/types/HostComponent';
11+
import type {HostComponent, RefreshControlProps} from 'react-native';
12+
import typeof * as TReactNative from 'react-native';
1313

1414
import * as React from 'react';
15-
import requireNativeComponent from 'react-native/Libraries/ReactNative/requireNativeComponent';
15+
16+
const {requireNativeComponent} =
17+
jest.requireActual<TReactNative>('react-native');
1618

1719
const RCTRefreshControl: HostComponent<{}> = requireNativeComponent<{}>(
1820
'RCTRefreshControl',

‎packages/jest-preset/jest/mocks/ScrollView.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@
1010

1111
import typeof * as TmockComponent from '../mockComponent';
1212
import typeof * as TMockNativeMethods from '../MockNativeMethods';
13+
import type {ScrollViewProps} from 'react-native';
14+
import typeof * as TReactNative from 'react-native';
1315
import typeof {ScrollView as TScrollView} from 'react-native';
14-
import type {ScrollViewNativeProps} from 'react-native/Libraries/Components/ScrollView/ScrollViewNativeComponentType';
1516

1617
import * as React from 'react';
17-
import View from 'react-native/Libraries/Components/View/View';
18-
import requireNativeComponent from 'react-native/Libraries/ReactNative/requireNativeComponent';
18+
19+
const {View, requireNativeComponent} =
20+
jest.requireActual<TReactNative>('react-native');
1921

2022
const mockComponent =
2123
jest.requireActual<TmockComponent>('../mockComponent').default;
2224
const MockNativeMethods = jest.requireActual<TMockNativeMethods>(
2325
'../MockNativeMethods',
2426
).default;
2527

26-
const RCTScrollView =
27-
requireNativeComponent<ScrollViewNativeProps>('RCTScrollView');
28+
const RCTScrollView = requireNativeComponent<ScrollViewProps>('RCTScrollView');
2829

2930
const BaseComponent = mockComponent(
3031
'react-native/Libraries/Components/ScrollView/ScrollView',

‎packages/jest-preset/jest/mocks/requireNativeComponent.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow strict
7+
* @flow strict-local
88
* @format
99
*/
1010

1111
import typeof * as TmockNativeComponent from '../mockNativeComponent';
12-
import type {HostComponent} from 'react-native/src/private/types/HostComponent';
12+
import type {HostComponent} from 'react-native';
1313

1414
const mockNativeComponent = jest.requireActual<TmockNativeComponent>(
1515
'../mockNativeComponent',

‎packages/jest-preset/jest/mocks/useColorScheme.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow strict
7+
* @flow strict-local
88
* @format
99
*/
1010

11-
import type {ColorSchemeName} from 'react-native/Libraries/Utilities/NativeAppearance';
11+
import type {ColorSchemeName} from 'react-native';
1212

1313
const useColorScheme = jest.fn(() => 'light') as JestMockFn<
1414
[],

‎packages/jest-preset/jest/setup.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,96 +87,119 @@ try {
8787
} catch {}
8888

8989
mock(
90+
// $FlowFixMe[cannot-resolve-module]
9091
'm#react-native/Libraries/AppState/AppState',
9192
// $FlowFixMe[incompatible-type] - `./mocks/AppState` is incomplete.
9293
'm#./mocks/AppState',
9394
);
9495
mock(
96+
// $FlowFixMe[cannot-resolve-module]
9597
'm#react-native/Libraries/BatchedBridge/NativeModules',
9698
'm#./mocks/NativeModules',
9799
);
98100
mock(
101+
// $FlowFixMe[cannot-resolve-module]
99102
'm#react-native/Libraries/Components/AccessibilityInfo/AccessibilityInfo',
100103
'm#./mocks/AccessibilityInfo',
101104
);
102105
mock(
106+
// $FlowFixMe[cannot-resolve-module]
103107
'm#react-native/Libraries/Components/ActivityIndicator/ActivityIndicator',
104108
'm#./mocks/ActivityIndicator',
105109
);
106110
mock(
111+
// $FlowFixMe[cannot-resolve-module]
107112
'm#react-native/Libraries/Components/Clipboard/Clipboard',
108113
'm#./mocks/Clipboard',
109114
);
110115
mock(
116+
// $FlowFixMe[cannot-resolve-module]
111117
'm#react-native/Libraries/Components/RefreshControl/RefreshControl',
112118
// $FlowFixMe[incompatible-type] - `react-native/Libraries/Components/RefreshControl/RefreshControl` should export a component type.
113119
'm#./mocks/RefreshControl',
114120
);
115121
mock(
122+
// $FlowFixMe[cannot-resolve-module]
116123
'm#react-native/Libraries/Components/ScrollView/ScrollView',
117124
// $FlowFixMe[incompatible-exact] - `react-native/Libraries/Components/ScrollView/ScrollView` is... I don't even.
118125
// $FlowFixMe[incompatible-type]
119126
'm#./mocks/ScrollView',
120127
);
121128
mock(
129+
// $FlowFixMe[cannot-resolve-module]
122130
'm#react-native/Libraries/Components/TextInput/TextInput',
123131
'm#./mocks/TextInput',
124132
);
133+
// $FlowFixMe[cannot-resolve-module]
125134
mock('m#react-native/Libraries/Components/View/View', 'm#./mocks/View');
126135
mock(
136+
// $FlowFixMe[cannot-resolve-module]
127137
'm#react-native/Libraries/Components/View/ViewNativeComponent',
128138
// $FlowFixMe[incompatible-type] - `./mocks/ViewNativeComponent` is incomplete.
129139
// $FlowFixMe[prop-missing]
130140
'm#./mocks/ViewNativeComponent',
131141
);
132142
mock(
143+
// $FlowFixMe[cannot-resolve-module]
133144
'm#react-native/Libraries/Core/InitializeCore',
134145
'm#./mocks/InitializeCore',
135146
);
136147
mock('m#react-native/setup-env', 'm#./mocks/InitializeCore');
148+
// $FlowFixMe[cannot-resolve-module]
137149
mock('m#react-native/Libraries/Core/NativeExceptionsManager');
150+
// $FlowFixMe[cannot-resolve-module]
138151
mock('m#react-native/Libraries/Image/Image', 'm#./mocks/Image');
139152
mock(
153+
// $FlowFixMe[cannot-resolve-module]
140154
'm#react-native/Libraries/Linking/Linking',
141155
// $FlowFixMe[incompatible-type] - `./mocks/Linking` is incomplete.
142156
'm#./mocks/Linking',
143157
);
144158
mock(
159+
// $FlowFixMe[cannot-resolve-module]
145160
'm#react-native/Libraries/Modal/Modal',
146161
// $FlowFixMe[incompatible-type] - `react-native/Libraries/Modal/Modal` should export a component type.
147162
'm#./mocks/Modal',
148163
);
149164
mock(
165+
// $FlowFixMe[cannot-resolve-module]
150166
'm#react-native/Libraries/NativeComponent/NativeComponentRegistry',
151167
// $FlowFixMe[incompatible-type] - `./mocks/NativeComponentRegistry` should export named functions.
152168
'm#./mocks/NativeComponentRegistry',
153169
);
154170
mock(
171+
// $FlowFixMe[cannot-resolve-module]
155172
'm#react-native/Libraries/ReactNative/RendererProxy',
156173
// $FlowFixMe[incompatible-type] - `./mocks/RendererProxy` is incomplete.
157174
'm#./mocks/RendererProxy',
158175
);
159176
mock(
177+
// $FlowFixMe[cannot-resolve-module]
160178
'm#react-native/Libraries/ReactNative/requireNativeComponent',
161179
'm#./mocks/requireNativeComponent',
162180
);
163181
mock(
182+
// $FlowFixMe[cannot-resolve-module]
164183
'm#react-native/Libraries/ReactNative/UIManager',
165184
// $FlowFixMe[incompatible-type] - `./mocks/UIManager` is incomplete.
166185
'm#./mocks/UIManager',
167186
);
187+
// $FlowFixMe[cannot-resolve-module]
168188
mock('m#react-native/Libraries/Text/Text', 'm#./mocks/Text');
169189
mock(
190+
// $FlowFixMe[cannot-resolve-module]
170191
'm#react-native/Libraries/Utilities/useColorScheme',
171192
// $FlowFixMe[react-rule-hook-incompatible]
172193
'm#./mocks/useColorScheme',
173194
);
174195
mock(
196+
// $FlowFixMe[cannot-resolve-module]
175197
'm#react-native/Libraries/Vibration/Vibration',
176198
// $FlowFixMe[incompatible-type]
177199
'm#./mocks/Vibration',
178200
);
179201
mock(
202+
// $FlowFixMe[cannot-resolve-module]
180203
'm#react-native/src/private/animated/NativeAnimatedHelper',
181204
'm#./mocks/NativeAnimatedHelper',
182205
);

0 commit comments

Comments
 (0)