From face3d23c66bc55a8aabd5ac4e9484939df4edbc Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 16 Feb 2016 13:59:14 -0800 Subject: [PATCH] `ReactWrapper#{html, render}` should handle `null` children --- src/ReactWrapper.js | 8 ++++++-- test/ReactWrapper-spec.js | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/ReactWrapper.js b/src/ReactWrapper.js index 76a82c653..8e4857a33 100644 --- a/src/ReactWrapper.js +++ b/src/ReactWrapper.js @@ -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, ''); + }); } /** @@ -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(); } /** diff --git a/test/ReactWrapper-spec.js b/test/ReactWrapper-spec.js index 44050e8d1..7441339e6 100644 --- a/test/ReactWrapper-spec.js +++ b/test/ReactWrapper-spec.js @@ -1763,4 +1763,18 @@ describeWithDOM('mount', () => { }); }); + it('works with components that return null', () => { + class Foo extends React.Component { + render() { + return null; + } + } + const wrapper = mount(); + 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); + }); });