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.  */
    
  9. 
    
  10. 'use strict';
    
  11. 
    
  12. const React = require('react');
    
  13. const ReactDOM = require('react-dom');
    
  14. 
    
  15. describe('ReactMount', () => {
    
  16.   it('should destroy a react root upon request', () => {
    
  17.     const mainContainerDiv = document.createElement('div');
    
  18.     document.body.appendChild(mainContainerDiv);
    
  19. 
    
  20.     const instanceOne = <div className="firstReactDiv" />;
    
  21.     const firstRootDiv = document.createElement('div');
    
  22.     mainContainerDiv.appendChild(firstRootDiv);
    
  23.     ReactDOM.render(instanceOne, firstRootDiv);
    
  24. 
    
  25.     const instanceTwo = <div className="secondReactDiv" />;
    
  26.     const secondRootDiv = document.createElement('div');
    
  27.     mainContainerDiv.appendChild(secondRootDiv);
    
  28.     ReactDOM.render(instanceTwo, secondRootDiv);
    
  29. 
    
  30.     // Test that two react roots are rendered in isolation
    
  31.     expect(firstRootDiv.firstChild.className).toBe('firstReactDiv');
    
  32.     expect(secondRootDiv.firstChild.className).toBe('secondReactDiv');
    
  33. 
    
  34.     // Test that after unmounting each, they are no longer in the document.
    
  35.     ReactDOM.unmountComponentAtNode(firstRootDiv);
    
  36.     expect(firstRootDiv.firstChild).toBeNull();
    
  37.     ReactDOM.unmountComponentAtNode(secondRootDiv);
    
  38.     expect(secondRootDiv.firstChild).toBeNull();
    
  39.   });
    
  40. 
    
  41.   it('should warn when unmounting a non-container root node', () => {
    
  42.     const mainContainerDiv = document.createElement('div');
    
  43. 
    
  44.     const component = (
    
  45.       <div>
    
  46.         <div />
    
  47.       </div>
    
  48.     );
    
  49.     ReactDOM.render(component, mainContainerDiv);
    
  50. 
    
  51.     // Test that unmounting at a root node gives a helpful warning
    
  52.     const rootDiv = mainContainerDiv.firstChild;
    
  53.     expect(() => ReactDOM.unmountComponentAtNode(rootDiv)).toErrorDev(
    
  54.       "Warning: unmountComponentAtNode(): The node you're attempting to " +
    
  55.         'unmount was rendered by React and is not a top-level container. You ' +
    
  56.         'may have accidentally passed in a React root node instead of its ' +
    
  57.         'container.',
    
  58.       {withoutStack: true},
    
  59.     );
    
  60.   });
    
  61. 
    
  62.   it('should warn when unmounting a non-container, non-root node', () => {
    
  63.     const mainContainerDiv = document.createElement('div');
    
  64. 
    
  65.     const component = (
    
  66.       <div>
    
  67.         <div>
    
  68.           <div />
    
  69.         </div>
    
  70.       </div>
    
  71.     );
    
  72.     ReactDOM.render(component, mainContainerDiv);
    
  73. 
    
  74.     // Test that unmounting at a non-root node gives a different warning
    
  75.     const nonRootDiv = mainContainerDiv.firstChild.firstChild;
    
  76.     expect(() => ReactDOM.unmountComponentAtNode(nonRootDiv)).toErrorDev(
    
  77.       "Warning: unmountComponentAtNode(): The node you're attempting to " +
    
  78.         'unmount was rendered by React and is not a top-level container. ' +
    
  79.         'Instead, have the parent component update its state and rerender in ' +
    
  80.         'order to remove this component.',
    
  81.       {withoutStack: true},
    
  82.     );
    
  83.   });
    
  84. });