55 * LICENSE file in the root directory of this source tree.
66 *
77 * @emails react-core
8+ * @jest -environment node
89 */
910
1011'use strict' ;
1112
1213let React ;
1314let ReactCallReturn ;
14- let ReactDOM ;
1515let ReactDOMServer ;
16- let ReactTestUtils ;
1716let PropTypes ;
1817
1918function normalizeCodeLocInfo ( str ) {
@@ -25,8 +24,6 @@ describe('ReactDOMServer', () => {
2524 jest . resetModules ( ) ;
2625 React = require ( 'react' ) ;
2726 ReactCallReturn = require ( 'react-call-return' ) ;
28- ReactDOM = require ( 'react-dom' ) ;
29- ReactTestUtils = require ( 'react-dom/test-utils' ) ;
3027 PropTypes = require ( 'prop-types' ) ;
3128 ReactDOMServer = require ( 'react-dom/server' ) ;
3229 } ) ;
@@ -157,243 +154,6 @@ describe('ReactDOMServer', () => {
157154 runTest ( ) ;
158155 } ) ;
159156
160- it ( 'should have the correct mounting behavior (old hydrate API)' , ( ) => {
161- spyOnDev ( console , 'warn' ) ;
162- spyOnDev ( console , 'error' ) ;
163-
164- let mountCount = 0 ;
165- let numClicks = 0 ;
166-
167- class TestComponent extends React . Component {
168- componentDidMount ( ) {
169- mountCount ++ ;
170- }
171-
172- click = ( ) => {
173- numClicks ++ ;
174- } ;
175-
176- render ( ) {
177- return (
178- < span ref = "span" onClick = { this . click } >
179- Name: { this . props . name }
180- </ span >
181- ) ;
182- }
183- }
184-
185- const element = document . createElement ( 'div' ) ;
186- ReactDOM . render ( < TestComponent /> , element ) ;
187-
188- let lastMarkup = element . innerHTML ;
189-
190- // Exercise the update path. Markup should not change,
191- // but some lifecycle methods should be run again.
192- ReactDOM . render ( < TestComponent name = "x" /> , element ) ;
193- expect ( mountCount ) . toEqual ( 1 ) ;
194-
195- // Unmount and remount. We should get another mount event and
196- // we should get different markup, as the IDs are unique each time.
197- ReactDOM . unmountComponentAtNode ( element ) ;
198- expect ( element . innerHTML ) . toEqual ( '' ) ;
199- ReactDOM . render ( < TestComponent name = "x" /> , element ) ;
200- expect ( mountCount ) . toEqual ( 2 ) ;
201- expect ( element . innerHTML ) . not . toEqual ( lastMarkup ) ;
202-
203- // Now kill the node and render it on top of server-rendered markup, as if
204- // we used server rendering. We should mount again, but the markup should
205- // be unchanged. We will append a sentinel at the end of innerHTML to be
206- // sure that innerHTML was not changed.
207- ReactDOM . unmountComponentAtNode ( element ) ;
208- expect ( element . innerHTML ) . toEqual ( '' ) ;
209-
210- lastMarkup = ReactDOMServer . renderToString ( < TestComponent name = "x" /> ) ;
211- element . innerHTML = lastMarkup ;
212-
213- let instance = ReactDOM . render ( < TestComponent name = "x" /> , element ) ;
214- expect ( mountCount ) . toEqual ( 3 ) ;
215- if ( __DEV__ ) {
216- expect ( console . warn . calls . count ( ) ) . toBe ( 1 ) ;
217- expect ( console . warn . calls . argsFor ( 0 ) [ 0 ] ) . toContain (
218- 'render(): Calling ReactDOM.render() to hydrate server-rendered markup ' +
219- 'will stop working in React v17. Replace the ReactDOM.render() call ' +
220- 'with ReactDOM.hydrate() if you want React to attach to the server HTML.' ,
221- ) ;
222- console . warn . calls . reset ( ) ;
223- }
224- expect ( element . innerHTML ) . toBe ( lastMarkup ) ;
225-
226- // Ensure the events system works after mount into server markup
227- expect ( numClicks ) . toEqual ( 0 ) ;
228- ReactTestUtils . Simulate . click ( ReactDOM . findDOMNode ( instance . refs . span ) ) ;
229- expect ( numClicks ) . toEqual ( 1 ) ;
230-
231- ReactDOM . unmountComponentAtNode ( element ) ;
232- expect ( element . innerHTML ) . toEqual ( '' ) ;
233-
234- // Now simulate a situation where the app is not idempotent. React should
235- // warn but do the right thing.
236- element . innerHTML = lastMarkup ;
237- instance = ReactDOM . render ( < TestComponent name = "y" /> , element ) ;
238- expect ( mountCount ) . toEqual ( 4 ) ;
239- if ( __DEV__ ) {
240- expect ( console . error . calls . count ( ) ) . toBe ( 1 ) ;
241- expect ( console . error . calls . argsFor ( 0 ) [ 0 ] ) . toContain (
242- 'Text content did not match. Server: "x" Client: "y"' ,
243- ) ;
244- console . error . calls . reset ( ) ;
245- }
246- expect ( element . innerHTML . length > 0 ) . toBe ( true ) ;
247- expect ( element . innerHTML ) . not . toEqual ( lastMarkup ) ;
248-
249- // Ensure the events system works after markup mismatch.
250- expect ( numClicks ) . toEqual ( 1 ) ;
251- ReactTestUtils . Simulate . click ( ReactDOM . findDOMNode ( instance . refs . span ) ) ;
252- expect ( numClicks ) . toEqual ( 2 ) ;
253- if ( __DEV__ ) {
254- expect ( console . warn . calls . count ( ) ) . toBe ( 0 ) ;
255- expect ( console . error . calls . count ( ) ) . toBe ( 0 ) ;
256- }
257- } ) ;
258-
259- it ( 'should have the correct mounting behavior (new hydrate API)' , ( ) => {
260- spyOnDev ( console , 'error' ) ;
261-
262- let mountCount = 0 ;
263- let numClicks = 0 ;
264-
265- class TestComponent extends React . Component {
266- componentDidMount ( ) {
267- mountCount ++ ;
268- }
269-
270- click = ( ) => {
271- numClicks ++ ;
272- } ;
273-
274- render ( ) {
275- return (
276- < span ref = "span" onClick = { this . click } >
277- Name: { this . props . name }
278- </ span >
279- ) ;
280- }
281- }
282-
283- const element = document . createElement ( 'div' ) ;
284- ReactDOM . render ( < TestComponent /> , element ) ;
285-
286- let lastMarkup = element . innerHTML ;
287-
288- // Exercise the update path. Markup should not change,
289- // but some lifecycle methods should be run again.
290- ReactDOM . render ( < TestComponent name = "x" /> , element ) ;
291- expect ( mountCount ) . toEqual ( 1 ) ;
292-
293- // Unmount and remount. We should get another mount event and
294- // we should get different markup, as the IDs are unique each time.
295- ReactDOM . unmountComponentAtNode ( element ) ;
296- expect ( element . innerHTML ) . toEqual ( '' ) ;
297- ReactDOM . render ( < TestComponent name = "x" /> , element ) ;
298- expect ( mountCount ) . toEqual ( 2 ) ;
299- expect ( element . innerHTML ) . not . toEqual ( lastMarkup ) ;
300-
301- // Now kill the node and render it on top of server-rendered markup, as if
302- // we used server rendering. We should mount again, but the markup should
303- // be unchanged. We will append a sentinel at the end of innerHTML to be
304- // sure that innerHTML was not changed.
305- ReactDOM . unmountComponentAtNode ( element ) ;
306- expect ( element . innerHTML ) . toEqual ( '' ) ;
307-
308- lastMarkup = ReactDOMServer . renderToString ( < TestComponent name = "x" /> ) ;
309- element . innerHTML = lastMarkup ;
310-
311- let instance = ReactDOM . hydrate ( < TestComponent name = "x" /> , element ) ;
312- expect ( mountCount ) . toEqual ( 3 ) ;
313- expect ( element . innerHTML ) . toBe ( lastMarkup ) ;
314-
315- // Ensure the events system works after mount into server markup
316- expect ( numClicks ) . toEqual ( 0 ) ;
317- ReactTestUtils . Simulate . click ( ReactDOM . findDOMNode ( instance . refs . span ) ) ;
318- expect ( numClicks ) . toEqual ( 1 ) ;
319-
320- ReactDOM . unmountComponentAtNode ( element ) ;
321- expect ( element . innerHTML ) . toEqual ( '' ) ;
322-
323- // Now simulate a situation where the app is not idempotent. React should
324- // warn but do the right thing.
325- element . innerHTML = lastMarkup ;
326- instance = ReactDOM . hydrate ( < TestComponent name = "y" /> , element ) ;
327- expect ( mountCount ) . toEqual ( 4 ) ;
328- if ( __DEV__ ) {
329- expect ( console . error . calls . count ( ) ) . toBe ( 1 ) ;
330- }
331- expect ( element . innerHTML . length > 0 ) . toBe ( true ) ;
332- expect ( element . innerHTML ) . not . toEqual ( lastMarkup ) ;
333-
334- // Ensure the events system works after markup mismatch.
335- expect ( numClicks ) . toEqual ( 1 ) ;
336- ReactTestUtils . Simulate . click ( ReactDOM . findDOMNode ( instance . refs . span ) ) ;
337- expect ( numClicks ) . toEqual ( 2 ) ;
338- } ) ;
339-
340- // We have a polyfill for autoFocus on the client, but we intentionally don't
341- // want it to call focus() when hydrating because this can mess up existing
342- // focus before the JS has loaded.
343- it ( 'should emit autofocus on the server but not focus() when hydrating' , ( ) => {
344- const element = document . createElement ( 'div' ) ;
345- element . innerHTML = ReactDOMServer . renderToString (
346- < input autoFocus = { true } /> ,
347- ) ;
348- expect ( element . firstChild . autofocus ) . toBe ( true ) ;
349-
350- // It should not be called on mount.
351- element . firstChild . focus = jest . fn ( ) ;
352- ReactDOM . hydrate ( < input autoFocus = { true } /> , element ) ;
353- expect ( element . firstChild . focus ) . not . toHaveBeenCalled ( ) ;
354-
355- // Or during an update.
356- ReactDOM . render ( < input autoFocus = { true } /> , element ) ;
357- expect ( element . firstChild . focus ) . not . toHaveBeenCalled ( ) ;
358- } ) ;
359-
360- it ( 'should not focus on either server or client with autofocus={false}' , ( ) => {
361- const element = document . createElement ( 'div' ) ;
362- element . innerHTML = ReactDOMServer . renderToString (
363- < input autoFocus = { false } /> ,
364- ) ;
365- expect ( element . firstChild . autofocus ) . toBe ( false ) ;
366-
367- element . firstChild . focus = jest . fn ( ) ;
368- ReactDOM . hydrate ( < input autoFocus = { false } /> , element ) ;
369- expect ( element . firstChild . focus ) . not . toHaveBeenCalled ( ) ;
370-
371- ReactDOM . render ( < input autoFocus = { false } /> , element ) ;
372- expect ( element . firstChild . focus ) . not . toHaveBeenCalled ( ) ;
373- } ) ;
374-
375- // Regression test for https://github.com/facebook/react/issues/11726
376- it ( 'should not focus on either server or client with autofocus={false} even if there is a markup mismatch' , ( ) => {
377- spyOnDev ( console , 'error' ) ;
378-
379- const element = document . createElement ( 'div' ) ;
380- element . innerHTML = ReactDOMServer . renderToString (
381- < button autoFocus = { false } > server</ button > ,
382- ) ;
383- expect ( element . firstChild . autofocus ) . toBe ( false ) ;
384-
385- element . firstChild . focus = jest . fn ( ) ;
386- ReactDOM . hydrate ( < button autoFocus = { false } > client</ button > , element ) ;
387-
388- expect ( element . firstChild . focus ) . not . toHaveBeenCalled ( ) ;
389- if ( __DEV__ ) {
390- expect ( console . error . calls . count ( ) ) . toBe ( 1 ) ;
391- expect ( console . error . calls . argsFor ( 0 ) [ 0 ] ) . toBe (
392- 'Warning: Text content did not match. Server: "server" Client: "client"' ,
393- ) ;
394- }
395- } ) ;
396-
397157 it ( 'should throw with silly args' , ( ) => {
398158 expect (
399159 ReactDOMServer . renderToString . bind ( ReactDOMServer , { x : 123 } ) ,
@@ -797,20 +557,7 @@ describe('ReactDOMServer', () => {
797557 }
798558 } ) ;
799559
800- it ( 'should throw rendering portals on the server' , ( ) => {
801- const div = document . createElement ( 'div' ) ;
802- expect ( ( ) => {
803- ReactDOMServer . renderToString (
804- < div > { ReactDOM . createPortal ( < div /> , div ) } </ div > ,
805- ) ;
806- } ) . toThrow (
807- 'Portals are not currently supported by the server renderer. ' +
808- 'Render them conditionally so that they only appear on the client render.' ,
809- ) ;
810- } ) ;
811-
812560 it ( 'should throw rendering call/return on the server' , ( ) => {
813- const div = document . createElement ( 'div' ) ;
814561 expect ( ( ) => {
815562 ReactDOMServer . renderToString (
816563 < div > { ReactCallReturn . unstable_createReturn ( 42 ) } </ div > ,
0 commit comments