| 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 | // Requires |
| 13 | let React; |
| 14 | let ReactDOMClient; |
| 15 | let act; |
| 16 | |
| 17 | // Test components |
| 18 | let LowerLevelComposite; |
| 19 | let MyCompositeComponent; |
| 20 | |
| 21 | /** |
| 22 | * Integration test, testing the combination of JSX with our unit of |
| 23 | * abstraction, `ReactCompositeComponent` does not ever add superfluous DOM |
| 24 | * nodes. |
| 25 | */ |
| 26 | describe('ReactCompositeComponentDOMMinimalism', () => { |
| 27 | beforeEach(() => { |
| 28 | React = require('react'); |
| 29 | ReactDOMClient = require('react-dom/client'); |
| 30 | act = require('internal-test-utils').act; |
| 31 | |
| 32 | LowerLevelComposite = class extends React.Component { |
| 33 | render() { |
| 34 | return <div>{this.props.children}</div>; |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | MyCompositeComponent = class extends React.Component { |
| 39 | render() { |
| 40 | return <LowerLevelComposite>{this.props.children}</LowerLevelComposite>; |
| 41 | } |
| 42 | }; |
| 43 | }); |
| 44 | |
| 45 | it('should not render extra nodes for non-interpolated text', async () => { |
| 46 | const container = document.createElement('div'); |
| 47 | const root = ReactDOMClient.createRoot(container); |
| 48 | await act(() => { |
| 49 | root.render(<MyCompositeComponent>A string child</MyCompositeComponent>); |
| 50 | }); |
| 51 | |
| 52 | const instance = container.firstChild; |
| 53 | expect(instance.tagName).toBe('DIV'); |
| 54 | expect(instance.children.length).toBe(0); |
| 55 | }); |
| 56 | |
| 57 | it('should not render extra nodes for interpolated text', async () => { |
| 58 | const container = document.createElement('div'); |
| 59 | const root = ReactDOMClient.createRoot(container); |
| 60 | await act(() => { |
| 61 | root.render( |
| 62 | <MyCompositeComponent> |
| 63 | {'Interpolated String Child'} |
| 64 | </MyCompositeComponent>, |
| 65 | ); |
| 66 | }); |
| 67 | |
| 68 | const instance = container.firstChild; |
| 69 | expect(instance.tagName).toBe('DIV'); |
| 70 | expect(instance.children.length).toBe(0); |
| 71 | }); |
| 72 | |
| 73 | it('should not render extra nodes for interpolated text children', async () => { |
| 74 | const container = document.createElement('div'); |
| 75 | const root = ReactDOMClient.createRoot(container); |
| 76 | await act(() => { |
| 77 | root.render( |
| 78 | <MyCompositeComponent> |
| 79 | <ul>This text causes no children in ul, just innerHTML</ul> |
| 80 | </MyCompositeComponent>, |
| 81 | ); |
| 82 | }); |
| 83 | |
| 84 | const instance = container.firstChild; |
| 85 | expect(instance.tagName).toBe('DIV'); |
| 86 | expect(instance.children.length).toBe(1); |
| 87 | expect(instance.children[0].tagName).toBe('UL'); |
| 88 | expect(instance.children[0].children.length).toBe(0); |
| 89 | }); |
| 90 | }); |