diff --git a/packages/react-native/Libraries/NativeComponent/NativeComponentRegistry.js b/packages/react-native/Libraries/NativeComponent/NativeComponentRegistry.js index deedfb009041..e4ebdb8cf1b4 100644 --- a/packages/react-native/Libraries/NativeComponent/NativeComponentRegistry.js +++ b/packages/react-native/Libraries/NativeComponent/NativeComponentRegistry.js @@ -17,6 +17,7 @@ import type { import getNativeComponentAttributes from '../ReactNative/getNativeComponentAttributes'; import UIManager from '../ReactNative/UIManager'; import * as ReactNativeViewConfigRegistry from '../Renderer/shims/ReactNativeViewConfigRegistry'; +import warnOnce from '../Utilities/warnOnce'; import * as StaticViewConfigValidator from './StaticViewConfigValidator'; import {createViewConfig} from './ViewConfig'; import invariant from 'invariant'; @@ -76,6 +77,20 @@ export function get( ); if (verify) { + // Skip verification in bridgeless mode when the legacy view config interop binding is absent + if ( + global.RN$Bridgeless === true && + global.RN$LegacyInterop_UIManager_getConstants == null + ) { + if (__DEV__) { + warnOnce( + 'static-view-config-verification-without-legacy-interop', + 'Skipping static view config verification because the legacy view config interop binding is unavailable in bridgeless mode. Disable `verify` in `NativeComponentRegistry.setRuntimeConfigProvider` or enable `useNativeViewConfigsInBridgelessMode`.', + ); + } + return viewConfig; + } + const nativeViewConfig = native ? viewConfig : getNativeComponentAttributes(name); @@ -149,7 +164,7 @@ export function getWithFallback_DEPRECATED( function hasNativeViewConfig(name: string): boolean { invariant(getRuntimeConfig == null, 'Unexpected invocation!'); - return UIManager.getViewManagerConfig(name) != null; + return UIManager.hasViewManagerConfig(name); } /** diff --git a/packages/react-native/Libraries/NativeComponent/__tests__/NativeComponentRegistry-test.js b/packages/react-native/Libraries/NativeComponent/__tests__/NativeComponentRegistry-test.js new file mode 100644 index 000000000000..6d4908f1a9bb --- /dev/null +++ b/packages/react-native/Libraries/NativeComponent/__tests__/NativeComponentRegistry-test.js @@ -0,0 +1,164 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + */ + +import type {PartialViewConfig} from '../../Renderer/shims/ReactNativeTypes'; + +// The Jest preset replaces this module with a mock. +jest.unmock('../NativeComponentRegistry'); + +const NAME = 'RCTTestComponent'; +const SECOND_NAME = 'RCTSecondTestComponent'; + +const PARTIAL_VIEW_CONFIG: PartialViewConfig = { + uiViewClassName: NAME, + validAttributes: {}, +}; + +const SECOND_PARTIAL_VIEW_CONFIG: PartialViewConfig = { + uiViewClassName: SECOND_NAME, + validAttributes: {}, +}; + +const originalBridgeless = global.RN$Bridgeless; +const originalGetConstants = global.RN$LegacyInterop_UIManager_getConstants; +const originalDev = global.__DEV__; + +beforeEach(() => { + jest.resetModules(); +}); + +afterEach(() => { + jest.restoreAllMocks(); + // $FlowExpectedError[cannot-write] + global.RN$Bridgeless = originalBridgeless; + // $FlowExpectedError[cannot-write] + global.RN$LegacyInterop_UIManager_getConstants = originalGetConstants; + // $FlowExpectedError[cannot-write] + global.__DEV__ = originalDev; +}); + +describe('get with verify', () => { + function getNativeViewConfigRequests({ + bridgeless, + dev = true, + interop, + names = [NAME], + }: { + bridgeless: boolean, + dev?: boolean, + interop: boolean, + names?: ReadonlyArray, + }): ReadonlyArray { + // $FlowExpectedError[cannot-write] + global.__DEV__ = dev; + // $FlowExpectedError[cannot-write] + global.RN$Bridgeless = bridgeless; + // $FlowExpectedError[cannot-write] + global.RN$LegacyInterop_UIManager_getConstants = interop + ? () => ({}) + : undefined; + const getNativeComponentAttributes = jest.fn((name: string) => null); + jest.doMock( + '../../ReactNative/getNativeComponentAttributes', + () => getNativeComponentAttributes, + ); + + const NativeComponentRegistry = require('../NativeComponentRegistry'); + NativeComponentRegistry.setRuntimeConfigProvider(() => ({ + native: false, + verify: true, + })); + const ViewConfigRegistry = require('../../Renderer/shims/ReactNativeViewConfigRegistry'); + for (const componentName of names) { + const config = + componentName === SECOND_NAME + ? SECOND_PARTIAL_VIEW_CONFIG + : PARTIAL_VIEW_CONFIG; + NativeComponentRegistry.get<{...}>(componentName, () => config); + // Reading the registered lazy view config runs verification + ViewConfigRegistry.get(componentName); + } + + return getNativeComponentAttributes.mock.calls.map(([name]) => name); + } + + test('skips verification and warns once in development without the interop binding in bridgeless mode', () => { + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); + + expect( + getNativeViewConfigRequests({ + bridgeless: true, + interop: false, + names: [NAME, SECOND_NAME], + }), + ).toEqual([]); + expect(warnSpy).toHaveBeenCalledTimes(1); + expect(warnSpy).toHaveBeenCalledWith( + expect.stringContaining('Skipping static view config verification'), + ); + }); + + test('does not warn outside development without the interop binding in bridgeless mode', () => { + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); + + expect( + getNativeViewConfigRequests({ + bridgeless: true, + dev: false, + interop: false, + }), + ).toEqual([]); + expect(warnSpy).not.toHaveBeenCalled(); + }); + + test('requests the native view config in bridgeless mode with the interop binding', () => { + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); + + expect( + getNativeViewConfigRequests({ + bridgeless: true, + interop: true, + }), + ).toEqual([NAME]); + expect(warnSpy).not.toHaveBeenCalled(); + }); + + test('requests the native view config outside bridgeless mode', () => { + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); + + expect( + getNativeViewConfigRequests({ + bridgeless: false, + interop: false, + }), + ).toEqual([NAME]); + expect(warnSpy).not.toHaveBeenCalled(); + }); +}); + +describe('getWithFallback_DEPRECATED without a runtime config provider', () => { + test('finds a component without requesting its view config', () => { + const getViewManagerConfig = jest.fn((name: string) => null); + jest.doMock('../../ReactNative/UIManager', () => ({ + getViewManagerConfig, + hasViewManagerConfig: (name: string) => name === NAME, + })); + + const NativeComponentRegistry = require('../NativeComponentRegistry'); + + expect( + NativeComponentRegistry.getWithFallback_DEPRECATED<{...}>( + NAME, + () => PARTIAL_VIEW_CONFIG, + ), + ).toBe(NAME); + expect(getViewManagerConfig).not.toHaveBeenCalled(); + }); +});