Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -76,6 +77,20 @@ export function get<Config extends {...}>(
);

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a warnOnce in development? verify was enabled but legacy interop is disabled.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, added a dev-only warnOnce in f416ed4 when verify is enabled without the legacy view config interop binding, along with unit tests covering dev and non-dev behavior.

}

const nativeViewConfig = native
? viewConfig
: getNativeComponentAttributes(name);
Expand Down Expand Up @@ -149,7 +164,7 @@ export function getWithFallback_DEPRECATED<Config extends {...}>(

function hasNativeViewConfig(name: string): boolean {
invariant(getRuntimeConfig == null, 'Unexpected invocation!');
return UIManager.getViewManagerConfig(name) != null;
return UIManager.hasViewManagerConfig(name);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string>,
}): ReadonlyArray<string> {
// $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();
});
});
Loading