Skip to content

Commit

Permalink
ReactWrapper#{html, render} should handle null children
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 16, 2016
1 parent 45d9343 commit face3d2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/ReactWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,10 @@ export default class ReactWrapper {
* @returns {String}
*/
html() {
return this.single(n => findDOMNode(n).outerHTML.replace(/\sdata-reactid+="[^"]+"/g, ''));
return this.single(n => {
const node = findDOMNode(n);
return node === null ? null : node.outerHTML.replace(/\sdata-reactid+="[^"]+"/g, '');
});
}

/**
Expand All @@ -353,7 +356,8 @@ export default class ReactWrapper {
* @returns {CheerioWrapper}
*/
render() {
return cheerio.load(this.html()).root();
const html = this.html();
return html === null ? cheerio() : cheerio.load(html).root();
}

/**
Expand Down
14 changes: 14 additions & 0 deletions test/ReactWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1763,4 +1763,18 @@ describeWithDOM('mount', () => {
});
});

it('works with components that return null', () => {
class Foo extends React.Component {
render() {
return null;
}
}
const wrapper = mount(<Foo />);
expect(wrapper).to.have.length(1);
expect(wrapper.type()).to.equal(Foo);
expect(wrapper.html()).to.equal(null);
const rendered = wrapper.render();
expect(rendered.length).to.equal(0);
expect(rendered.html()).to.equal(null);
});
});

0 comments on commit face3d2

Please sign in to comment.