mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-01-31 19:36:44 +01:00
fix coverage for draggable utils
This commit is contained in:
parent
a9de6e2d66
commit
dc2037ee75
2 changed files with 32 additions and 2 deletions
|
@ -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', () => {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue