Skip to content

Commit

Permalink
fix: Rewrite of all the functions, using Enzyme internal functions fo…
Browse files Browse the repository at this point in the history
…r better support/compatibi
  • Loading branch information
adriantoine committed Oct 3, 2016
1 parent 07c82c8 commit 0e06c67
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 87 deletions.
66 changes: 3 additions & 63 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,3 @@
import omit from 'lodash.omit';
import pickBy from 'lodash.pickby';
import compact from 'lodash.compact';
import get from 'lodash.get';
import getDisplayName from 'react-display-name';

export function shallowToJson(wrapper) {
const type = wrapper.type();
if (!type) {
return wrapper.node;
}

const children = compact(wrapper.children().map(c => shallowToJson(c)));
const props = pickBy(wrapper.props(), val => val !== undefined);

const json = {
type: wrapper.name(),
props: omit(props, 'children'),
children: (children.length) ? children : null,
$$typeof: Symbol.for('react.test.json'),
};

// If the type of element is not a function, it means that is a DOM element
if (typeof type !== 'function') {
return json;
}

// We need to get the rendered component in Enzyme internals
// because it won't be returned by `.children()`
const element = get(wrapper, 'node._reactInternalInstance._renderedComponent._currentElement');
if(!element) {
return json;
}

const jsonChildren = json.children;
json.children = [{
type: getDisplayName(element.type),
props: omit(element.props, 'children'),
children: jsonChildren,
$$typeof: Symbol.for('react.test.json'),
}];

return json;
}

export {shallowToJson as mountToJson};

const renderChildToJson = child => {
if(!child) return;

if(child.type === 'tag') {
return {
type: child.name,
props: child.attribs,
children: child.children && child.children.length ? compact(child.children.map(renderChildToJson)) : null,
$$typeof: Symbol.for('react.test.json'),
};
} else if(child.type === 'text') {
return child.data;
}
};
export const renderToJson = wrapper =>
renderChildToJson(wrapper.children()[0]);
export {mountToJson} from './mount';
export {shallowToJson} from './shallow';
export {renderToJson} from './render';
51 changes: 51 additions & 0 deletions src/mount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import compact from 'lodash.compact';
import omit from 'lodash.omit';
import {isDOMComponent, isElement} from 'enzyme/build/react-compat';
import {internalInstance, propsOfNode} from 'enzyme/build/Utils';
import {typeName} from 'enzyme/build/Debug';
import {childrenOfNode} from 'enzyme/build/ShallowTraversal';

function instToJson(inst) {
if (typeof inst === 'string' || typeof inst === 'number') return inst;
if (!inst) return '';

if (!inst.getPublicInstance) {
const internal = internalInstance(inst);
return instToJson(internal);
}
const publicInst = inst.getPublicInstance();

if (typeof publicInst === 'string' || typeof publicInst === 'number') return publicInst;
if (!publicInst && !inst._renderedComponent) return '';

const currentElement = inst._currentElement;
const type = typeName(currentElement);
const props = omit(propsOfNode(currentElement), 'children');
const children = [];
if (isDOMComponent(publicInst)) {
const renderedChildren = inst._renderedChildren;
if (!renderedChildren) {
children.push(...childrenOfNode(currentElement));
} else {
children.push(...Object.values(renderedChildren));
}
} else if (
isElement(currentElement) &&
typeof currentElement.type === 'function'
) {
children.push(inst._renderedComponent);
}

const childrenArray = compact(children.map(n => instToJson(n)));

return {
type,
props,
children: childrenArray.length ? childrenArray : null,
$$typeof: Symbol.for('react.test.json'),
};
}

export function mountToJson(wrapper) {
return instToJson(wrapper.node);
}
19 changes: 19 additions & 0 deletions src/render.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import compact from 'lodash.compact';

const renderChildToJson = child => {
if(!child) return;

if(child.type === 'tag') {
return {
type: child.name,
props: child.attribs,
children: child.children && child.children.length ? compact(child.children.map(renderChildToJson)) : null,
$$typeof: Symbol.for('react.test.json'),
};
} else if(child.type === 'text') {
return child.data;
}
};

export const renderToJson = wrapper =>
renderChildToJson(wrapper.children()[0]);
26 changes: 26 additions & 0 deletions src/shallow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import compact from 'lodash.compact';
import omit from 'lodash.omit';
import {propsOfNode} from 'enzyme/build/Utils';
import {typeName} from 'enzyme/build/Debug';
import {childrenOfNode} from 'enzyme/build/ShallowTraversal';

function nodeToJson(node) {
if (typeof node === 'string' || typeof node === 'number') {
return node;
}

const children = compact(childrenOfNode(node).map(n => nodeToJson(n)));
const type = typeName(node);
const props = omit(propsOfNode(node), 'children');

return {
type,
props,
children: children.length ? children : null,
$$typeof: Symbol.for('react.test.json'),
};
}

export function shallowToJson(wrapper) {
return nodeToJson(wrapper.node);
}
66 changes: 42 additions & 24 deletions test/__snapshots__/mount.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,36 @@ exports[`test converts a class mount with a class component in it as a direct ch
className="class">
<ClassWithPure
className="nested-pure">
<BasicPure
className="nested-pure">
<div
className="basic-pure nested-pure"
onClick={[Function]}>
<div
className="class-with-pure nested-pure"
onClick={[Function]}>
<BasicPure
className="nested-pure">
<div
className="group"
id="group-id">
<span>
className="basic-pure nested-pure"
onClick={[Function]}>
<div
className="group"
id="group-id">
<span>
<span>
<strong />
<span>
<strong>
Hello!
</strong>
</span>
<span
className="empty" />
</span>
<span
className="empty" />
</span>
<span
className="empty" />
</span>
<span
className="empty" />
</div>
</div>
</div>
</BasicPure>
</BasicPure>
</div>
</ClassWithPure>
</ClassWithDirectComponent>
`;
Expand All @@ -47,7 +53,9 @@ exports[`test converts a class mount with a pure function in it 1`] = `
id="group-id">
<span>
<span>
<strong />
<strong>
Hello!
</strong>
</span>
<span
className="empty" />
Expand All @@ -67,17 +75,23 @@ exports[`test converts a class mount with a pure function in it as a direct chil
<BasicPure
className="nested-pure">
<div
className="group"
id="group-id">
<span>
className="basic-pure nested-pure"
onClick={[Function]}>
<div
className="group"
id="group-id">
<span>
<strong />
<span>
<strong>
Hello!
</strong>
</span>
<span
className="empty" />
</span>
<span
className="empty" />
</span>
<span
className="empty" />
</div>
</div>
</BasicPure>
</ClassWithDirectPure>
Expand All @@ -93,7 +107,9 @@ exports[`test converts basic class mount 1`] = `
className="group"
id="group-id">
<span>
<strong />
<strong>
Hello!
</strong>
</span>
<span
className="empty" />
Expand All @@ -112,7 +128,9 @@ exports[`test converts basic pure mount 1`] = `
className="group"
id="group-id">
<span>
<strong />
<strong>
Hello!
</strong>
</span>
<span
className="empty" />
Expand Down

0 comments on commit 0e06c67

Please sign in to comment.