Skip to content

Commit

Permalink
Warn about ReactDOM.createPortal usage within ReactTestRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed May 23, 2018
1 parent fe747a5 commit 13c6ac3
Show file tree
Hide file tree
Showing 2 changed files with 37 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 @@ -8,6 +8,7 @@
*/

import emptyObject from 'fbjs/lib/emptyObject';
import warning from 'fbjs/lib/warning';

import * as TestRendererScheduling from './ReactTestRendererScheduling';

Expand Down Expand Up @@ -57,6 +58,15 @@ export function appendChild(
parentInstance: Instance | Container,
child: Instance | TextInstance,
): void {
if (__DEV__) {
warning(
parentInstance.children.indexOf === 'function',
'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,27 @@
/**
* 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.');
});
});

0 comments on commit 13c6ac3

Please sign in to comment.