diff --git a/src/view/drag-drop-context/drag-drop-context.jsx b/src/view/drag-drop-context/drag-drop-context.jsx index 7eaab5a9cd..cae8f3fdbc 100644 --- a/src/view/drag-drop-context/drag-drop-context.jsx +++ b/src/view/drag-drop-context/drag-drop-context.jsx @@ -167,7 +167,7 @@ export default class DragDropContext extends React.Component { } componentWillUnmount() { - window.addEventListener('error', this.onWindowError); + window.removeEventListener('error', this.onWindowError); const state: State = this.store.getState(); if (state.phase !== 'IDLE') { diff --git a/src/view/drag-handle/util/create-post-drag-event-preventer.js b/src/view/drag-handle/util/create-post-drag-event-preventer.js index 4a7a293459..361f239751 100644 --- a/src/view/drag-handle/util/create-post-drag-event-preventer.js +++ b/src/view/drag-handle/util/create-post-drag-event-preventer.js @@ -1,6 +1,6 @@ // @flow /* eslint-disable no-use-before-define */ -import type { EventBinding } from './event-types'; +import type { EventBinding, EventOptions } from './event-types'; import { bindEvents, unbindEvents } from './bind-events'; type GetWindowFn = () => HTMLElement; @@ -10,6 +10,8 @@ export type EventPreventer = {| abort: () => void, |}; +const sharedOptions: EventOptions = { capture: true }; + export default (getWindow: GetWindowFn): EventPreventer => { let isBound: boolean = false; @@ -18,7 +20,7 @@ export default (getWindow: GetWindowFn): EventPreventer => { return; } isBound = true; - bindEvents(getWindow(), pointerEvents, { capture: true }); + bindEvents(getWindow(), pointerEvents, sharedOptions); }; const unbind = () => { @@ -26,7 +28,7 @@ export default (getWindow: GetWindowFn): EventPreventer => { return; } isBound = false; - unbindEvents(getWindow(), pointerEvents, { capture: true }); + unbindEvents(getWindow(), pointerEvents, sharedOptions); }; const pointerEvents: EventBinding[] = [ diff --git a/test/unit/view/drag-drop-context/lifecycle.spec.js b/test/unit/view/drag-drop-context/lifecycle.spec.js deleted file mode 100644 index 7cdd4cd9ee..0000000000 --- a/test/unit/view/drag-drop-context/lifecycle.spec.js +++ /dev/null @@ -1,15 +0,0 @@ -// @flow -import React from 'react'; -import { mount } from 'enzyme'; -import App from './app'; -import DragDropContext from '../../../../src/view/drag-drop-context'; - -it('should not throw when unmounting', () => { - const wrapper = mount( - {}}> - - , - ); - - expect(() => wrapper.unmount()).not.toThrow(); -}); diff --git a/test/unit/view/drag-drop-context/unmount.spec.js b/test/unit/view/drag-drop-context/unmount.spec.js new file mode 100644 index 0000000000..9da074c207 --- /dev/null +++ b/test/unit/view/drag-drop-context/unmount.spec.js @@ -0,0 +1,35 @@ +// @flow +import React from 'react'; +import { mount } from 'enzyme'; +import App from './app'; +import DragDropContext from '../../../../src/view/drag-drop-context'; + +it('should not throw when unmounting', () => { + const wrapper = mount( + {}}> + + , + ); + + expect(() => wrapper.unmount()).not.toThrow(); +}); + +it('should clean up any window event handlers', () => { + jest.spyOn(window, 'addEventListener'); + jest.spyOn(window, 'removeEventListener'); + + const wrapper = mount( + {}}> + + , + ); + + wrapper.unmount(); + + expect(window.addEventListener.mock.calls).toHaveLength( + window.removeEventListener.mock.calls.length, + ); + // validation + expect(window.addEventListener).toHaveBeenCalled(); + expect(window.removeEventListener).toHaveBeenCalled(); +});