Skip to content

Commit

Permalink
Revert "Improved handling for mixed children case"
Browse files Browse the repository at this point in the history
This reverts commit c0d103c.
  • Loading branch information
Brian Vaughn committed May 29, 2018
1 parent 807e623 commit 963ed6b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 59 deletions.
5 changes: 0 additions & 5 deletions packages/react-test-renderer/src/ReactTestHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ export function removeChild(
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;
}
const index = parentInstance.children.indexOf(child);
parentInstance.children.splice(index, 1);
}
Expand Down
18 changes: 5 additions & 13 deletions packages/react-test-renderer/src/ReactTestRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,14 @@ function toJSON(inst: Instance | TextInstance): ReactTestRendererNode {
const {children, ...props} = inst.props;
/* eslint-enable */
let renderedChildren = null;
if (inst.props != null && inst.props.children != null) {
// At least some of this element's children are from another renderer
// (e.g. ReactDOM.createPortal)
// In this case, treat all children as potentially from the other renderer.
// If we don't, the JSON representation may contain overlaps.
if (inst.children && inst.children.length) {
renderedChildren = inst.children.map(toJSON);
} else if (inst.props != null && inst.props.children != null) {
// The element's children are from another renderer (e.g. ReactDOM.createPortal)
renderedChildren = React.Children.toArray(inst.props.children).map(
reactElementToJSON,
);
} else if (inst.children && inst.children.length) {
renderedChildren = inst.children.map(toJSON);
}

const json: ReactTestRendererJSON = {
type: inst.type,
props: props,
Expand All @@ -101,14 +97,10 @@ function toJSON(inst: Instance | TextInstance): ReactTestRendererNode {

function reactElementToJSON(inst: any): ReactTestRendererNode {
if (typeof inst === 'object') {
/* eslint-disable no-unused-vars */
// We don't include the `children` prop in JSON.
// Instead, we will include the actual rendered children.
const {children, ...props} = inst.props != null ? inst.props : {};
const type = typeOf(inst);
return {
type: type == null ? 'unknown' : type.toString(),
props,
props: inst.props || {},
children: React.Children.toArray(
inst.children || inst.props.children,
).map(reactElementToJSON),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,10 @@ jest.resetModules();
const ReactTestRenderer = require('react-test-renderer');

describe('ReactTestRenderer', () => {
it('should support ReactDOM portal usage', () => {
it('should support ReactDOM portal', () => {
const container = document.createElement('div');
let rendered = ReactTestRenderer.create(
<div>
{ReactDOM.createPortal(<span>Rendered by ReactDOM</span>, container)}
</div>,
);
expect(rendered.toJSON()).toMatchSnapshot();

rendered.update(
<div>
<span>Rendered by ReactTestRenderer</span>
{ReactDOM.createPortal(<span>Rendered by ReactDOM</span>, container)}
</div>,
const rendered = ReactTestRenderer.create(
<div>{ReactDOM.createPortal(<span>Hi!</span>, container)}</div>,
);
expect(rendered.toJSON()).toMatchSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ReactTestRenderer should support ReactDOM portal usage 1`] = `
exports[`ReactTestRenderer should support ReactDOM portal 1`] = `
<div>
Object {
"children": Array [
Object {
"children": Array [
"Rendered by ReactDOM",
"Hi!",
],
"props": Object {},
"type": "Symbol(react.element)",
},
],
"props": Object {},
"type": "Symbol(react.portal)",
}
</div>
`;

exports[`ReactTestRenderer should support ReactDOM portal usage 2`] = `
<div>
Object {
"children": Array [
"Rendered by ReactTestRenderer",
],
"props": Object {},
"type": "Symbol(react.element)",
}
Object {
"children": Array [
Object {
"children": Array [
"Rendered by ReactDOM",
],
"props": Object {},
"props": Object {
"children": "Hi!",
},
"type": "Symbol(react.element)",
},
],
Expand Down

0 comments on commit 963ed6b

Please sign in to comment.