| 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 | * @jest-environment node |
| 9 | */ |
| 10 | |
| 11 | 'use strict'; |
| 12 | |
| 13 | if (typeof Blob === 'undefined') { |
| 14 | global.Blob = require('buffer').Blob; |
| 15 | } |
| 16 | if (typeof File === 'undefined' || typeof FormData === 'undefined') { |
| 17 | global.File = require('undici').File; |
| 18 | global.FormData = require('undici').FormData; |
| 19 | } |
| 20 | |
| 21 | let act; |
| 22 | let React; |
| 23 | let ReactNoop; |
| 24 | let ReactNoopFlightServer; |
| 25 | let ReactNoopFlightClient; |
| 26 | let getDebugInfo; |
| 27 | |
| 28 | describe('ReactFlight', () => { |
| 29 | beforeEach(() => { |
| 30 | // Mock performance.now for timing tests |
| 31 | let time = 10; |
| 32 | jest.spyOn(performance, 'timeOrigin', 'get').mockReturnValue(time); |
| 33 | jest.spyOn(performance, 'now').mockImplementation(() => { |
| 34 | return time++; |
| 35 | }); |
| 36 | |
| 37 | jest.resetModules(); |
| 38 | jest.mock('react', () => require('react/react.react-server')); |
| 39 | ReactNoopFlightServer = require('react-noop-renderer/flight-server'); |
| 40 | // This stores the state so we need to preserve it |
| 41 | const flightModules = require('react-noop-renderer/flight-modules'); |
| 42 | jest.resetModules(); |
| 43 | __unmockReact(); |
| 44 | jest.mock('react-noop-renderer/flight-modules', () => flightModules); |
| 45 | React = require('react'); |
| 46 | ReactNoop = require('react-noop-renderer'); |
| 47 | ReactNoopFlightClient = require('react-noop-renderer/flight-client'); |
| 48 | act = require('internal-test-utils').act; |
| 49 | |
| 50 | getDebugInfo = require('internal-test-utils').getDebugInfo.bind(null, { |
| 51 | useV8Stack: true, |
| 52 | ignoreRscStreamInfo: true, |
| 53 | }); |
| 54 | }); |
| 55 | |
| 56 | afterEach(() => { |
| 57 | jest.restoreAllMocks(); |
| 58 | }); |
| 59 | |
| 60 | // @gate __DEV__ && enableComponentPerformanceTrack |
| 61 | it('can render deep but cut off JSX in debug info', async () => { |
| 62 | function createDeepJSX(n) { |
| 63 | if (n <= 0) { |
| 64 | return null; |
| 65 | } |
| 66 | return <div>{createDeepJSX(n - 1)}</div>; |
| 67 | } |
| 68 | |
| 69 | function ServerComponent(props) { |
| 70 | return <div>not using props</div>; |
| 71 | } |
| 72 | |
| 73 | const debugChannel = {onMessage(message) {}}; |
| 74 | |
| 75 | const transport = ReactNoopFlightServer.render( |
| 76 | { |
| 77 | root: ( |
| 78 | <ServerComponent> |
| 79 | {createDeepJSX(100) /* deper than objectLimit */} |
| 80 | </ServerComponent> |
| 81 | ), |
| 82 | }, |
| 83 | {debugChannel}, |
| 84 | ); |
| 85 | |
| 86 | await act(async () => { |
| 87 | const rootModel = await ReactNoopFlightClient.read(transport, { |
| 88 | debugChannel, |
| 89 | }); |
| 90 | const root = rootModel.root; |
| 91 | const children = getDebugInfo(root)[1].props.children; |
| 92 | expect(children.type).toBe('div'); |
| 93 | expect(children.props.children.type).toBe('div'); |
| 94 | ReactNoop.render(root); |
| 95 | }); |
| 96 | |
| 97 | expect(ReactNoop).toMatchRenderedOutput(<div>not using props</div>); |
| 98 | }); |
| 99 | }); |