Skip to content

Commit

Permalink
Merge pull request #18 from trayio/rewrite
Browse files Browse the repository at this point in the history
Rewrite of all the functions, using Enzyme internal functions for better support/compatibility
  • Loading branch information
adriantoine authored Oct 3, 2016
2 parents 07c82c8 + 37a43e2 commit c40ce32
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 94 deletions.
8 changes: 3 additions & 5 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{
"plugins": [
"transform-es2015-modules-commonjs",
"transform-es2015-arrow-functions",
],
"presets": ["es2015"],
"plugins": ["syntax-export-extensions"],
"env": {
"test": {
"presets": ["react"]
"presets": ["es2015", "react"]
}
}
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"lodash.get": "^4.4.2",
"lodash.omit": "^4.5.0",
"lodash.pickby": "^4.6.0",
"object-values": "^1.0.0",
"react-display-name": "^0.2.0"
},
"peerDependencies": {
Expand All @@ -47,8 +48,8 @@
"devDependencies": {
"babel-cli": "^6.14.0",
"babel-jest": "^16.0.0",
"babel-plugin-transform-es2015-arrow-functions": "^6.8.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.14.0",
"babel-plugin-syntax-export-extensions": "^6.13.0",
"babel-preset-es2015": "^6.16.0",
"babel-preset-react": "^6.11.1",
"codecov": "^1.0.1",
"cz-conventional-changelog": "^1.2.0",
Expand Down
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';
52 changes: 52 additions & 0 deletions src/mount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import compact from 'lodash.compact';
import omit from 'lodash.omit';
import values from 'object-values';
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(...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 c40ce32

Please sign in to comment.