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