main
js 63 lines 1.9 KB
Raw
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 describe('ReactDOMComponentTree', () => {
13 let React;
14 let ReactDOM;
15 let container;
16 let assertConsoleErrorDev;
17
18 beforeEach(() => {
19 React = require('react');
20 ReactDOM = require('react-dom');
21 assertConsoleErrorDev =
22 require('internal-test-utils').assertConsoleErrorDev;
23
24 container = document.createElement('div');
25 document.body.appendChild(container);
26 });
27
28 afterEach(() => {
29 document.body.removeChild(container);
30 container = null;
31 });
32
33 // @gate !disableLegacyMode
34 it('finds instance of node that is attempted to be unmounted', () => {
35 const component = <div />;
36 const node = ReactDOM.render(<div>{component}</div>, container);
37 ReactDOM.unmountComponentAtNode(node);
38 assertConsoleErrorDev([
39 "unmountComponentAtNode(): The node you're attempting to unmount " +
40 'was rendered by React and is not a top-level container. You may ' +
41 'have accidentally passed in a React root node instead of its ' +
42 'container.',
43 ]);
44 });
45
46 // @gate !disableLegacyMode
47 it('finds instance from node to stop rendering over other react rendered components', () => {
48 const component = (
49 <div>
50 <span>Hello</span>
51 </div>
52 );
53 const anotherComponent = <div />;
54 const instance = ReactDOM.render(component, container);
55 ReactDOM.render(anotherComponent, instance);
56 assertConsoleErrorDev([
57 'Replacing React-rendered children with a new root ' +
58 'component. If you intended to update the children of this node, ' +
59 'you should instead have the existing children update their state ' +
60 'and render the new components instead of calling ReactDOM.render.',
61 ]);
62 });
63 });