Skip to content

Commit

Permalink
Revert "Hacky support for ReactDOM.createPortal() within ReactTestRen…
Browse files Browse the repository at this point in the history
…derer"

This reverts commit 4afbe18.
  • Loading branch information
Brian Vaughn committed May 29, 2018
1 parent 444619b commit db42945
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 59 deletions.
13 changes: 9 additions & 4 deletions packages/react-test-renderer/src/ReactTestHostConfig.js
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,10 +58,14 @@ export function appendChild(
parentInstance: Instance | Container,
child: Instance | TextInstance,
): void {
// Detect and ignore ReactDOM.createPortal() usage.
// Test renderer's toJSON() method knows how to handle this case.
if (parentInstance instanceof HTMLElement) {
return;
if (__DEV__) {
warning(
typeof 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) {
Expand Down
25 changes: 0 additions & 25 deletions packages/react-test-renderer/src/ReactTestRenderer.js
Expand Up @@ -11,8 +11,6 @@ import type {Fiber} from 'react-reconciler/src/ReactFiber';
import type {FiberRoot} from 'react-reconciler/src/ReactFiberRoot';
import type {Instance, TextInstance} from './ReactTestHostConfig';

import React from 'react';
import {isElement, isValidElementType, typeOf} from 'react-is';
import * as TestRenderer from 'react-reconciler/inline.test';
import {batchedUpdates} from 'events/ReactGenericBatching';
import {findCurrentFiberUsingSlowPath} from 'react-reconciler/reflection';
Expand Down Expand Up @@ -75,11 +73,6 @@ function toJSON(inst: Instance | TextInstance): ReactTestRendererNode {
let renderedChildren = null;
if (inst.children && inst.children.length) {
renderedChildren = inst.children.map(toJSON);
} else if (inst.props != null && inst.props.children != null) {
// TODO
renderedChildren = React.Children.toArray(inst.props.children).map(
toJSON,
);
}
const json: ReactTestRendererJSON = {
type: inst.type,
Expand All @@ -91,28 +84,10 @@ function toJSON(inst: Instance | TextInstance): ReactTestRendererNode {
});
return json;
default:
if (isElement(inst) || isValidElementType(inst)) {
return reactDOMElementToJSON(inst);
}
throw new Error(`Unexpected node type in toJSON: ${inst.tag}`);
}
}

function reactDOMElementToJSON(inst: any): ReactTestRendererNode {
if (typeof inst === 'object') {
const type = typeOf(inst);
return {
type: type == null ? 'unknown' : type.toString(),
props: inst.props || {},
children: React.Children.toArray(
inst.children || inst.props.children,
).map(reactDOMElementToJSON),
};
} else {
return inst;
}
}

function childrenToTree(node) {
if (!node) {
return null;
Expand Down
Expand Up @@ -9,19 +9,19 @@

'use strict';

const React = require('react');
const ReactDOM = require('react-dom');

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

describe('ReactTestRenderer', () => {
it('should support ReactDOM portal', () => {
it('should warn if used to render a ReactDOM portal', () => {
const container = document.createElement('div');
const rendered = ReactTestRenderer.create(
<div>{ReactDOM.createPortal(<span>Hi!</span>, container)}</div>,
);
expect(rendered.toJSON()).toMatchSnapshot();
expect(() => {
expect(() => {
ReactTestRenderer.create(ReactDOM.createPortal('foo', container));
}).toThrow();
}).toWarnDev('An invalid container has been provided.');
});
});

This file was deleted.

4 changes: 1 addition & 3 deletions packages/shared/isValidElementType.js
Expand Up @@ -12,7 +12,6 @@ import {
REACT_CONTEXT_TYPE,
REACT_FORWARD_REF_TYPE,
REACT_FRAGMENT_TYPE,
REACT_PORTAL_TYPE,
REACT_PROFILER_TYPE,
REACT_PROVIDER_TYPE,
REACT_STRICT_MODE_TYPE,
Expand All @@ -33,7 +32,6 @@ export default function isValidElementType(type: mixed) {
type !== null &&
(type.$$typeof === REACT_PROVIDER_TYPE ||
type.$$typeof === REACT_CONTEXT_TYPE ||
type.$$typeof === REACT_FORWARD_REF_TYPE ||
type.$$typeof === REACT_PORTAL_TYPE))
type.$$typeof === REACT_FORWARD_REF_TYPE))
);
}

0 comments on commit db42945

Please sign in to comment.