File tree Expand file tree Collapse file tree 2 files changed +54
-2
lines changed
Expand file tree Collapse file tree 2 files changed +54
-2
lines changed Original file line number Diff line number Diff line change @@ -74,6 +74,46 @@ describe('<State>', () => {
7474 } ) ;
7575
7676 expect ( foos ) . toEqual ( [ 'bar' , 'baz' ] ) ;
77- } )
77+ } ) ;
78+ } ) ;
79+
80+ describe ( 'life-cycle methods' , ( ) => {
81+ it ( 'fires onMount() with right arguments' , ( ) => {
82+ const onMount = jest . fn ( ) ;
83+ const onUnmount = jest . fn ( ) ;
84+
85+ const wrapper = mount ( h ( State , {
86+ init : {
87+ foo : 'bar'
88+ } ,
89+ onMount,
90+ onUnmount
91+ } ) ) ;
92+
93+ expect ( onMount ) . toHaveBeenCalledTimes ( 1 ) ;
94+ expect ( onMount ) . toHaveBeenCalledWith ( {
95+ foo : 'bar'
96+ } ) ;
97+
98+ expect ( onUnmount ) . not . toHaveBeenCalled ( ) ;
99+ } ) ;
100+
101+ it ( 'fires onMount() with right arguments' , ( ) => {
102+ const onUnmount = jest . fn ( ) ;
103+
104+ const wrapper = mount ( h ( State , {
105+ init : {
106+ foo : 'bar'
107+ } ,
108+ onUnmount
109+ } ) ) ;
110+
111+ wrapper . unmount ( ) ;
112+
113+ expect ( onUnmount ) . toHaveBeenCalledTimes ( 1 ) ;
114+ expect ( onUnmount ) . toHaveBeenCalledWith ( {
115+ foo : 'bar'
116+ } ) ;
117+ } ) ;
78118 } ) ;
79119} ) ;
Original file line number Diff line number Diff line change @@ -7,6 +7,8 @@ export interface IStateProps {
77 init : object ,
88 onChange ?: ( state : any ) => void ;
99 render ?: ( state : any , setState ?) => void ;
10+ onMount ?: ( state ) => void ;
11+ onUnmount ?: ( state ) => void ;
1012}
1113
1214export interface IStateState {
@@ -15,7 +17,9 @@ export interface IStateState {
1517
1618export class State extends Component < IStateProps , IStateState > {
1719 static defaultProps = {
18- onChange : noop
20+ onChange : noop ,
21+ onMount : noop ,
22+ onUnmount : noop
1923 } ;
2024
2125 state : IStateState ;
@@ -27,6 +31,14 @@ export class State extends Component<IStateProps, IStateState> {
2731 this . setState = this . setState . bind ( this ) ;
2832 }
2933
34+ componentDidMount ( ) {
35+ this . props . onMount ( this . state ) ;
36+ }
37+
38+ componentWillUnmount ( ) {
39+ this . props . onUnmount ( this . state ) ;
40+ }
41+
3042 render ( ) {
3143 return renderProp ( this . props , this . state , this . setState ) ;
3244 }
You can’t perform that action at this time.
0 commit comments