forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactLegacyMount-test.js
More file actions
434 lines (374 loc) · 14.5 KB
/
ReactLegacyMount-test.js
File metadata and controls
434 lines (374 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/**
* 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';
const {COMMENT_NODE} = require('react-dom-bindings/src/client/HTMLNodeType');
let React;
let ReactDOM;
let ReactDOMClient;
let waitForAll;
let assertConsoleErrorDev;
describe('ReactMount', () => {
beforeEach(() => {
jest.resetModules();
React = require('react');
ReactDOM = require('react-dom');
ReactDOMClient = require('react-dom/client');
const InternalTestUtils = require('internal-test-utils');
waitForAll = InternalTestUtils.waitForAll;
assertConsoleErrorDev = InternalTestUtils.assertConsoleErrorDev;
});
describe('unmountComponentAtNode', () => {
// @gate !disableLegacyMode
it('throws when given a non-node', () => {
const nodeArray = document.getElementsByTagName('div');
expect(() => {
ReactDOM.unmountComponentAtNode(nodeArray);
}).toThrowError('Target container is not a DOM element.');
});
// @gate !disableLegacyMode
it('returns false on non-React containers', () => {
const d = document.createElement('div');
d.innerHTML = '<b>hellooo</b>';
expect(ReactDOM.unmountComponentAtNode(d)).toBe(false);
expect(d.textContent).toBe('hellooo');
});
// @gate !disableLegacyMode
it('returns true on React containers', () => {
const d = document.createElement('div');
ReactDOM.render(<b>hellooo</b>, d);
expect(d.textContent).toBe('hellooo');
expect(ReactDOM.unmountComponentAtNode(d)).toBe(true);
expect(d.textContent).toBe('');
});
});
// @gate !disableLegacyMode
it('warns when given a factory', () => {
class Component extends React.Component {
render() {
return <div />;
}
}
const container = document.createElement('div');
ReactDOM.render(Component, container);
assertConsoleErrorDev([
'Functions are not valid as a React child. ' +
'This may happen if you return Component instead of <Component /> from render. ' +
'Or maybe you meant to call this function rather than return it.\n' +
' root.render(Component)',
]);
});
// @gate !disableLegacyMode
it('should render different components in same root', () => {
const container = document.createElement('container');
document.body.appendChild(container);
ReactDOM.render(<div />, container);
expect(container.firstChild.nodeName).toBe('DIV');
ReactDOM.render(<span />, container);
expect(container.firstChild.nodeName).toBe('SPAN');
});
// @gate !disableLegacyMode
it('should unmount and remount if the key changes', () => {
const container = document.createElement('container');
const mockMount = jest.fn();
const mockUnmount = jest.fn();
class Component extends React.Component {
componentDidMount = mockMount;
componentWillUnmount = mockUnmount;
render() {
return <span>{this.props.text}</span>;
}
}
expect(mockMount).toHaveBeenCalledTimes(0);
expect(mockUnmount).toHaveBeenCalledTimes(0);
ReactDOM.render(<Component text="orange" key="A" />, container);
expect(container.firstChild.innerHTML).toBe('orange');
expect(mockMount).toHaveBeenCalledTimes(1);
expect(mockUnmount).toHaveBeenCalledTimes(0);
// If we change the key, the component is unmounted and remounted
ReactDOM.render(<Component text="green" key="B" />, container);
expect(container.firstChild.innerHTML).toBe('green');
expect(mockMount).toHaveBeenCalledTimes(2);
expect(mockUnmount).toHaveBeenCalledTimes(1);
// But if we don't change the key, the component instance is reused
ReactDOM.render(<Component text="blue" key="B" />, container);
expect(container.firstChild.innerHTML).toBe('blue');
expect(mockMount).toHaveBeenCalledTimes(2);
expect(mockUnmount).toHaveBeenCalledTimes(1);
});
// @gate !disableLegacyMode
it('should reuse markup if rendering to the same target twice', () => {
const container = document.createElement('container');
const instance1 = ReactDOM.render(<div />, container);
const instance2 = ReactDOM.render(<div />, container);
expect(instance1 === instance2).toBe(true);
});
// @gate !disableLegacyMode
it('should not warn if mounting into non-empty node', () => {
const container = document.createElement('container');
container.innerHTML = '<div></div>';
ReactDOM.render(<div />, container);
});
// @gate !disableLegacyMode
it('should warn when mounting into document.body', () => {
const iFrame = document.createElement('iframe');
document.body.appendChild(iFrame);
// HostSingletons make the warning for document.body unnecessary
ReactDOM.render(<div />, iFrame.contentDocument.body);
});
// @gate !disableLegacyMode
it('should warn if render removes React-rendered children', () => {
const container = document.createElement('container');
class Component extends React.Component {
render() {
return (
<div>
<div />
</div>
);
}
}
ReactDOM.render(<Component />, container);
// Test that blasting away children throws a warning
const rootNode = container.firstChild;
ReactDOM.render(<span />, rootNode);
assertConsoleErrorDev([
'Replacing React-rendered children with a new ' +
'root component. If you intended to update the children of this node, ' +
'you should instead have the existing children update their state and ' +
'render the new components instead of calling ReactDOM.render.',
]);
});
// @gate !disableLegacyMode
it('should warn if the unmounted node was rendered by another copy of React', () => {
jest.resetModules();
const ReactDOMOther = require('react-dom');
const container = document.createElement('div');
class Component extends React.Component {
render() {
return (
<div>
<div />
</div>
);
}
}
ReactDOM.render(<Component />, container);
// Make sure ReactDOM and ReactDOMOther are different copies
expect(ReactDOM).not.toEqual(ReactDOMOther);
ReactDOMOther.unmountComponentAtNode(container);
assertConsoleErrorDev([
"unmountComponentAtNode(): The node you're attempting to unmount " +
'was rendered by another copy of React.',
]);
// Don't throw a warning if the correct React copy unmounts the node
ReactDOM.unmountComponentAtNode(container);
});
// @gate !disableLegacyMode
it('passes the correct callback context', () => {
const container = document.createElement('div');
let calls = 0;
ReactDOM.render(<div />, container, function () {
expect(this.nodeName).toBe('DIV');
calls++;
});
// Update, no type change
ReactDOM.render(<div />, container, function () {
expect(this.nodeName).toBe('DIV');
calls++;
});
// Update, type change
ReactDOM.render(<span />, container, function () {
expect(this.nodeName).toBe('SPAN');
calls++;
});
// Batched update, no type change
ReactDOM.unstable_batchedUpdates(function () {
ReactDOM.render(<span />, container, function () {
expect(this.nodeName).toBe('SPAN');
calls++;
});
});
// Batched update, type change
ReactDOM.unstable_batchedUpdates(function () {
ReactDOM.render(<article />, container, function () {
expect(this.nodeName).toBe('ARTICLE');
calls++;
});
});
expect(calls).toBe(5);
});
// @gate !disableLegacyMode && classic
it('initial mount of legacy root is sync inside batchedUpdates, as if it were wrapped in flushSync', () => {
const container1 = document.createElement('div');
const container2 = document.createElement('div');
class Foo extends React.Component {
state = {active: false};
componentDidMount() {
this.setState({active: true});
}
render() {
return (
<div>{this.props.children + (this.state.active ? '!' : '')}</div>
);
}
}
ReactDOM.render(<div>1</div>, container1);
ReactDOM.unstable_batchedUpdates(() => {
// Update. Does not flush yet.
ReactDOM.render(<div>2</div>, container1);
expect(container1.textContent).toEqual('1');
// Initial mount on another root. Should flush immediately.
ReactDOM.render(<Foo>a</Foo>, container2);
// The earlier update also flushed, since flushSync flushes all pending
// sync work across all roots.
expect(container1.textContent).toEqual('2');
// Layout updates are also flushed synchronously
expect(container2.textContent).toEqual('a!');
});
expect(container1.textContent).toEqual('2');
expect(container2.textContent).toEqual('a!');
});
describe('mount point is a comment node', () => {
let containerDiv;
let mountPoint;
beforeEach(() => {
containerDiv = document.createElement('div');
containerDiv.innerHTML = 'A<!-- react-mount-point-unstable -->B';
mountPoint = containerDiv.childNodes[1];
expect(mountPoint.nodeType).toBe(COMMENT_NODE);
});
// @gate !disableLegacyMode
it('renders at a comment node', () => {
function Char(props) {
return props.children;
}
function list(chars) {
return chars.split('').map(c => <Char key={c}>{c}</Char>);
}
ReactDOM.render(list('aeiou'), mountPoint);
expect(containerDiv.innerHTML).toBe(
'Aaeiou<!-- react-mount-point-unstable -->B',
);
ReactDOM.render(list('yea'), mountPoint);
expect(containerDiv.innerHTML).toBe(
'Ayea<!-- react-mount-point-unstable -->B',
);
ReactDOM.render(list(''), mountPoint);
expect(containerDiv.innerHTML).toBe(
'A<!-- react-mount-point-unstable -->B',
);
});
});
// @gate !disableLegacyMode
it('clears existing children with legacy API', async () => {
const container = document.createElement('div');
container.innerHTML = '<div>a</div><div>b</div>';
ReactDOM.render(
<div>
<span>c</span>
<span>d</span>
</div>,
container,
);
expect(container.textContent).toEqual('cd');
ReactDOM.render(
<div>
<span>d</span>
<span>c</span>
</div>,
container,
);
await waitForAll([]);
expect(container.textContent).toEqual('dc');
});
// @gate !disableLegacyMode
it('warns when rendering with legacy API into createRoot() container', async () => {
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
root.render(<div>Hi</div>);
await waitForAll([]);
expect(container.textContent).toEqual('Hi');
ReactDOM.render(<div>Bye</div>, container);
assertConsoleErrorDev([
// We care about this warning:
'You are calling ReactDOM.render() on a container that was previously ' +
'passed to ReactDOMClient.createRoot(). This is not supported. ' +
'Did you mean to call root.render(element)?',
// This is more of a symptom but restructuring the code to avoid it isn't worth it:
'Replacing React-rendered children with a new root component. ' +
'If you intended to update the children of this node, ' +
'you should instead have the existing children update their state ' +
'and render the new components instead of calling ReactDOM.render.',
]);
await waitForAll([]);
// This works now but we could disallow it:
expect(container.textContent).toEqual('Bye');
});
// @gate !disableLegacyMode
it('warns when unmounting with legacy API (no previous content)', async () => {
const container = document.createElement('div');
const root = ReactDOMClient.createRoot(container);
root.render(<div>Hi</div>);
await waitForAll([]);
expect(container.textContent).toEqual('Hi');
const unmounted = ReactDOM.unmountComponentAtNode(container);
assertConsoleErrorDev([
// We care about this warning:
'You are calling ReactDOM.unmountComponentAtNode() on a container that was previously ' +
'passed to ReactDOMClient.createRoot(). This is not supported. Did you mean to call root.unmount()?',
// This is more of a symptom but restructuring the code to avoid it isn't worth it:
'unmountComponentAtNode(): ' +
"The node you're attempting to unmount was rendered by React and is not a top-level container. " +
'Instead, have the parent component update its state and rerender in order to remove this component.',
]);
expect(unmounted).toBe(false);
await waitForAll([]);
expect(container.textContent).toEqual('Hi');
root.unmount();
await waitForAll([]);
expect(container.textContent).toEqual('');
});
// @gate !disableLegacyMode
it('warns when unmounting with legacy API (has previous content)', async () => {
const container = document.createElement('div');
// Currently createRoot().render() doesn't clear this.
container.appendChild(document.createElement('div'));
// The rest is the same as test above.
const root = ReactDOMClient.createRoot(container);
root.render(<div>Hi</div>);
await waitForAll([]);
expect(container.textContent).toEqual('Hi');
const unmounted = ReactDOM.unmountComponentAtNode(container);
assertConsoleErrorDev([
'You are calling ReactDOM.unmountComponentAtNode() on a container ' +
'that was previously passed to ReactDOMClient.createRoot(). ' +
'This is not supported. Did you mean to call root.unmount()?',
// This is more of a symptom but restructuring the code to avoid it isn't worth it:
'unmountComponentAtNode(): ' +
"The node you're attempting to unmount was rendered by React and is not a top-level container. " +
'Instead, have the parent component update its state and rerender in order to remove this component.',
]);
expect(unmounted).toBe(false);
await waitForAll([]);
expect(container.textContent).toEqual('Hi');
root.unmount();
await waitForAll([]);
expect(container.textContent).toEqual('');
});
// @gate !disableLegacyMode
it('warns when passing legacy container to createRoot()', () => {
const container = document.createElement('div');
ReactDOM.render(<div>Hi</div>, container);
ReactDOMClient.createRoot(container);
assertConsoleErrorDev([
'You are calling ReactDOMClient.createRoot() on a container that was previously ' +
'passed to ReactDOM.render(). This is not supported.',
]);
});
});