File tree Expand file tree Collapse file tree 6 files changed +79
-6
lines changed Expand file tree Collapse file tree 6 files changed +79
-6
lines changed Original file line number Diff line number Diff line change 30
30
* [ closest(selector)] ( /docs/api/ShallowWrapper/closest.md )
31
31
* [ shallow()] ( /docs/api/ShallowWrapper/shallow.md )
32
32
* [ render()] ( /docs/api/ShallowWrapper/render.md )
33
+ * [ unmount()] ( /docs/api/ShallowWrapper/unmount.md )
33
34
* [ text()] ( /docs/api/ShallowWrapper/text.md )
34
35
* [ html()] ( /docs/api/ShallowWrapper/html.md )
35
36
* [ get(index)] ( /docs/api/ShallowWrapper/get.md )
Original file line number Diff line number Diff line change
1
+ # ` .unmount() => Self `
2
+
3
+ A method that unmounts the component. This can be used to simulate a component going through
4
+ an unmount/mount lifecycle.
5
+
6
+ #### Returns
7
+
8
+ ` ShallowWrapper ` : Returns itself.
9
+
10
+
11
+
12
+ #### Example
13
+
14
+ ``` jsx
15
+ const spy = sinon .spy ();
16
+
17
+ class Foo extends React .Component {
18
+ constructor (props ) {
19
+ super (props);
20
+ this .componentWillUnmount = spy;
21
+ }
22
+ render () {
23
+ return (
24
+ < div className= {this .props .id }>
25
+ {this .props .id }
26
+ < / div>
27
+ );
28
+ }
29
+ }
30
+ const wrapper = shallow (< Foo id= " foo" / > );
31
+ expect (spy .calledOnce ).to .equal (false );
32
+ wrapper .unmount ();
33
+ expect (spy .calledOnce ).to .equal (true );
34
+ ```
Original file line number Diff line number Diff line change @@ -103,6 +103,9 @@ Shallow renders the current node and returns a shallow wrapper around it.
103
103
#### [ ` .render() => CheerioWrapper ` ] ( ShallowWrapper/render.md )
104
104
Returns a CheerioWrapper of the current node's subtree.
105
105
106
+ #### [ ` .unmount() => ShallowWrapper ` ] ( ShallowWrapper/unmount.md )
107
+ A method that un-mounts the component.
108
+
106
109
#### [ ` .text() => String ` ] ( ShallowWrapper/text.md )
107
110
Returns a string representation of the text nodes in the current render tree.
108
111
Original file line number Diff line number Diff line change @@ -324,6 +324,16 @@ export default class ShallowWrapper {
324
324
return this . type ( ) === null ? cheerio ( ) : cheerio . load ( this . html ( ) ) . root ( ) ;
325
325
}
326
326
327
+ /**
328
+ * A method that unmounts the component. This can be used to simulate a component going through
329
+ * and unmount/mount lifecycle.
330
+ * @returns {ShallowWrapper }
331
+ */
332
+ unmount ( ) {
333
+ this . renderer . unmount ( ) ;
334
+ return this ;
335
+ }
336
+
327
337
/**
328
338
* Used to simulate events. Pass an eventname and (optionally) event arguments. This method of
329
339
* testing events should be met with some skepticism.
Original file line number Diff line number Diff line change 1
1
/* eslint react/no-deprecated: 0 */
2
2
import { REACT013 } from './version' ;
3
+ import objectAssign from 'object.assign' ;
3
4
4
5
let TestUtils ;
5
6
let createShallowRenderer ;
@@ -80,27 +81,27 @@ if (REACT013) {
80
81
// shallow rendering when it's just a DOM element.
81
82
createShallowRenderer = function createRendererCompatible ( ) {
82
83
const renderer = TestUtils . createRenderer ( ) ;
84
+ const originalRender = renderer . render ;
85
+ const originalRenderOutput = renderer . getRenderOutput ;
83
86
let isDOM = false ;
84
87
let _node ;
85
- return {
86
- _instance : renderer . _instance ,
88
+ return objectAssign ( renderer , {
87
89
render ( node , context ) {
88
90
if ( typeof node . type === 'string' ) {
89
91
isDOM = true ;
90
92
_node = node ;
91
93
} else {
92
94
isDOM = false ;
93
- renderer . render ( node , context ) ;
94
- this . _instance = renderer . _instance ;
95
+ return originalRender . call ( this , node , context ) ;
95
96
}
96
97
} ,
97
98
getRenderOutput ( ) {
98
99
if ( isDOM ) {
99
100
return _node ;
100
101
}
101
- return renderer . getRenderOutput ( ) ;
102
+ return originalRenderOutput . call ( this ) ;
102
103
} ,
103
- } ;
104
+ } ) ;
104
105
} ;
105
106
renderIntoDocument = TestUtils . renderIntoDocument ;
106
107
childrenToArray = React . Children . toArray ;
Original file line number Diff line number Diff line change @@ -1644,6 +1644,30 @@ describe('shallow', () => {
1644
1644
1645
1645
} ) ;
1646
1646
1647
+ describe ( '.unmount()' , ( ) => {
1648
+ it ( 'should call componentWillUnmount()' , ( ) => {
1649
+ const spy = sinon . spy ( ) ;
1650
+
1651
+ class Foo extends React . Component {
1652
+ constructor ( props ) {
1653
+ super ( props ) ;
1654
+ this . componentWillUnmount = spy ;
1655
+ }
1656
+ render ( ) {
1657
+ return (
1658
+ < div className = { this . props . id } >
1659
+ { this . props . id }
1660
+ </ div >
1661
+ ) ;
1662
+ }
1663
+ }
1664
+ const wrapper = shallow ( < Foo id = "foo" /> ) ;
1665
+ expect ( spy . calledOnce ) . to . equal ( false ) ;
1666
+ wrapper . unmount ( ) ;
1667
+ expect ( spy . calledOnce ) . to . equal ( true ) ;
1668
+ } ) ;
1669
+ } ) ;
1670
+
1647
1671
describe ( '.render()' , ( ) => {
1648
1672
1649
1673
it ( 'should return a cheerio wrapper around the current node' , ( ) => {
You can’t perform that action at this time.
0 commit comments