Skip to content

Commit

Permalink
fix: Fixed render with children (#45)
Browse files Browse the repository at this point in the history
* Fixed renderToJson when it's executed with a non-root node

* Removed debug file
  • Loading branch information
adriantoine authored Feb 9, 2017
1 parent 87e5613 commit 95001b0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
11 changes: 3 additions & 8 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,11 @@ rules:
prefer-spread: 2
max-len: [2, 260]
no-template-curly-in-string: 2
generator-star-spacing: [2, both]
object-shorthand: [2, always]
arrow-parens: [2, as-needed]

react/jsx-uses-react: 2
react/jsx-uses-vars: 2

# disable builtin rules that are incompatible with Babel plugin ones
generator-star-spacing: 0
object-shorthand: 0
arrow-parens: 0

babel/generator-star-spacing: [2, both]
babel/object-shorthand: [2, always]
babel/arrow-parens: [2, as-needed]
babel/no-await-in-loop: 2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
coverage
build
/serializer.js
npm-debug.log
6 changes: 4 additions & 2 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import compact from 'lodash.compact';
const renderChildToJson = child => {
if(!child) return null;

if(child.type === 'tag') {
if(child.type === 'root') {
return renderChildToJson(child.children[0]);
} else if(child.type === 'tag') {
return {
type: child.name,
props: child.attribs,
Expand All @@ -15,4 +17,4 @@ const renderChildToJson = child => {
}
};

export default wrapper => renderChildToJson(wrapper.children()[0]);
export default wrapper => renderChildToJson(wrapper[0]);
11 changes: 11 additions & 0 deletions tests/core/__snapshots__/render.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,15 @@ exports[`test converts basic pure render 1`] = `
</div>
`;

exports[`test renders the whole list 1`] = `
<ul>
<li>
0
</li>
<li>
1
</li>
</ul>
`;

exports[`test handles a component which returns null 1`] = `null`;
11 changes: 11 additions & 0 deletions tests/core/fixtures/pure-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,14 @@ export function BasicWithUndefined(props) {
<button disabled={props.disabled}>Hello</button>
);
}

export function BasicWithAList() {
return (
<div>
<ul>
<li>0</li>
<li>1</li>
</ul>
</div>
);
}
7 changes: 6 additions & 1 deletion tests/core/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React from 'react';
import { render } from 'enzyme';
import { renderToJson } from '../../src';
import { BasicPure } from './fixtures/pure-function';
import { BasicPure, BasicWithAList } from './fixtures/pure-function';
import { BasicClass, ClassWithNull } from './fixtures/class';

it('converts basic pure render', () => {
Expand All @@ -22,6 +22,11 @@ it('converts basic class render', () => {
expect(renderToJson(rendered)).toMatchSnapshot();
});

it('renders the whole list', () => {
const wrapper = render(<BasicWithAList />);
expect(renderToJson(wrapper.find('ul'))).toMatchSnapshot();
});

it('handles a component which returns null', () => {
const rendered = render(
<ClassWithNull />
Expand Down

0 comments on commit 95001b0

Please sign in to comment.