Skip to content
Draft
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 @@ -76,6 +76,15 @@ export function get<Config extends {...}>(
);

if (verify) {
// In bridgeless mode, native view configs come from the legacy view
// config interop binding. Without it, asking for one logs an error.
if (
global.RN$Bridgeless === true &&
global.RN$LegacyInterop_UIManager_getConstants == null
) {
return viewConfig;
}

const nativeViewConfig = native
? viewConfig
: getNativeComponentAttributes(name);
Expand Down Expand Up @@ -149,7 +158,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,115 @@
/**
* 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 PARTIAL_VIEW_CONFIG: PartialViewConfig = {
uiViewClassName: NAME,
validAttributes: {},
};

const originalBridgeless = global.RN$Bridgeless;
const originalGetConstants = global.RN$LegacyInterop_UIManager_getConstants;

beforeEach(() => {
jest.resetModules();
});

afterEach(() => {
// $FlowExpectedError[cannot-write]
global.RN$Bridgeless = originalBridgeless;
// $FlowExpectedError[cannot-write]
global.RN$LegacyInterop_UIManager_getConstants = originalGetConstants;
});

describe('get with verify', () => {
function getNativeViewConfigRequests({
bridgeless,
interop,
}: {
bridgeless: boolean,
interop: boolean,
}): ReadonlyArray<string> {
// $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,
}));
NativeComponentRegistry.get<{...}>(NAME, () => PARTIAL_VIEW_CONFIG);
// `get` registers a lazy view config. Reading it runs the verification.
require('../../Renderer/shims/ReactNativeViewConfigRegistry').get(NAME);

return getNativeComponentAttributes.mock.calls.map(([name]) => name);
}

test('does not request a native view config in bridgeless mode without the interop binding', () => {
expect(
getNativeViewConfigRequests({
bridgeless: true,
interop: false,
}),
).toEqual([]);
});

test('requests the native view config in bridgeless mode with the interop binding', () => {
expect(
getNativeViewConfigRequests({
bridgeless: true,
interop: true,
}),
).toEqual([NAME]);
});

test('requests the native view config outside bridgeless mode', () => {
expect(
getNativeViewConfigRequests({
bridgeless: false,
interop: false,
}),
).toEqual([NAME]);
});
});

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