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.  * @flow
    
  8.  */
    
  9. 
    
  10. import {normalizeCodeLocInfo} from './utils';
    
  11. 
    
  12. describe('component stack', () => {
    
  13.   let React;
    
  14.   let act;
    
  15.   let legacyRender;
    
  16.   let mockError;
    
  17.   let mockWarn;
    
  18. 
    
  19.   beforeEach(() => {
    
  20.     // Intercept native console methods before DevTools bootstraps.
    
  21.     // Normalize component stack locations.
    
  22.     mockError = jest.fn();
    
  23.     mockWarn = jest.fn();
    
  24.     console.error = (...args) => {
    
  25.       mockError(...args.map(normalizeCodeLocInfo));
    
  26.     };
    
  27.     console.warn = (...args) => {
    
  28.       mockWarn(...args.map(normalizeCodeLocInfo));
    
  29.     };
    
  30. 
    
  31.     const utils = require('./utils');
    
  32.     act = utils.act;
    
  33.     legacyRender = utils.legacyRender;
    
  34. 
    
  35.     React = require('react');
    
  36.   });
    
  37. 
    
  38.   // @reactVersion >=16.9
    
  39.   it('should log the current component stack along with an error or warning', () => {
    
  40.     const Grandparent = () => <Parent />;
    
  41.     const Parent = () => <Child />;
    
  42.     const Child = () => {
    
  43.       console.error('Test error.');
    
  44.       console.warn('Test warning.');
    
  45.       return null;
    
  46.     };
    
  47. 
    
  48.     const container = document.createElement('div');
    
  49. 
    
  50.     act(() => legacyRender(<Grandparent />, container));
    
  51. 
    
  52.     expect(mockError).toHaveBeenCalledWith(
    
  53.       'Test error.',
    
  54.       '\n    in Child (at **)' +
    
  55.         '\n    in Parent (at **)' +
    
  56.         '\n    in Grandparent (at **)',
    
  57.     );
    
  58.     expect(mockWarn).toHaveBeenCalledWith(
    
  59.       'Test warning.',
    
  60.       '\n    in Child (at **)' +
    
  61.         '\n    in Parent (at **)' +
    
  62.         '\n    in Grandparent (at **)',
    
  63.     );
    
  64.   });
    
  65. 
    
  66.   // This test should have caught #19911
    
  67.   // but didn't because both DevTools and ReactDOM are running in the same memory space,
    
  68.   // so the case we're testing against (DevTools prod build and React DEV build) doesn't exist.
    
  69.   // It would be nice to figure out a way to test this combination at some point...
    
  70.   xit('should disable the current dispatcher before shallow rendering so no effects get scheduled', () => {
    
  71.     let useEffectCount = 0;
    
  72. 
    
  73.     const Example = props => {
    
  74.       React.useEffect(() => {
    
  75.         useEffectCount++;
    
  76.         expect(props).toBeDefined();
    
  77.       }, [props]);
    
  78.       console.warn('Warning to trigger appended component stacks.');
    
  79.       return null;
    
  80.     };
    
  81. 
    
  82.     const container = document.createElement('div');
    
  83.     act(() => legacyRender(<Example test="abc" />, container));
    
  84. 
    
  85.     expect(useEffectCount).toBe(1);
    
  86. 
    
  87.     expect(mockWarn).toHaveBeenCalledWith(
    
  88.       'Warning to trigger appended component stacks.',
    
  89.       '\n    in Example (at **)',
    
  90.     );
    
  91.   });
    
  92. });