File tree Expand file tree Collapse file tree 5 files changed +76
-0
lines changed
Expand file tree Collapse file tree 5 files changed +76
-0
lines changed Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff line change @@ -88,6 +88,9 @@ Get a wrapper with the direct parent of the current node.
8888#### [ ` .closest(selector) => ReactWrapper ` ] ( ReactWrapper/closest.md )
8989Get 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 )
9295Returns a string representation of the text nodes in the current render tree.
9396
Original file line number Diff line number Diff line change 11import React from 'react' ;
2+ import cheerio from 'cheerio' ;
23import { flatten , unique , compact } from 'underscore' ;
34import createWrapperComponent from './ReactWrapperComponent' ;
45import {
@@ -304,6 +305,17 @@ export default class ReactWrapper {
304305 return this . single ( n => findDOMNode ( n ) . outerHTML . replace ( / \s d a t a - r e a c t i d + = " [ ^ " ] + " / 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.
Original file line number Diff line number Diff 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} ) ;
You can’t perform that action at this time.
0 commit comments