Skip to content

Commit

Permalink
Merge pull request #196 from airbnb/ljharb/type_null
Browse files Browse the repository at this point in the history
Ensure that `ShallowWrapper` and `ReactWrapper` handle a null child correctly
  • Loading branch information
lelandrichardson committed Feb 18, 2016
2 parents 4746bc9 + 1e410ff commit aa947f8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
16 changes: 13 additions & 3 deletions docs/api/ShallowWrapper/type.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# `.type() => String|Function`
# `.type() => String|Function|null`

Returns the type of the current node of this wrapper. If it's a composite component, this will be
the component constructor. If it's native DOM node, it will be a string of the tag name.
the component constructor. If it's native DOM node, it will be a string of the tag name. If it's `null`, it will be `null`.

Note: can only be called on a wrapper of a single node.


#### Returns

`String|Function`: The type of the current node
`String|Function|null`: The type of the current node



Expand All @@ -23,3 +23,13 @@ expect(wrapper.type()).to.equal('div');
const wrapper = shallow(<Foo />);
expect(wrapper.type()).to.equal(Foo);
```

```jsx
class Null extends React.Component {
render() {
return null;
}
}
const wrapper = shallow(<Null />);
expect(wrapper.type()).to.equal(null);
```
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
4 changes: 2 additions & 2 deletions src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export default class ShallowWrapper {
* @returns {String}
*/
html() {
return this.single(renderToStaticMarkup);
return this.single(n => this.type() === null ? null : renderToStaticMarkup(n));
}

/**
Expand All @@ -321,7 +321,7 @@ export default class ShallowWrapper {
* @returns {CheerioWrapper}
*/
render() {
return cheerio.load(this.html()).root();
return this.type() === null ? cheerio() : cheerio.load(this.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);
});
});
14 changes: 14 additions & 0 deletions test/ShallowWrapper-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1670,4 +1670,18 @@ describe('shallow', () => {

});

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

0 comments on commit aa947f8

Please sign in to comment.