diff --git a/package.json b/package.json index 5cedd6f..9f9ad08 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,8 @@ "dependencies": { "lodash.compact": "^3.0.1", "lodash.omit": "^4.5.0", - "object-values": "^1.0.0" + "object-values": "^1.0.0", + "object.entries": "^1.0.3" }, "peerDependencies": { "enzyme": "^2.4.1" diff --git a/src/shallow.js b/src/shallow.js index 51a82c7..2c60049 100644 --- a/src/shallow.js +++ b/src/shallow.js @@ -1,8 +1,10 @@ import compact from 'lodash.compact'; import omit from 'lodash.omit'; +import entries from 'object.entries'; import {propsOfNode} from 'enzyme/build/Utils'; import {typeName} from 'enzyme/build/Debug'; import {childrenOfNode} from 'enzyme/build/ShallowTraversal'; +import {isElement} from 'enzyme/build/react-compat'; function nodeToJson(node) { if(!node) return node; @@ -11,13 +13,28 @@ function nodeToJson(node) { return node; } + if (!isElement(node)) { + if (Array.isArray(node)) { + return node.map(nodeToJson); + } + + if (typeof node === 'object') { + return entries(node).reduce((obj, [key, val]) => { + obj[key] = nodeToJson(val); + return obj; + }, {}); + } + + return node; + } + const children = compact(childrenOfNode(node).map(n => nodeToJson(n))); const type = typeName(node); const props = omit(propsOfNode(node), 'children'); return { type, - props, + props: nodeToJson(props), children: children.length ? children : null, $$typeof: Symbol.for('react.test.json'), }; diff --git a/test/__snapshots__/shallow.test.js.snap b/test/__snapshots__/shallow.test.js.snap index a347903..42984b0 100644 --- a/test/__snapshots__/shallow.test.js.snap +++ b/test/__snapshots__/shallow.test.js.snap @@ -52,3 +52,47 @@ exports[`test converts basic pure shallow 1`] = ` `; exports[`test handles a component which returns null 1`] = `null`; + +exports[`test handles elements in prop arrays 1`] = ` + + + Hello! + + , + ] + } /> +`; + +exports[`test handles elements in prop objects 1`] = ` + + + Hello! + + , + "nestedElements": Array [ + + + Hello again! + + , + ], + } + } /> +`; + +exports[`test handles elements in props 1`] = ` + + + Hello! + + + } /> +`; diff --git a/test/shallow.test.js b/test/shallow.test.js index b151b41..13b7676 100644 --- a/test/shallow.test.js +++ b/test/shallow.test.js @@ -6,6 +6,10 @@ import { shallowToJson } from '../src'; import { BasicPure } from './fixtures/pure-function'; import { BasicClass, ClassWithPure, ClassWithNull } from './fixtures/class'; +function WrapperComponent(props) { + return ; +} + it('converts basic pure shallow', () => { const shallowed = shallow( Hello! @@ -35,3 +39,32 @@ it('handles a component which returns null', () => { ); expect(shallowToJson(shallowed)).toMatchSnapshot(); }); + +it('handles elements in props', () => { + const shallowed = shallow( + Hello!} /> + ); + expect(shallowToJson(shallowed)).toMatchSnapshot(); +}); + +it('handles elements in prop arrays', () => { + const shallowed = shallow( + Hello!, + ]} /> + ); + expect(shallowToJson(shallowed)).toMatchSnapshot(); +}); + +it('handles elements in prop objects', () => { + const shallowed = shallow( + Hello!, + nestedElements: [ + Hello again!, + ], + }} /> + ); + + expect(shallowToJson(shallowed)).toMatchSnapshot(); +});