File tree Expand file tree Collapse file tree 5 files changed +107
-0
lines changed
Expand file tree Collapse file tree 5 files changed +107
-0
lines changed Original file line number Diff line number Diff line change 6060 * [ parent()] ( /docs/api/ReactWrapper/parent.md )
6161 * [ closest(selector)] ( /docs/api/ReactWrapper/closest.md )
6262 * [ text()] ( /docs/api/ReactWrapper/text.md )
63+ * [ html()] ( /docs/api/ReactWrapper/html.md )
6364 * [ get(index)] ( /docs/api/ReactWrapper/get.md )
6465 * [ at(index)] ( /docs/api/ReactWrapper/at.md )
6566 * [ first()] ( /docs/api/ReactWrapper/first.md )
Original file line number Diff line number Diff line change 1+ # ` .html() => String `
2+
3+ Returns a string of the rendered HTML markup of the current render tree.
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 .html ()).to .equal (
39+ ` <div class="in-bar"><div class="in-foo"></div></div>`
40+ );
41+ expect (wrapper .find (Foo).html ()).to .equal (
42+ ` <div class="in-foo"></div>`
43+ );
44+ ```
45+
46+ ``` jsx
47+ const wrapper = mount (< div>< b> important< / b>< / div> );
48+ expect (wrapper .html ()).to .equal (' <div><b>important</b></div>' );
49+ ```
50+
51+
52+ #### Related Methods
53+
54+ [ ` .text() => String ` ] ( text.md )
Original file line number Diff line number Diff line change @@ -91,6 +91,9 @@ Get a wrapper with the first ancestor of the current node to match the provided
9191#### [ ` .text() => String ` ] ( ReactWrapper/text.md )
9292Returns a string representation of the text nodes in the current render tree.
9393
94+ #### [ ` .html() => String ` ] ( ReactWrapper/html.md )
95+ Returns a static HTML rendering of the current node.
96+
9497#### [ ` .get(index) => ReactWrapper ` ] ( ReactWrapper/get.md )
9598Returns the node at the provided index of the current wrapper.
9699
Original file line number Diff line number Diff line change @@ -285,6 +285,17 @@ export default class ReactWrapper {
285285 return this . single ( n => findDOMNode ( n ) . textContent ) ;
286286 }
287287
288+ /**
289+ * Returns the HTML of the node.
290+ *
291+ * NOTE: can only be called on a wrapper of a single node.
292+ *
293+ * @returns {String }
294+ */
295+ html ( ) {
296+ return this . single ( n => findDOMNode ( n ) . outerHTML . replace ( / \s d a t a - r e a c t i d + = " [ ^ " ] + " / g, '' ) ) ;
297+ }
298+
288299 /**
289300 * Used to simulate events. Pass an eventname and (optionally) event arguments. This method of
290301 * testing events should be met with some skepticism.
Original file line number Diff line number Diff line change @@ -1300,4 +1300,42 @@ describeWithDOM('mount', () => {
13001300 expect ( wrapper . ref ( 'secondRef' ) . text ( ) ) . to . equal ( 'Second' ) ;
13011301 } ) ;
13021302 } ) ;
1303+
1304+ describe ( '.html()' , ( ) => {
1305+ it ( 'should return html of straight DOM elements' , ( ) => {
1306+ const wrapper = mount (
1307+ < div className = "test" >
1308+ < span > Hello World!</ span >
1309+ </ div >
1310+ ) ;
1311+ expect ( wrapper . html ( ) ) . to . equal (
1312+ `<div class="test"><span>Hello World!</span></div>`
1313+ ) ;
1314+ } ) ;
1315+
1316+ it ( 'should render out nested composite components' , ( ) => {
1317+ class Foo extends React . Component {
1318+ render ( ) {
1319+ return ( < div className = "in-foo" /> ) ;
1320+ }
1321+ }
1322+ class Bar extends React . Component {
1323+ render ( ) {
1324+ return (
1325+ < div className = "in-bar" >
1326+ < Foo />
1327+ </ div >
1328+ ) ;
1329+ }
1330+ }
1331+ const wrapper = mount ( < Bar /> ) ;
1332+ expect ( wrapper . html ( ) ) . to . equal (
1333+ `<div class="in-bar"><div class="in-foo"></div></div>`
1334+ ) ;
1335+ expect ( wrapper . find ( Foo ) . html ( ) ) . to . equal (
1336+ `<div class="in-foo"></div>`
1337+ ) ;
1338+ } ) ;
1339+ } ) ;
1340+
13031341} ) ;
You can’t perform that action at this time.
0 commit comments