forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Classes consume ref prop during SSR, too
Same as facebook#28719 but for SSR.
- Loading branch information
Showing
2 changed files
with
128 additions
and
3 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
packages/react-dom/src/__tests__/ReactClassComponentPropResolutionFizz-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import {insertNodesAndExecuteScripts} from '../test-utils/FizzTestUtils'; | ||
|
||
// Polyfills for test environment | ||
global.ReadableStream = | ||
require('web-streams-polyfill/ponyfill/es6').ReadableStream; | ||
global.TextEncoder = require('util').TextEncoder; | ||
|
||
let React; | ||
let ReactDOMServer; | ||
let Scheduler; | ||
let assertLog; | ||
let container; | ||
|
||
describe('ReactClassComponentPropResolutionFizz', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
React = require('react'); | ||
Scheduler = require('scheduler'); | ||
ReactDOMServer = require('react-dom/server.browser'); | ||
assertLog = require('internal-test-utils').assertLog; | ||
container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
}); | ||
|
||
afterEach(() => { | ||
document.body.removeChild(container); | ||
}); | ||
|
||
async function readIntoContainer(stream) { | ||
const reader = stream.getReader(); | ||
let result = ''; | ||
while (true) { | ||
const {done, value} = await reader.read(); | ||
if (done) { | ||
break; | ||
} | ||
result += Buffer.from(value).toString('utf8'); | ||
} | ||
const temp = document.createElement('div'); | ||
temp.innerHTML = result; | ||
insertNodesAndExecuteScripts(temp, container, null); | ||
} | ||
|
||
function Text({text}) { | ||
Scheduler.log(text); | ||
return text; | ||
} | ||
|
||
test('resolves ref and default props before calling lifecycle methods', async () => { | ||
function getPropKeys(props) { | ||
return Object.keys(props).join(', '); | ||
} | ||
|
||
class Component extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
Scheduler.log('constructor: ' + getPropKeys(props)); | ||
} | ||
UNSAFE_componentWillMount() { | ||
Scheduler.log('componentWillMount: ' + getPropKeys(this.props)); | ||
} | ||
render() { | ||
return <Text text={'render: ' + getPropKeys(this.props)} />; | ||
} | ||
} | ||
|
||
Component.defaultProps = { | ||
default: 'yo', | ||
}; | ||
|
||
// `ref` should never appear as a prop. `default` always should. | ||
const ref = React.createRef(); | ||
const stream = await ReactDOMServer.renderToReadableStream( | ||
<Component text="Yay" ref={ref} />, | ||
); | ||
await readIntoContainer(stream); | ||
assertLog([ | ||
'constructor: text, default', | ||
'componentWillMount: text, default', | ||
'render: text, default', | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters