| 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 ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegrationTestUtils'); |
| 13 | |
| 14 | let React; |
| 15 | let ReactDOMClient; |
| 16 | let ReactDOMServer; |
| 17 | |
| 18 | function initModules() { |
| 19 | // Reset warning cache. |
| 20 | jest.resetModules(); |
| 21 | React = require('react'); |
| 22 | ReactDOMClient = require('react-dom/client'); |
| 23 | ReactDOMServer = require('react-dom/server'); |
| 24 | |
| 25 | // Make them available to the helpers. |
| 26 | return { |
| 27 | ReactDOMClient, |
| 28 | ReactDOMServer, |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | const {resetModules, clientRenderOnServerString, expectMarkupMatch} = |
| 33 | ReactDOMServerIntegrationUtils(initModules); |
| 34 | |
| 35 | describe('ReactDOMServerIntegration', () => { |
| 36 | beforeEach(() => { |
| 37 | resetModules(); |
| 38 | }); |
| 39 | |
| 40 | describe('refs', function () { |
| 41 | it('should not run ref code on server', async () => { |
| 42 | let refCount = 0; |
| 43 | class RefsComponent extends React.Component { |
| 44 | render() { |
| 45 | return <div ref={e => refCount++} />; |
| 46 | } |
| 47 | } |
| 48 | await expectMarkupMatch(<RefsComponent />, <div />); |
| 49 | expect(refCount).toBe(0); |
| 50 | }); |
| 51 | |
| 52 | it('should run ref code on client', async () => { |
| 53 | let refCount = 0; |
| 54 | class RefsComponent extends React.Component { |
| 55 | render() { |
| 56 | return <div ref={e => refCount++} />; |
| 57 | } |
| 58 | } |
| 59 | await expectMarkupMatch(<div />, <RefsComponent />); |
| 60 | expect(refCount).toBe(1); |
| 61 | }); |
| 62 | |
| 63 | it('should send the correct element to ref functions on client', async () => { |
| 64 | let refElement = null; |
| 65 | class RefsComponent extends React.Component { |
| 66 | render() { |
| 67 | return <div ref={e => (refElement = e)} />; |
| 68 | } |
| 69 | } |
| 70 | const e = await clientRenderOnServerString(<RefsComponent />); |
| 71 | expect(refElement).not.toBe(null); |
| 72 | expect(refElement).toBe(e); |
| 73 | }); |
| 74 | }); |
| 75 | |
| 76 | it('should forward refs', async () => { |
| 77 | const divRef = React.createRef(); |
| 78 | |
| 79 | class InnerComponent extends React.Component { |
| 80 | render() { |
| 81 | return <div ref={this.props.forwardedRef}>{this.props.value}</div>; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | const OuterComponent = React.forwardRef((props, ref) => ( |
| 86 | <InnerComponent {...props} forwardedRef={ref} /> |
| 87 | )); |
| 88 | |
| 89 | await clientRenderOnServerString( |
| 90 | <OuterComponent ref={divRef} value="hello" />, |
| 91 | ); |
| 92 | |
| 93 | expect(divRef.current).not.toBe(null); |
| 94 | expect(divRef.current.textContent).toBe('hello'); |
| 95 | }); |
| 96 | }); |