File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -106,6 +106,10 @@ const Modal = React.createClass({
106106 } ;
107107 } ,
108108
109+ componentWillUnmount ( ) {
110+ events . off ( window , 'resize' , this . handleWindowResize ) ;
111+ } ,
112+
109113 render ( ) {
110114 let {
111115 className
Original file line number Diff line number Diff line change 1+ import events from 'dom-helpers/events' ;
12import React from 'react' ;
23import ReactTestUtils from 'react/lib/ReactTestUtils' ;
34import ReactDOM from 'react-dom' ;
@@ -175,4 +176,42 @@ describe('Modal', () => {
175176 , mountPoint ) ;
176177 } ) ;
177178
179+ describe ( 'cleanup' , ( ) => {
180+ let offSpy ;
181+
182+ beforeEach ( ( ) => {
183+ offSpy = sinon . spy ( events , 'off' ) ;
184+ } ) ;
185+
186+ afterEach ( ( ) => {
187+ events . off . restore ( ) ;
188+ } ) ;
189+
190+ it ( 'should remove resize listener when unmounted' , ( ) => {
191+ class Component extends React . Component {
192+ constructor ( props , context ) {
193+ super ( props , context ) ;
194+
195+ this . state = {
196+ show : true ,
197+ } ;
198+ }
199+
200+ render ( ) {
201+ if ( ! this . state . show ) {
202+ return null ;
203+ }
204+
205+ return (
206+ < Modal show > Foo</ Modal >
207+ ) ;
208+ }
209+ }
210+
211+ const instance = render ( < Component /> , mountPoint ) ;
212+ instance . setState ( { show : false } ) ;
213+
214+ expect ( offSpy ) . to . have . been . calledWith ( window , 'resize' ) ;
215+ } ) ;
216+ } ) ;
178217} ) ;
You can’t perform that action at this time.
0 commit comments