Skip to content

Commit

Permalink
feat: allow multiple nodes to be rendered to JSON for shallow, mount …
Browse files Browse the repository at this point in the history
…and render (#49)
  • Loading branch information
bscharm authored and adriantoine committed Feb 22, 2017
1 parent 90e1717 commit 0ae7351
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"lodash.isnil": "^4.0.0",
"lodash.isplainobject": "^4.0.6",
"lodash.omitby": "^4.5.0",
"lodash.range": "^3.2.0",
"object-values": "^1.0.0",
"object.entries": "^1.0.3"
},
Expand Down
6 changes: 5 additions & 1 deletion src/mount.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@ function instToJson(inst) {
};
}

export default wrapper => instToJson(wrapper.node);
export default wrapper => {
return wrapper.length > 1
? wrapper.nodes.map(instToJson)
: instToJson(wrapper.node);
};
7 changes: 6 additions & 1 deletion src/render.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import range from 'lodash.range';
import {compact} from './utils';

const renderChildToJson = child => {
Expand All @@ -23,4 +24,8 @@ const renderChildToJson = child => {
}
};

export default wrapper => renderChildToJson(wrapper[0]);
export default wrapper => {
return wrapper.length > 1
? range(0, wrapper.length).map(node => renderChildToJson(wrapper[node]))
: renderChildToJson(wrapper[0])
}
6 changes: 5 additions & 1 deletion src/shallow.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ function nodeToJson(node) {
};
}

export default wrapper => nodeToJson(wrapper.node);
export default wrapper => {
return wrapper.length > 1
? wrapper.nodes.map(nodeToJson)
: nodeToJson(wrapper.node);
};
11 changes: 11 additions & 0 deletions tests/core/__snapshots__/mount.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ exports[`test converts pure mount with mixed children 1`] = `

exports[`test handles a component which returns null 1`] = `<ClassWithNull />`;

exports[`test renders multiple elements as a result of find 1`] = `
Array [
<li>
0
</li>,
<li>
1
</li>,
]
`;

exports[`test renders zero-children 1`] = `
<ComponentWithAZeroChildren>
<div>
Expand Down
15 changes: 13 additions & 2 deletions tests/core/__snapshots__/render.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ exports[`test converts basic pure render 1`] = `
</div>
`;

exports[`test handles a component which returns null 1`] = `null`;

exports[`test renders multiple elements as a result of find 1`] = `
Array [
<li>
0
</li>,
<li>
1
</li>,
]
`;

exports[`test renders the whole list 1`] = `
<ul>
<li>
Expand All @@ -42,5 +55,3 @@ exports[`test renders the whole list 1`] = `
</li>
</ul>
`;

exports[`test handles a component which returns null 1`] = `null`;
11 changes: 11 additions & 0 deletions tests/core/__snapshots__/shallow.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ exports[`test ignores non-plain objects 1`] = `
} />
`;

exports[`test renders multiple elements as a result of find 1`] = `
Array [
<li>
0
</li>,
<li>
1
</li>,
]
`;

exports[`test skips undefined props 1`] = `
<button>
Hello
Expand Down
7 changes: 6 additions & 1 deletion tests/core/mount.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React from 'react';
import {mount} from 'enzyme';
import {mountToJson} from '../../src';
import {BasicPure, BasicWithUndefined, ComponentWithAZeroChildren} from './fixtures/pure-function';
import {BasicPure, BasicWithUndefined, BasicWithAList, ComponentWithAZeroChildren} from './fixtures/pure-function';
import {
BasicClass,
ClassWithPure,
Expand Down Expand Up @@ -72,3 +72,8 @@ it('renders zero-children', () => {
const mounted = mount(<ComponentWithAZeroChildren />);
expect(mountToJson(mounted)).toMatchSnapshot();
});

it('renders multiple elements as a result of find', () => {
const mounted = mount(<BasicWithAList />);
expect(mountToJson(mounted.find('li'))).toMatchSnapshot();
});
5 changes: 5 additions & 0 deletions tests/core/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ it('handles a component which returns null', () => {

expect(renderToJson(rendered)).toMatchSnapshot();
});

it('renders multiple elements as a result of find', () => {
const rendered = render(<BasicWithAList />);
expect(renderToJson(rendered.find('li'))).toMatchSnapshot();
});
7 changes: 6 additions & 1 deletion tests/core/shallow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React from 'react';
import {shallow} from 'enzyme';
import {shallowToJson} from '../../src';
import {BasicPure, BasicWithUndefined} from './fixtures/pure-function';
import {BasicPure, BasicWithUndefined, BasicWithAList} from './fixtures/pure-function';
import {BasicClass, ClassWithPure, ClassWithNull} from './fixtures/class';

function WrapperComponent(props) {
Expand Down Expand Up @@ -76,3 +76,8 @@ it('skips undefined props', () => {

expect(shallowToJson(shallowed)).toMatchSnapshot();
});

it('renders multiple elements as a result of find', () => {
const shallowed = shallow(<BasicWithAList />);
expect(shallowToJson(shallowed.find('li'))).toMatchSnapshot();
});

0 comments on commit 0ae7351

Please sign in to comment.