Skip to content

Commit e776630

Browse files
Merge pull request #156 from airbnb/lmr--render-on-react-wrapper
Add render method to ReactWrapper
2 parents b4cc71d + ef7e1a8 commit e776630

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
* [parents()](/docs/api/ReactWrapper/parents.md)
6262
* [parent()](/docs/api/ReactWrapper/parent.md)
6363
* [closest(selector)](/docs/api/ReactWrapper/closest.md)
64+
* [render()](/docs/api/ReactWrapper/render.md)
6465
* [text()](/docs/api/ReactWrapper/text.md)
6566
* [html()](/docs/api/ReactWrapper/html.md)
6667
* [get(index)](/docs/api/ReactWrapper/get.md)

docs/api/ReactWrapper/render.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# `.render() => CheerioWrapper`
2+
3+
Returns a CheerioWrapper around the rendered HTML of the current node's subtree.
4+
5+
Note: can only be called on a wrapper of a single node.
6+
7+
8+
#### Returns
9+
10+
`String`: The resulting HTML string
11+
12+
13+
14+
#### Examples
15+
16+
```jsx
17+
class Foo extends React.Component {
18+
render() {
19+
return (<div className="in-foo" />);
20+
}
21+
}
22+
```
23+
24+
```jsx
25+
class Bar extends React.Component {
26+
render() {
27+
return (
28+
<div className="in-bar">
29+
<Foo />
30+
</div>
31+
);
32+
}
33+
}
34+
```
35+
36+
```jsx
37+
const wrapper = mount(<Bar />);
38+
expect(wrapper.find(Foo).render().find('.in-foo')).to.have.length(1);
39+
```

docs/api/mount.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ Get a wrapper with the direct parent of the current node.
8888
#### [`.closest(selector) => ReactWrapper`](ReactWrapper/closest.md)
8989
Get a wrapper with the first ancestor of the current node to match the provided selector.
9090

91+
#### [`.render() => CheerioWrapper`](ReactWrapper/render.md)
92+
Returns a CheerioWrapper of the current node's subtree.
93+
9194
#### [`.text() => String`](ReactWrapper/text.md)
9295
Returns a string representation of the text nodes in the current render tree.
9396

src/ReactWrapper.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import cheerio from 'cheerio';
23
import { flatten, unique, compact } from 'underscore';
34
import createWrapperComponent from './ReactWrapperComponent';
45
import {
@@ -304,6 +305,17 @@ export default class ReactWrapper {
304305
return this.single(n => findDOMNode(n).outerHTML.replace(/\sdata-reactid+="[^"]+"/g, ''));
305306
}
306307

308+
/**
309+
* Returns the current node rendered to HTML and wrapped in a CheerioWrapper.
310+
*
311+
* NOTE: can only be called on a wrapper of a single node.
312+
*
313+
* @returns {CheerioWrapper}
314+
*/
315+
render() {
316+
return cheerio.load(this.html()).root();
317+
}
318+
307319
/**
308320
* Used to simulate events. Pass an eventname and (optionally) event arguments. This method of
309321
* testing events should be met with some skepticism.

src/__tests__/ReactWrapper-spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,4 +1464,25 @@ describeWithDOM('mount', () => {
14641464
});
14651465
});
14661466

1467+
describe('.render()', () => {
1468+
it('should return a cheerio wrapper around the current node', () => {
1469+
class Foo extends React.Component {
1470+
render() {
1471+
return (<div className="in-foo" />);
1472+
}
1473+
}
1474+
class Bar extends React.Component {
1475+
render() {
1476+
return (
1477+
<div className="in-bar">
1478+
<Foo />
1479+
</div>
1480+
);
1481+
}
1482+
}
1483+
const wrapper = mount(<Bar />);
1484+
expect(wrapper.render().find('.in-foo')).to.have.length(1);
1485+
});
1486+
});
1487+
14671488
});

0 commit comments

Comments
 (0)