fix coverage for draggable utils

This commit is contained in:
SeinopSys 2022-04-03 02:13:15 +02:00 committed by Luna D
parent a9de6e2d66
commit dc2037ee75
No known key found for this signature in database
GPG key ID: 4B1C63448394F688
2 changed files with 32 additions and 2 deletions

View file

@ -1,4 +1,4 @@
import { initDraggables } from '../draggable';
import { clearDragSource, initDraggables } from '../draggable';
import { fireEvent } from '@testing-library/dom';
import { getRandomArrayItem } from '../../../test/randomness';
@ -115,6 +115,14 @@ describe('Draggable Utilities', () => {
expect(mockEvent.dataTransfer?.effectAllowed).toEqual('move');
});
it('should not throw if the event has no dataTransfer property', () => {
initDraggables();
const mockEvent = createDragEvent('dragstart');
delete (mockEvent as Record<keyof typeof mockEvent, unknown>).dataTransfer;
expect(() => fireEvent(mockDraggable, mockEvent)).not.toThrow();
});
});
describe('dragOver', () => {
@ -228,6 +236,20 @@ describe('Draggable Utilities', () => {
boundingBoxSpy.mockRestore();
}
});
it('should not throw if drag source element is not set', () => {
clearDragSource();
initDraggables();
const mockSecondDraggable = createDraggableElement();
mockDragContainer.appendChild(mockSecondDraggable);
const mockDropEvent = createDragEvent('drop');
fireEvent(mockDraggable, mockDropEvent);
expect(() => fireEvent(mockDraggable, mockDropEvent)).not.toThrow();
expect(mockDropEvent.defaultPrevented).toBe(true);
});
});
describe('dragEnd', () => {

View file

@ -53,7 +53,7 @@ function drop(event: DragEvent, target: HTMLElement) {
}
function dragEnd(event: DragEvent, target: HTMLElement) {
dragSrcEl?.classList.remove('dragging');
clearDragSource();
if (target.parentNode) {
$$('.over', target.parentNode).forEach(t => t.classList.remove('over'));
@ -78,3 +78,11 @@ export function initDraggables() {
document.addEventListener('dragend', wrapper(dragEnd));
document.addEventListener('drop', wrapper(drop));
}
export function clearDragSource() {
if (!dragSrcEl) return;
dragSrcEl.classList.remove('dragging');
// eslint-disable-next-line no-undefined
dragSrcEl = undefined;
}