Skip to content

Commit f76d3bc

Browse files
Merge pull request FormidableLabs#692 from FormidableLabs/bug/issue-661-unmounting-bug
Bug/issue 661 unmounting bug
2 parents e767908 + 65522c6 commit f76d3bc

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/components/manager.js

+3
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ export class Manager extends Component {
213213

214214
componentWillUnmount() {
215215
this._detachEvents();
216+
if (this.autoplayInterval) {
217+
clearInterval(this.autoplayInterval);
218+
}
216219
}
217220

218221
_attachEvents() {

src/components/manager.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const _mockContext = function(slide, routeParams) {
1414
controls: {},
1515
progress: {
1616
pacman: []
17+
},
18+
autoplay: {
19+
pause: {}
1720
}
1821
},
1922
store: {
@@ -199,4 +202,22 @@ describe('<Manager />', () => {
199202
// slide 4 (index 3) the delta should be 3 - 1, thus 2.
200203
expect(managerInstance._getOffset(1)).toEqual(2);
201204
});
205+
206+
test('should cancel autoplay when component unmounts', () => {
207+
const setIntervalSpy = jest.spyOn(window, 'setInterval');
208+
const clearIntervalSpy = jest.spyOn(window, 'clearInterval');
209+
const wrapper = mount(
210+
<Manager autoplay autoplayOnStart>
211+
<MockSlide />
212+
<MockSlide />
213+
<MockSlide />
214+
</Manager>,
215+
{ context: _mockContext(1, []), childContextTypes: _mockChildContext() }
216+
);
217+
expect(setIntervalSpy.mock.calls.length).toBe(1);
218+
wrapper.unmount();
219+
// Called once in `Manager._startAutoplay`
220+
// and again in `Manager.componentWillUnmount`
221+
expect(clearIntervalSpy.mock.calls.length).toBe(2);
222+
});
202223
});

0 commit comments

Comments
 (0)