Skip to content

Commit 257e3cb

Browse files
authored
Warn about ReactDOM.createPortal usage within ReactTestRenderer (#12895)
1 parent 9ca5aee commit 257e3cb

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/ReactTestHostConfig.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @flow
88
*/
99

10+
import warning from 'shared/warning';
1011
import * as TestRendererScheduling from './ReactTestRendererScheduling';
1112

1213
export type Type = string;
@@ -62,6 +63,15 @@ export function appendChild(
6263
parentInstance: Instance | Container,
6364
child: Instance | TextInstance,
6465
): void {
66+
if (__DEV__) {
67+
warning(
68+
Array.isArray(parentInstance.children),
69+
'An invalid container has been provided. ' +
70+
'This may indicate that another render is being used in addition to the test renderer. ' +
71+
'(For example, ReactDOM.createPortal inside of a ReactTestRenderer tree.) ' +
72+
'This is not supported.',
73+
);
74+
}
6575
const index = parentInstance.children.indexOf(child);
6676
if (index !== -1) {
6777
parentInstance.children.splice(index, 1);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 ReactDOM = require('react-dom');
13+
14+
// Isolate test renderer.
15+
jest.resetModules();
16+
const ReactTestRenderer = require('react-test-renderer');
17+
18+
describe('ReactTestRenderer', () => {
19+
it('should warn if used to render a ReactDOM portal', () => {
20+
const container = document.createElement('div');
21+
expect(() => {
22+
expect(() => {
23+
ReactTestRenderer.create(ReactDOM.createPortal('foo', container));
24+
}).toThrow();
25+
}).toWarnDev('An invalid container has been provided.', {
26+
withoutStack: true,
27+
});
28+
});
29+
});

0 commit comments

Comments
 (0)