Skip to content

Commit 7d57c8a

Browse files
author
Brian Vaughn
committed
Warn about ReactDOM.createPortal usage within ReactTestRenderer
1 parent fe747a5 commit 7d57c8a

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

packages/react-test-renderer/src/ReactTestHostConfig.js

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

1010
import emptyObject from 'fbjs/lib/emptyObject';
11+
import warning from 'fbjs/lib/warning';
1112

1213
import * as TestRendererScheduling from './ReactTestRendererScheduling';
1314

@@ -57,6 +58,15 @@ export function appendChild(
5758
parentInstance: Instance | Container,
5859
child: Instance | TextInstance,
5960
): void {
61+
if (__DEV__) {
62+
warning(
63+
typeof parentInstance.children.indexOf === 'function',
64+
'An invalid container has been provided. ' +
65+
'This may indicate that another render is being used in addition to the test renderer. ' +
66+
'(For example, ReactDOM.createPortal inside of a ReactTestRenderer tree.) ' +
67+
'This is not supported.',
68+
);
69+
}
6070
const index = parentInstance.children.indexOf(child);
6171
if (index !== -1) {
6272
parentInstance.children.splice(index, 1);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
});
27+
});

0 commit comments

Comments
 (0)