33
ReactDOMServer = require('react-dom/server');
34
});
35
36
- describe('with old implicit hydration API', () => {
37
- function expectDeprecationWarningWithFiber(callback) {
38
- expect(
39
- callback,
40
- ).toWarnDev(
41
- 'render(): Calling ReactDOM.render() to hydrate server-rendered markup ' +
42
- 'will stop working in React v18. Replace the ReactDOM.render() call ' +
43
- 'with ReactDOM.hydrate() if you want React to attach to the server HTML.',
44
- {withoutStack: true},
45
- );
46
- }
47
-
48
- it('should be able to adopt server markup', () => {
49
- class Root extends React.Component {
50
- render() {
51
- return (
52
- <html>
53
- <head>
54
- <title>Hello World</title>
55
- </head>
56
- <body>{'Hello ' + this.props.hello}</body>
57
- </html>
58
- );
59
- }
60
- }
61
-
62
- const markup = ReactDOMServer.renderToString(<Root hello="world" />);
63
- const testDocument = getTestDocument(markup);
64
- const body = testDocument.body;
65
-
66
- expectDeprecationWarningWithFiber(() =>
67
- ReactDOM.render(<Root hello="world" />, testDocument),
68
- );
69
- expect(testDocument.body.innerHTML).toBe('Hello world');
70
-
71
- ReactDOM.render(<Root hello="moon" />, testDocument);
72
- expect(testDocument.body.innerHTML).toBe('Hello moon');
73
-
74
- expect(body === testDocument.body).toBe(true);
75
- });
76
-
77
- it('should not be able to unmount component from document node', () => {
78
- class Root extends React.Component {
79
- render() {
80
- return (
81
- <html>
82
- <head>
83
- <title>Hello World</title>
84
- </head>
85
- <body>Hello world</body>
86
- </html>
87
- );
88
- }
89
- }
90
-
91
- const markup = ReactDOMServer.renderToString(<Root />);
92
- const testDocument = getTestDocument(markup);
93
- expectDeprecationWarningWithFiber(() =>
94
- ReactDOM.render(<Root />, testDocument),
95
- );
96
- expect(testDocument.body.innerHTML).toBe('Hello world');
97
-
98
- // In Fiber this actually works. It might not be a good idea though.
99
- ReactDOM.unmountComponentAtNode(testDocument);
100
- expect(testDocument.firstChild).toBe(null);
101
- });
102
-
103
- it('should not be able to switch root constructors', () => {
104
- class Component extends React.Component {
105
- render() {
106
- return (
107
- <html>
108
- <head>
109
- <title>Hello World</title>
110
- </head>
111
- <body>Hello world</body>
112
- </html>
113
- );
114
- }
115
- }
116
-
117
- class Component2 extends React.Component {
118
- render() {
119
- return (
120
- <html>
121
- <head>
122
- <title>Hello World</title>
123
- </head>
124
- <body>Goodbye world</body>
125
- </html>
126
- );
127
- }
128
- }
129
-
130
- const markup = ReactDOMServer.renderToString(<Component />);
131
- const testDocument = getTestDocument(markup);
132
-
133
- expectDeprecationWarningWithFiber(() =>
134
- ReactDOM.render(<Component />, testDocument),
135
- );
136
- expect(testDocument.body.innerHTML).toBe('Hello world');
137
-
138
- // This works but is probably a bad idea.
139
- ReactDOM.render(<Component2 />, testDocument);
140
-
141
- expect(testDocument.body.innerHTML).toBe('Goodbye world');
142
- });
143
-
144
- it('should be able to mount into document', () => {
145
- class Component extends React.Component {
146
- render() {
147
- return (
148
- <html>
149
- <head>
150
- <title>Hello World</title>
151
- </head>
152
- <body>{this.props.text}</body>
153
- </html>
154
- );
155
- }
156
- }
157
-
158
- const markup = ReactDOMServer.renderToString(
159
- <Component text="Hello world" />,
160
- );
161
- const testDocument = getTestDocument(markup);
162
-
163
- expectDeprecationWarningWithFiber(() =>
164
- ReactDOM.render(<Component text="Hello world" />, testDocument),
165
- );
166
-
167
- expect(testDocument.body.innerHTML).toBe('Hello world');
168
- });
169
-
170
- it('renders over an existing text child without throwing', () => {
171
- const container = document.createElement('div');
172
- container.textContent = 'potato';
173
- ReactDOM.render(<div>parsnip</div>, container);
174
- expect(container.textContent).toBe('parsnip');
175
- // We don't expect a warning about new hydration API here because
176
- // we aren't sure if the user meant to hydrate or replace a stub node.
177
- // We would see a warning if the container had React-rendered HTML in it.
178
- });
179
-
180
- it('should give helpful errors on state desync', () => {
181
- class Component extends React.Component {
182
- render() {
183
- return (
184
- <html>
185
- <head>
186
- <title>Hello World</title>
187
- </head>
188
- <body>{this.props.text}</body>
189
- </html>
190
- );
191
- }
192
- }
193
-
194
- const markup = ReactDOMServer.renderToString(
195
- <Component text="Goodbye world" />,
196
- );
197
- const testDocument = getTestDocument(markup);
198
-
199
- expect(() => {
200
- expect(() =>
201
- ReactDOM.render(<Component text="Hello world" />, testDocument),
202
- ).toWarnDev(
203
- 'render(): Calling ReactDOM.render() to hydrate server-rendered markup ' +
204
- 'will stop working in React v18. Replace the ReactDOM.render() call ' +
205
- 'with ReactDOM.hydrate() if you want React to attach to the server HTML.',
206
- {withoutStack: true},
207
- );
208
- }).toErrorDev('Warning: Text content did not match.');
209
- });
210
-
211
- it('should throw on full document render w/ no markup', () => {
212
- const testDocument = getTestDocument();
213
-
214
- class Component extends React.Component {
215
- render() {
216
- return (
217
- <html>
218
- <head>
219
- <title>Hello World</title>
220
- </head>
221
- <body>{this.props.text}</body>
222
- </html>
223
- );
224
- }
225
- }
226
-
227
- ReactDOM.render(<Component text="Hello world" />, testDocument);
228
- expect(testDocument.body.innerHTML).toBe('Hello world');
229
- // We don't expect a warning about new hydration API here because
230
- // we aren't sure if the user meant to hydrate or replace the document.
231
- // We would see a warning if the document had React-rendered HTML in it.
232
- });
233
-
234
- it('supports findDOMNode on full-page components', () => {
235
- const tree = (
236
- <html>
237
- <head>
238
- <title>Hello World</title>
239
- </head>
240
- <body>Hello world</body>
241
- </html>
242
- );
243
-
244
- const markup = ReactDOMServer.renderToString(tree);
245
- const testDocument = getTestDocument(markup);
246
- let component;
247
- expectDeprecationWarningWithFiber(() => {
248
- component = ReactDOM.render(tree, testDocument);
249
- });
250
- expect(testDocument.body.innerHTML).toBe('Hello world');
251
- expect(ReactDOM.findDOMNode(component).tagName).toBe('HTML');
252
- });
253
- });
254
-
36
describe('with new explicit hydration API', () => {
37
it('should be able to adopt server markup', () => {
38
class Root extends React.Component {