Skip to content

Commit

Permalink
ShallowWrapper#{html, render} should handle a null node.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 15, 2016
1 parent 68f1bf6 commit 45d9343
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 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);
```
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/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 45d9343

Please sign in to comment.