/** * Copyright (c) Facebook, Inc. and its 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 * @jest-environment node */ 'use strict'; let React; let ReactDOMServer; let PropTypes; let ReactCurrentDispatcher; let enableSuspenseServerRenderer = require('shared/ReactFeatureFlags') .enableSuspenseServerRenderer; function normalizeCodeLocInfo(str) { return str && str.replace(/\(at .+?:\d+\)/g, '(at **)'); } describe('ReactDOMServer', () => { beforeEach(() => { jest.resetModules(); React = require('react'); PropTypes = require('prop-types'); ReactDOMServer = require('react-dom/server'); ReactCurrentDispatcher = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED .ReactCurrentDispatcher; }); describe('renderToString', () => { it('should generate simple markup', () => { const response = ReactDOMServer.renderToString(hello world); expect(response).toMatch( new RegExp('hello world'), ); }); it('should generate simple markup for self-closing tags', () => { const response = ReactDOMServer.renderToString(); expect(response).toMatch(new RegExp('')); }); it('should generate comment markup for component returns null', () => { class NullComponent extends React.Component { render() { return null; } } const response = ReactDOMServer.renderToString(); expect(response).toBe(''); }); // TODO: Test that listeners are not registered onto any document/container. it('should render composite components', () => { class Parent extends React.Component { render() { return (
); } } class Child extends React.Component { render() { return My name is {this.props.name}; } } const response = ReactDOMServer.renderToString(); expect(response).toMatch( new RegExp( '
' + '' + 'My name is child' + '' + '
', ), ); }); it('should only execute certain lifecycle methods', () => { function runTest() { const lifecycle = []; class TestComponent extends React.Component { constructor(props) { super(props); lifecycle.push('getInitialState'); this.state = {name: 'TestComponent'}; } UNSAFE_componentWillMount() { lifecycle.push('componentWillMount'); } componentDidMount() { lifecycle.push('componentDidMount'); } render() { lifecycle.push('render'); return Component name: {this.state.name}; } UNSAFE_componentWillUpdate() { lifecycle.push('componentWillUpdate'); } componentDidUpdate() { lifecycle.push('componentDidUpdate'); } shouldComponentUpdate() { lifecycle.push('shouldComponentUpdate'); } UNSAFE_componentWillReceiveProps() { lifecycle.push('componentWillReceiveProps'); } componentWillUnmount() { lifecycle.push('componentWillUnmount'); } } const response = ReactDOMServer.renderToString(); expect(response).toMatch( new RegExp( '' + 'Component name: TestComponent' + '', ), ); expect(lifecycle).toEqual([ 'getInitialState', 'componentWillMount', 'render', ]); } runTest(); }); it('should throw with silly args', () => { expect( ReactDOMServer.renderToString.bind(ReactDOMServer, {x: 123}), ).toThrowError( 'Objects are not valid as a React child (found: object with keys {x})', ); }); it('should throw prop mapping error for an