fix(dragstart): cleanup DOM modifications when drag:start is canceled

This commit is contained in:
randym 2018-02-15 18:27:37 +09:00 committed by Max Hoffmann
parent 6efc520efb
commit 70a388938d
No known key found for this signature in database
GPG Key ID: 1DFA4D13DD27A676
3 changed files with 14 additions and 1 deletions

3
.gitignore vendored
View File

@ -18,3 +18,6 @@ example.html
coverage/
docs/
lib/
# 'cause we are all yarny and stuff
package-lock.json

View File

@ -438,6 +438,9 @@ export default class Draggable {
this.mirror.parentNode.removeChild(this.mirror);
}
this.source.parentNode.removeChild(this.source);
this.originalSource.style.display = null;
this.source.classList.remove(this.getClassNameFor('source:dragging'));
this.sourceContainer.classList.remove(this.getClassNameFor('container:dragging'));
document.body.classList.remove(this.getClassNameFor('body:dragging'));

View File

@ -418,16 +418,21 @@ describe('Draggable', () => {
.toBeInstanceOf(DragStartEvent);
});
test('sets dragging to false when `drag:start` event is canceled', () => {
test('cleans up when `drag:start` event is canceled', () => {
const newInstance = new Draggable(containers, {
draggable: 'li',
});
const draggableElement = sandbox.querySelector('li');
document.elementFromPoint = () => draggableElement;
let source;
let originalSource;
const callback = jest.fn((event) => {
source = event.source;
originalSource = event.originalSource;
event.cancel();
});
newInstance.on('drag:start', callback);
triggerEvent(draggableElement, 'mousedown', {button: 0});
@ -438,6 +443,8 @@ describe('Draggable', () => {
triggerEvent(draggableElement, 'dragstart', {button: 0});
expect(newInstance.dragging).toBeFalsy();
expect(source.parentNode).toBeNull();
expect(originalSource.style.display).toEqual('');
});
test('triggers `drag:move` drag event on mousedown', () => {