|
| 1 | +/** |
| 2 | + * Copyright (c) 2013-present, Facebook, Inc. |
| 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 | +const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegrationTestUtils'); |
| 13 | + |
| 14 | +const TEXT_NODE_TYPE = 3; |
| 15 | + |
| 16 | +let React; |
| 17 | +let ReactDOM; |
| 18 | +let ReactDOMServer; |
| 19 | + |
| 20 | +// When there is a test that renders on server and then on client and expects a logged |
| 21 | +// error, you want to see the error show up both on server and client. Unfortunately, |
| 22 | +// React refuses to issue the same error twice to avoid clogging up the console. |
| 23 | +// To get around this, we must reload React modules in between server and client render. |
| 24 | +function initModules() { |
| 25 | + // First, reset the modules to load the client renderer. |
| 26 | + jest.resetModuleRegistry(); |
| 27 | + React = require('react'); |
| 28 | + ReactDOM = require('react-dom'); |
| 29 | + |
| 30 | + // Now we reset the modules again to load the server renderer. |
| 31 | + // Resetting is important because we want to avoid any shared state |
| 32 | + // influencing the tests. |
| 33 | + jest.resetModuleRegistry(); |
| 34 | + ReactDOMServer = require('react-dom/server'); |
| 35 | + |
| 36 | + // Make them available to the helpers. |
| 37 | + return { |
| 38 | + ReactDOM, |
| 39 | + ReactDOMServer, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +const {resetModules, itRenders} = ReactDOMServerIntegrationUtils(initModules); |
| 44 | + |
| 45 | +describe('ReactDOMServerIntegration', () => { |
| 46 | + beforeEach(() => { |
| 47 | + resetModules(); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('basic rendering', function() { |
| 51 | + itRenders('a blank div', async render => { |
| 52 | + const e = await render(<div />); |
| 53 | + expect(e.tagName).toBe('DIV'); |
| 54 | + }); |
| 55 | + |
| 56 | + itRenders('a self-closing tag', async render => { |
| 57 | + const e = await render(<br />); |
| 58 | + expect(e.tagName).toBe('BR'); |
| 59 | + }); |
| 60 | + |
| 61 | + itRenders('a self-closing tag as a child', async render => { |
| 62 | + const e = await render( |
| 63 | + <div> |
| 64 | + <br /> |
| 65 | + </div>, |
| 66 | + ); |
| 67 | + expect(e.childNodes.length).toBe(1); |
| 68 | + expect(e.firstChild.tagName).toBe('BR'); |
| 69 | + }); |
| 70 | + |
| 71 | + itRenders('a string', async render => { |
| 72 | + let e = await render('Hello'); |
| 73 | + expect(e.nodeType).toBe(3); |
| 74 | + expect(e.nodeValue).toMatch('Hello'); |
| 75 | + }); |
| 76 | + |
| 77 | + itRenders('a number', async render => { |
| 78 | + let e = await render(42); |
| 79 | + expect(e.nodeType).toBe(3); |
| 80 | + expect(e.nodeValue).toMatch('42'); |
| 81 | + }); |
| 82 | + |
| 83 | + itRenders('an array with one child', async render => { |
| 84 | + let e = await render([<div key={1}>text1</div>]); |
| 85 | + let parent = e.parentNode; |
| 86 | + expect(parent.childNodes[0].tagName).toBe('DIV'); |
| 87 | + }); |
| 88 | + |
| 89 | + itRenders('an array with several children', async render => { |
| 90 | + let Header = props => { |
| 91 | + return <p>header</p>; |
| 92 | + }; |
| 93 | + let Footer = props => { |
| 94 | + return [<h2 key={1}>footer</h2>, <h3 key={2}>about</h3>]; |
| 95 | + }; |
| 96 | + let e = await render([ |
| 97 | + <div key={1}>text1</div>, |
| 98 | + <span key={2}>text2</span>, |
| 99 | + <Header key={3} />, |
| 100 | + <Footer key={4} />, |
| 101 | + ]); |
| 102 | + let parent = e.parentNode; |
| 103 | + expect(parent.childNodes[0].tagName).toBe('DIV'); |
| 104 | + expect(parent.childNodes[1].tagName).toBe('SPAN'); |
| 105 | + expect(parent.childNodes[2].tagName).toBe('P'); |
| 106 | + expect(parent.childNodes[3].tagName).toBe('H2'); |
| 107 | + expect(parent.childNodes[4].tagName).toBe('H3'); |
| 108 | + }); |
| 109 | + |
| 110 | + itRenders('a nested array', async render => { |
| 111 | + let e = await render([ |
| 112 | + [<div key={1}>text1</div>], |
| 113 | + <span key={1}>text2</span>, |
| 114 | + [[[null, <p key={1} />], false]], |
| 115 | + ]); |
| 116 | + let parent = e.parentNode; |
| 117 | + expect(parent.childNodes[0].tagName).toBe('DIV'); |
| 118 | + expect(parent.childNodes[1].tagName).toBe('SPAN'); |
| 119 | + expect(parent.childNodes[2].tagName).toBe('P'); |
| 120 | + }); |
| 121 | + |
| 122 | + itRenders('an iterable', async render => { |
| 123 | + const threeDivIterable = { |
| 124 | + '@@iterator': function() { |
| 125 | + var i = 0; |
| 126 | + return { |
| 127 | + next: function() { |
| 128 | + if (i++ < 3) { |
| 129 | + return {value: <div key={i} />, done: false}; |
| 130 | + } else { |
| 131 | + return {value: undefined, done: true}; |
| 132 | + } |
| 133 | + }, |
| 134 | + }; |
| 135 | + }, |
| 136 | + }; |
| 137 | + let e = await render(threeDivIterable); |
| 138 | + let parent = e.parentNode; |
| 139 | + expect(parent.childNodes.length).toBe(3); |
| 140 | + expect(parent.childNodes[0].tagName).toBe('DIV'); |
| 141 | + expect(parent.childNodes[1].tagName).toBe('DIV'); |
| 142 | + expect(parent.childNodes[2].tagName).toBe('DIV'); |
| 143 | + }); |
| 144 | + |
| 145 | + itRenders('emptyish values', async render => { |
| 146 | + let e = await render(0); |
| 147 | + expect(e.nodeType).toBe(TEXT_NODE_TYPE); |
| 148 | + expect(e.nodeValue).toMatch('0'); |
| 149 | + |
| 150 | + // Empty string is special because client renders a node |
| 151 | + // but server returns empty HTML. So we compare parent text. |
| 152 | + expect((await render(<div>{''}</div>)).textContent).toBe(''); |
| 153 | + |
| 154 | + expect(await render([])).toBe(null); |
| 155 | + expect(await render(false)).toBe(null); |
| 156 | + expect(await render(true)).toBe(null); |
| 157 | + expect(await render(undefined)).toBe(null); |
| 158 | + expect(await render([[[false]], undefined])).toBe(null); |
| 159 | + }); |
| 160 | + }); |
| 161 | +}); |
0 commit comments