Skip to content

Commit

Permalink
Warn about ReactDOM.createPortal usage within ReactTestRenderer (#12895)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn authored Aug 20, 2018
1 parent bf1abf4 commit d670bdc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/react-test-renderer/src/ReactTestHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @flow
*/

import warning from 'shared/warning';
import * as TestRendererScheduling from './ReactTestRendererScheduling';

export type Type = string;
Expand Down Expand Up @@ -62,6 +63,15 @@ export function appendChild(
parentInstance: Instance | Container,
child: Instance | TextInstance,
): void {
if (__DEV__) {
warning(
Array.isArray(parentInstance.children),
'An invalid container has been provided. ' +
'This may indicate that another render is being used in addition to the test renderer. ' +
'(For example, ReactDOM.createPortal inside of a ReactTestRenderer tree.) ' +
'This is not supported.',
);
}
const index = parentInstance.children.indexOf(child);
if (index !== -1) {
parentInstance.children.splice(index, 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* 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 ReactDOM = require('react-dom');

// Isolate test renderer.
jest.resetModules();
const ReactTestRenderer = require('react-test-renderer');

describe('ReactTestRenderer', () => {
it('should warn if used to render a ReactDOM portal', () => {
const container = document.createElement('div');
expect(() => {
expect(() => {
ReactTestRenderer.create(ReactDOM.createPortal('foo', container));
}).toThrow();
}).toWarnDev('An invalid container has been provided.', {
withoutStack: true,
});
});
});

0 comments on commit d670bdc

Please sign in to comment.