Skip to content

Commit fd8fc48

Browse files
committed
Merge pull request react-bootstrap#1579 from taion/Modal-cleanup
Ensure Modal cleanup
2 parents d809f3c + 3fbbbcc commit fd8fc48

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

src/Modal.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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

test/ModalSpec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import events from 'dom-helpers/events';
12
import React from 'react';
23
import ReactTestUtils from 'react/lib/ReactTestUtils';
34
import 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
});

0 commit comments

Comments
 (0)