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. const ReactTestUtils = require('react-dom/test-utils');
    
  15. const StrictMode = React.StrictMode;
    
  16. 
    
  17. describe('findDOMNode', () => {
    
  18.   it('findDOMNode should return null if passed null', () => {
    
  19.     expect(ReactDOM.findDOMNode(null)).toBe(null);
    
  20.   });
    
  21. 
    
  22.   it('findDOMNode should find dom element', () => {
    
  23.     class MyNode extends React.Component {
    
  24.       render() {
    
  25.         return (
    
  26.           <div>
    
  27.             <span>Noise</span>
    
  28.           </div>
    
  29.         );
    
  30.       }
    
  31.     }
    
  32. 
    
  33.     const myNode = ReactTestUtils.renderIntoDocument(<MyNode />);
    
  34.     const myDiv = ReactDOM.findDOMNode(myNode);
    
  35.     const mySameDiv = ReactDOM.findDOMNode(myDiv);
    
  36.     expect(myDiv.tagName).toBe('DIV');
    
  37.     expect(mySameDiv).toBe(myDiv);
    
  38.   });
    
  39. 
    
  40.   it('findDOMNode should find dom element after an update from null', () => {
    
  41.     function Bar({flag}) {
    
  42.       if (flag) {
    
  43.         return <span>A</span>;
    
  44.       }
    
  45.       return null;
    
  46.     }
    
  47.     class MyNode extends React.Component {
    
  48.       render() {
    
  49.         return <Bar flag={this.props.flag} />;
    
  50.       }
    
  51.     }
    
  52. 
    
  53.     const container = document.createElement('div');
    
  54. 
    
  55.     const myNodeA = ReactDOM.render(<MyNode />, container);
    
  56.     const a = ReactDOM.findDOMNode(myNodeA);
    
  57.     expect(a).toBe(null);
    
  58. 
    
  59.     const myNodeB = ReactDOM.render(<MyNode flag={true} />, container);
    
  60.     expect(myNodeA === myNodeB).toBe(true);
    
  61. 
    
  62.     const b = ReactDOM.findDOMNode(myNodeB);
    
  63.     expect(b.tagName).toBe('SPAN');
    
  64.   });
    
  65. 
    
  66.   it('findDOMNode should reject random objects', () => {
    
  67.     expect(function () {
    
  68.       ReactDOM.findDOMNode({foo: 'bar'});
    
  69.     }).toThrowError('Argument appears to not be a ReactComponent. Keys: foo');
    
  70.   });
    
  71. 
    
  72.   it('findDOMNode should reject unmounted objects with render func', () => {
    
  73.     class Foo extends React.Component {
    
  74.       render() {
    
  75.         return <div />;
    
  76.       }
    
  77.     }
    
  78. 
    
  79.     const container = document.createElement('div');
    
  80.     const inst = ReactDOM.render(<Foo />, container);
    
  81.     ReactDOM.unmountComponentAtNode(container);
    
  82. 
    
  83.     expect(() => ReactDOM.findDOMNode(inst)).toThrowError(
    
  84.       'Unable to find node on an unmounted component.',
    
  85.     );
    
  86.   });
    
  87. 
    
  88.   it('findDOMNode should not throw an error when called within a component that is not mounted', () => {
    
  89.     class Bar extends React.Component {
    
  90.       UNSAFE_componentWillMount() {
    
  91.         expect(ReactDOM.findDOMNode(this)).toBeNull();
    
  92.       }
    
  93. 
    
  94.       render() {
    
  95.         return <div />;
    
  96.       }
    
  97.     }
    
  98.     expect(() => ReactTestUtils.renderIntoDocument(<Bar />)).not.toThrow();
    
  99.   });
    
  100. 
    
  101.   it('findDOMNode should warn if used to find a host component inside StrictMode', () => {
    
  102.     let parent = undefined;
    
  103.     let child = undefined;
    
  104. 
    
  105.     class ContainsStrictModeChild extends React.Component {
    
  106.       render() {
    
  107.         return (
    
  108.           <StrictMode>
    
  109.             <div ref={n => (child = n)} />
    
  110.           </StrictMode>
    
  111.         );
    
  112.       }
    
  113.     }
    
  114. 
    
  115.     ReactTestUtils.renderIntoDocument(
    
  116.       <ContainsStrictModeChild ref={n => (parent = n)} />,
    
  117.     );
    
  118. 
    
  119.     let match;
    
  120.     expect(() => (match = ReactDOM.findDOMNode(parent))).toErrorDev([
    
  121.       'Warning: findDOMNode is deprecated in StrictMode. ' +
    
  122.         'findDOMNode was passed an instance of ContainsStrictModeChild which renders StrictMode children. ' +
    
  123.         'Instead, add a ref directly to the element you want to reference. ' +
    
  124.         'Learn more about using refs safely here: ' +
    
  125.         'https://reactjs.org/link/strict-mode-find-node' +
    
  126.         '\n    in div (at **)' +
    
  127.         '\n    in ContainsStrictModeChild (at **)',
    
  128.     ]);
    
  129.     expect(match).toBe(child);
    
  130.   });
    
  131. 
    
  132.   it('findDOMNode should warn if passed a component that is inside StrictMode', () => {
    
  133.     let parent = undefined;
    
  134.     let child = undefined;
    
  135. 
    
  136.     class IsInStrictMode extends React.Component {
    
  137.       render() {
    
  138.         return <div ref={n => (child = n)} />;
    
  139.       }
    
  140.     }
    
  141. 
    
  142.     ReactTestUtils.renderIntoDocument(
    
  143.       <StrictMode>
    
  144.         <IsInStrictMode ref={n => (parent = n)} />
    
  145.       </StrictMode>,
    
  146.     );
    
  147. 
    
  148.     let match;
    
  149.     expect(() => (match = ReactDOM.findDOMNode(parent))).toErrorDev([
    
  150.       'Warning: findDOMNode is deprecated in StrictMode. ' +
    
  151.         'findDOMNode was passed an instance of IsInStrictMode which is inside StrictMode. ' +
    
  152.         'Instead, add a ref directly to the element you want to reference. ' +
    
  153.         'Learn more about using refs safely here: ' +
    
  154.         'https://reactjs.org/link/strict-mode-find-node' +
    
  155.         '\n    in div (at **)' +
    
  156.         '\n    in IsInStrictMode (at **)',
    
  157.     ]);
    
  158.     expect(match).toBe(child);
    
  159.   });
    
  160. });