Skip to content

Commit

Permalink
fix: renderToJson return null when a component returns null (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrottimark authored and adriantoine committed Jan 11, 2017
1 parent 8649858 commit 6aabd9e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/render.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import compact from 'lodash.compact';

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

if(child.type === 'tag') {
return {
Expand Down
2 changes: 2 additions & 0 deletions tests/core/__snapshots__/mount.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ exports[`test converts pure mount with mixed children 1`] = `
</BasicPure>
`;

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

exports[`test skips undefined props 1`] = `
<BasicWithUndefined>
<button>
Expand Down
2 changes: 2 additions & 0 deletions tests/core/__snapshots__/render.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ exports[`test converts basic pure render 1`] = `
</div>
</div>
`;

exports[`test handles a component which returns null 1`] = `null`;
10 changes: 9 additions & 1 deletion tests/core/mount.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React from 'react';
import { mount } from 'enzyme';
import { mountToJson } from '../../src';
import { BasicPure, BasicWithUndefined } from './fixtures/pure-function';
import { BasicClass, ClassWithPure, ClassWithDirectPure, ClassWithDirectComponent } from './fixtures/class';
import { BasicClass, ClassWithPure, ClassWithDirectPure, ClassWithDirectComponent, ClassWithNull } from './fixtures/class';

it('converts basic pure mount', () => {
const mounted = mount(
Expand Down Expand Up @@ -60,6 +60,14 @@ it('converts a class mount with a class component in it as a direct child', () =
expect(mountToJson(mounted)).toMatchSnapshot();
});

it('handles a component which returns null', () => {
const mounted = mount(
<ClassWithNull />
);

expect(mountToJson(mounted)).toMatchSnapshot();
});

it('skips undefined props', () => {
const mounted = mount(
<BasicWithUndefined>Hello!</BasicWithUndefined>
Expand Down
10 changes: 9 additions & 1 deletion tests/core/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React from 'react';
import { render } from 'enzyme';
import { renderToJson } from '../../src';
import { BasicPure } from './fixtures/pure-function';
import { BasicClass } from './fixtures/class';
import { BasicClass, ClassWithNull } from './fixtures/class';

it('converts basic pure render', () => {
const rendered = render(
Expand All @@ -21,3 +21,11 @@ it('converts basic class render', () => {

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

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

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

0 comments on commit 6aabd9e

Please sign in to comment.