Warn about ReactDOM.createPortal usage within ReactTestRenderer (#12895)
Brian Vaughn committed
Aug 20, 2018 at 10:03 UTC
d670bdc6b1ed4fc8c0c935aa26a2d448c969e0e9
2 files changed
+39
packages/react-test-renderer/src/ReactTestHostConfig.js
+10
@@ -7,6 +7,7 @@
7
* @flow
8
*/
9
10
+import warning from 'shared/warning';
11
import * as TestRendererScheduling from './ReactTestRendererScheduling';
12
13
export type Type = string;
@@ -62,6 +63,15 @@ export function appendChild(
63
parentInstance: Instance | Container,
64
child: Instance | TextInstance,
65
): 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
+ }
75
const index = parentInstance.children.indexOf(child);
76
if (index !== -1) {
77
parentInstance.children.splice(index, 1);
packages/react-test-renderer/src/__tests__/ReactTestRenderer-test.js
new
+29
@@ -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
+});