Skip to content

Commit 664f7c0

Browse files
rubennortefacebook-github-bot
authored andcommitted
Add test to verify that components like View and Text are not loaded as a side-effect of environment initialization (#53053)
Summary: Pull Request resolved: #53053 Changelog: [internal] This adds a Fantom test to ensure that setting up the RN environment doesn't trigger the initialization for React components like View and Text, which should be lazy loaded when necessary. Reviewed By: rshest Differential Revision: D79636160 fbshipit-source-id: ef1fbd6cd531eb7082dce000ba74a5eed451e259
1 parent 14c869c commit 664f7c0

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
import setUpDefaultReactNativeEnvironment from 'react-native/src/private/setup/setUpDefaultReactNativeEnvironment';
12+
13+
describe('setUpReactNativeEnvironment', () => {
14+
it('should not load components as a side effect', () => {
15+
// We set up has not been done yet.
16+
expect(globalThis.self).toBeUndefined();
17+
18+
setUpDefaultReactNativeEnvironment(false);
19+
20+
// The set up worked.
21+
expect(globalThis.self).toBe(globalThis);
22+
23+
// Verify that `View` and `Text` are not loaded as a side-effect
24+
25+
// $FlowExpectedError[prop-missing]
26+
const viewModuleId = require.resolveWeak(
27+
'react-native/Libraries/Components/View/View',
28+
);
29+
// $FlowExpectedError[prop-missing]
30+
const textModuleId = require.resolveWeak(
31+
'react-native/Libraries/Text/Text',
32+
);
33+
// $FlowExpectedError[prop-missing]
34+
const metroModules = require.getModules();
35+
const viewModule = metroModules.get(viewModuleId);
36+
const textModule = metroModules.get(textModuleId);
37+
expect(viewModule).toBeInstanceOf(Object);
38+
expect(textModule).toBeInstanceOf(Object);
39+
40+
expect(viewModule.isInitialized).toBe(false);
41+
expect(textModule.isInitialized).toBe(false);
42+
43+
// But then we can detect when they're loaded (to verify the previous
44+
// detection works).
45+
46+
require('react-native').View;
47+
48+
expect(viewModule.isInitialized).toBe(true);
49+
expect(textModule.isInitialized).toBe(false);
50+
51+
require('react-native').Text;
52+
53+
expect(viewModule.isInitialized).toBe(true);
54+
expect(textModule.isInitialized).toBe(true);
55+
});
56+
});

0 commit comments

Comments
 (0)