1import React from 'react'; 2import TestRenderer from 'react-test-renderer'; 3 4import { requireNativeViewManager } from '../NativeViewManagerAdapter'; 5 6jest.mock('react-native', () => { 7 const ReactNative = jest.requireActual('react-native'); 8 // Mock a natively defined test view that the adapter will reference 9 ReactNative.NativeModules.NativeUnimoduleProxy.viewManagersNames = [ 10 ...ReactNative.NativeModules.NativeUnimoduleProxy.viewManagersNames, 11 'ExpoTestView', 12 ]; 13 ReactNative.UIManager.ExpoTestView = { 14 NativeProps: {}, 15 directEventTypes: {}, 16 }; 17 return ReactNative; 18}); 19 20describe('requireNativeViewManager', () => { 21 it(`sets the "displayName" of the native component`, () => { 22 // USING REACT INTERNALS HERE! `getComponentName()` isn't exported from 23 // React, so we can't test the resulting component name the way React will 24 // definitely calculate it. For now, let's use the fact that we know that 25 // TestView will be a ForwardRef which stores the underlying render function 26 // under the `render` property and that's how the component name is 27 // calculated. 28 // https://github.com/facebook/react/blob/769b1f270e1251d9dbdce0fcbd9e92e502d059b8/packages/shared/getComponentName.js#L81 29 const TestView: any = requireNativeViewManager('ExpoTestView'); 30 expect(TestView.render.displayName).toBe('Adapter<ExpoTestView>'); 31 }); 32 33 it(`partitions props into React Native and custom props`, () => { 34 const TestView = requireNativeViewManager('ExpoTestView'); 35 const testRenderer = TestRenderer.create( 36 <TestView testID="test" custom="hello"> 37 <TestView /> 38 </TestView> 39 ); 40 const testInstance = testRenderer.root; 41 // NOTE: update this test if the naming scheme of the native adapter components changes 42 const testNativeComponent = testInstance.findByType('ViewManagerAdapter_ExpoTestView' as any); 43 expect(testNativeComponent).toBeDefined(); 44 45 // React Native props 46 expect(testNativeComponent.props.testID).toBe('test'); 47 expect(React.Children.toArray(testNativeComponent.props.children)).toHaveLength(1); 48 49 // Custom props 50 expect(testNativeComponent.props.proxiedProperties).toEqual({ custom: 'hello' }); 51 expect(testNativeComponent.props).not.toHaveProperty('custom'); 52 }); 53}); 54