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. let React;
    
  13. let ReactDOM;
    
  14. 
    
  15. describe('ReactEventIndependence', () => {
    
  16.   beforeEach(() => {
    
  17.     jest.resetModules();
    
  18. 
    
  19.     React = require('react');
    
  20.     ReactDOM = require('react-dom');
    
  21.   });
    
  22. 
    
  23.   it('does not crash with other react inside', () => {
    
  24.     let clicks = 0;
    
  25.     const container = document.createElement('div');
    
  26.     document.body.appendChild(container);
    
  27.     try {
    
  28.       const div = ReactDOM.render(
    
  29.         <div
    
  30.           onClick={() => clicks++}
    
  31.           dangerouslySetInnerHTML={{
    
  32.             __html: '<button data-reactid=".z">click me</div>',
    
  33.           }}
    
  34.         />,
    
  35.         container,
    
  36.       );
    
  37. 
    
  38.       div.firstChild.click();
    
  39.       expect(clicks).toBe(1);
    
  40.     } finally {
    
  41.       document.body.removeChild(container);
    
  42.     }
    
  43.   });
    
  44. 
    
  45.   it('does not crash with other react outside', () => {
    
  46.     let clicks = 0;
    
  47.     const outer = document.createElement('div');
    
  48.     document.body.appendChild(outer);
    
  49.     try {
    
  50.       outer.setAttribute('data-reactid', '.z');
    
  51.       const inner = ReactDOM.render(
    
  52.         <button onClick={() => clicks++}>click me</button>,
    
  53.         outer,
    
  54.       );
    
  55.       inner.click();
    
  56.       expect(clicks).toBe(1);
    
  57.     } finally {
    
  58.       document.body.removeChild(outer);
    
  59.     }
    
  60.   });
    
  61. 
    
  62.   it('does not when event fired on unmounted tree', () => {
    
  63.     let clicks = 0;
    
  64.     const container = document.createElement('div');
    
  65.     document.body.appendChild(container);
    
  66.     try {
    
  67.       const button = ReactDOM.render(
    
  68.         <button onClick={() => clicks++}>click me</button>,
    
  69.         container,
    
  70.       );
    
  71. 
    
  72.       // Now we unmount the component, as if caused by a non-React event handler
    
  73.       // for the same click we're about to simulate, like closing a layer:
    
  74.       ReactDOM.unmountComponentAtNode(container);
    
  75.       button.click();
    
  76. 
    
  77.       // Since the tree is unmounted, we don't dispatch the click event.
    
  78.       expect(clicks).toBe(0);
    
  79.     } finally {
    
  80.       document.body.removeChild(container);
    
  81.     }
    
  82.   });
    
  83. });