Files
react-native/Libraries/Utilities/__tests__/codegenNativeComponent-test.js
T
Tim Yung 494c47360f RN: Sort Imports via ESLint
Summary:
Applies the autofix from the newly introduced `lint/sort-imports` ESLint rule.

Changelog:
[Internal]

Reviewed By: cortinico, skinsshark

Differential Revision: D39907798

fbshipit-source-id: 17f5f11b08a5b4bb66286816b78eb26e07e829b8
2022-09-30 14:28:48 -07:00

68 lines
2.2 KiB
JavaScript

/**
* 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.
*
* @format
* @oncall react_native
*/
'use strict';
const UIManager = require('../../ReactNative/UIManager');
const codegenNativeComponent = require('../codegenNativeComponent').default;
// We need to unmock requireNativeComponent since it's under test.
// Instead, we mock the function it calls, createReactNativeComponentClass,
// so that we don't run into issues populating the registry with the same
// component names.
jest.unmock('../../ReactNative/requireNativeComponent');
jest.mock(
'../../Renderer/shims/createReactNativeComponentClass',
() => componentName => componentName,
);
jest
.spyOn(UIManager, 'hasViewManagerConfig')
.mockImplementation(componentName =>
componentName.includes('ComponentNameDoesNotExist') ? false : true,
);
describe('codegenNativeComponent', () => {
it('should require component as is ', () => {
const component = codegenNativeComponent('ComponentName');
expect(component).toBe('ComponentName');
});
it('should require paperComponentName', () => {
const component = codegenNativeComponent('ComponentName', {
paperComponentName: 'PaperComponentName',
});
expect(component).toBe('PaperComponentName');
});
it('should fall back to requiring the deprecated paper component name', () => {
const component = codegenNativeComponent('ComponentNameDoesNotExist', {
paperComponentNameDeprecated: 'ComponentName',
});
expect(component).toBe('ComponentName');
});
it('should require the new component name', () => {
const component = codegenNativeComponent('ComponentName', {
paperComponentNameDeprecated: 'ComponentNameDoesNotExist',
});
expect(component).toBe('ComponentName');
});
it('should throw if neither component names exist', () => {
expect(() =>
codegenNativeComponent('ComponentNameDoesNotExistOne', {
paperComponentNameDeprecated: 'ComponentNameDoesNotExistTwo',
}),
).toThrow(
'Failed to find native component for either ComponentNameDoesNotExistOne or ComponentNameDoesNotExistTwo',
);
});
});