1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @emails react-core
    
  8.  * @jest-environment node
    
  9.  */
    
  10. 
    
  11. 'use strict';
    
  12. 
    
  13. let React;
    
  14. let ReactNative;
    
  15. let createReactNativeComponentClass;
    
  16. let computeComponentStackForErrorReporting;
    
  17. 
    
  18. function normalizeCodeLocInfo(str) {
    
  19.   return (
    
  20.     str &&
    
  21.     str.replace(/\n +(?:at|in) ([\S]+)[^\n]*/g, function (m, name) {
    
  22.       return '\n    in ' + name + ' (at **)';
    
  23.     })
    
  24.   );
    
  25. }
    
  26. 
    
  27. describe('ReactNativeError', () => {
    
  28.   beforeEach(() => {
    
  29.     jest.resetModules();
    
  30. 
    
  31.     React = require('react');
    
  32.     ReactNative = require('react-native-renderer');
    
  33.     createReactNativeComponentClass =
    
  34.       require('react-native/Libraries/ReactPrivate/ReactNativePrivateInterface')
    
  35.         .ReactNativeViewConfigRegistry.register;
    
  36.     computeComponentStackForErrorReporting =
    
  37.       ReactNative.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED
    
  38.         .computeComponentStackForErrorReporting;
    
  39.   });
    
  40. 
    
  41.   it('should throw error if null component registration getter is used', () => {
    
  42.     expect(() => {
    
  43.       try {
    
  44.         createReactNativeComponentClass('View', null);
    
  45.       } catch (e) {
    
  46.         throw new Error(e.toString());
    
  47.       }
    
  48.     }).toThrow(
    
  49.       'View config getter callback for component `View` must be a function (received `null`)',
    
  50.     );
    
  51.   });
    
  52. 
    
  53.   it('should be able to extract a component stack from a native view', () => {
    
  54.     const View = createReactNativeComponentClass('View', () => ({
    
  55.       validAttributes: {foo: true},
    
  56.       uiViewClassName: 'View',
    
  57.     }));
    
  58. 
    
  59.     const ref = React.createRef();
    
  60. 
    
  61.     function FunctionComponent(props) {
    
  62.       return props.children;
    
  63.     }
    
  64. 
    
  65.     class ClassComponent extends React.Component {
    
  66.       render() {
    
  67.         return (
    
  68.           <FunctionComponent>
    
  69.             <View foo="test" ref={ref} />
    
  70.           </FunctionComponent>
    
  71.         );
    
  72.       }
    
  73.     }
    
  74. 
    
  75.     ReactNative.render(<ClassComponent />, 1);
    
  76. 
    
  77.     const reactTag = ReactNative.findNodeHandle(ref.current);
    
  78. 
    
  79.     const componentStack = normalizeCodeLocInfo(
    
  80.       computeComponentStackForErrorReporting(reactTag),
    
  81.     );
    
  82. 
    
  83.     expect(componentStack).toBe(
    
  84.       '\n' +
    
  85.         '    in View (at **)\n' +
    
  86.         '    in FunctionComponent (at **)\n' +
    
  87.         '    in ClassComponent (at **)',
    
  88.     );
    
  89.   });
    
  90. });