|
| 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 | +import {insertNodesAndExecuteScripts} from '../test-utils/FizzTestUtils'; |
| 13 | + |
| 14 | +// Polyfills for test environment |
| 15 | +global.ReadableStream = |
| 16 | + require('web-streams-polyfill/ponyfill/es6').ReadableStream; |
| 17 | +global.TextEncoder = require('util').TextEncoder; |
| 18 | + |
| 19 | +let React; |
| 20 | +let ReactDOMServer; |
| 21 | +let Scheduler; |
| 22 | +let assertLog; |
| 23 | +let container; |
| 24 | + |
| 25 | +describe('ReactClassComponentPropResolutionFizz', () => { |
| 26 | + beforeEach(() => { |
| 27 | + jest.resetModules(); |
| 28 | + React = require('react'); |
| 29 | + Scheduler = require('scheduler'); |
| 30 | + ReactDOMServer = require('react-dom/server.browser'); |
| 31 | + assertLog = require('internal-test-utils').assertLog; |
| 32 | + container = document.createElement('div'); |
| 33 | + document.body.appendChild(container); |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(() => { |
| 37 | + document.body.removeChild(container); |
| 38 | + }); |
| 39 | + |
| 40 | + async function readIntoContainer(stream) { |
| 41 | + const reader = stream.getReader(); |
| 42 | + let result = ''; |
| 43 | + while (true) { |
| 44 | + const {done, value} = await reader.read(); |
| 45 | + if (done) { |
| 46 | + break; |
| 47 | + } |
| 48 | + result += Buffer.from(value).toString('utf8'); |
| 49 | + } |
| 50 | + const temp = document.createElement('div'); |
| 51 | + temp.innerHTML = result; |
| 52 | + insertNodesAndExecuteScripts(temp, container, null); |
| 53 | + } |
| 54 | + |
| 55 | + function Text({text}) { |
| 56 | + Scheduler.log(text); |
| 57 | + return text; |
| 58 | + } |
| 59 | + |
| 60 | + test('resolves ref and default props before calling lifecycle methods', async () => { |
| 61 | + function getPropKeys(props) { |
| 62 | + return Object.keys(props).join(', '); |
| 63 | + } |
| 64 | + |
| 65 | + class Component extends React.Component { |
| 66 | + constructor(props) { |
| 67 | + super(props); |
| 68 | + Scheduler.log('constructor: ' + getPropKeys(props)); |
| 69 | + } |
| 70 | + UNSAFE_componentWillMount() { |
| 71 | + Scheduler.log('componentWillMount: ' + getPropKeys(this.props)); |
| 72 | + } |
| 73 | + render() { |
| 74 | + return <Text text={'render: ' + getPropKeys(this.props)} />; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + Component.defaultProps = { |
| 79 | + default: 'yo', |
| 80 | + }; |
| 81 | + |
| 82 | + // `ref` should never appear as a prop. `default` always should. |
| 83 | + const ref = React.createRef(); |
| 84 | + const stream = await ReactDOMServer.renderToReadableStream( |
| 85 | + <Component text="Yay" ref={ref} />, |
| 86 | + ); |
| 87 | + await readIntoContainer(stream); |
| 88 | + assertLog([ |
| 89 | + 'constructor: text, default', |
| 90 | + 'componentWillMount: text, default', |
| 91 | + 'render: text, default', |
| 92 | + ]); |
| 93 | + }); |
| 94 | +}); |
0 commit comments