From 2e8ecab583a3c66491cbc31202a0f1b6e23c912e Mon Sep 17 00:00:00 2001 From: Rick Hanlon Date: Wed, 7 Aug 2019 05:52:52 -0700 Subject: [PATCH] Add tests for codegenNativeComponent Summary: Adds tests for codegenNativeComponent Reviewed By: TheSavior Differential Revision: D16582627 fbshipit-source-id: 3527126c7838f3e2c0c56b19956c618f0a7fb9f9 --- .../__tests__/codegenNativeComponent-test.js | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Libraries/Utilities/__tests__/codegenNativeComponent-test.js diff --git a/Libraries/Utilities/__tests__/codegenNativeComponent-test.js b/Libraries/Utilities/__tests__/codegenNativeComponent-test.js new file mode 100644 index 00000000000..f9acf7fe816 --- /dev/null +++ b/Libraries/Utilities/__tests__/codegenNativeComponent-test.js @@ -0,0 +1,67 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + * @emails oncall+react_native + */ + +'use strict'; + +const codegenNativeComponent = require('../codegenNativeComponent').default; +const {UIManager} = require('react-native'); + +// 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, 'getViewManagerConfig') + .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', + ); + }); +});