If you want to do it with HTML draggable elements, without jquery you need to mock the google chrome dataTransfer API with your own class, because google does not want you creating your own dataTransfer instance.
function simulateDragAndDrop(sourceNode, destinationNode) {
class MyDataTransfer {
constructor() {
this.dropEffect = "all";
this.effectAllowed = "all";
this.files = [];
this.items = new DataTransferItemList();
this.types = [];
}
setData(format, data) {
this.data[format] = data;
}
getData(format) {
return this.data[format];
}
clearData(format = null) {
if (format) {
delete this.data[format];
} else {
this.data = {};
}
}
clearData(type) {
if (type) {
const index = this.types.indexOf(type);
if (index !== -1) {
this.types.splice(index, 1);
}
} else {
this.types = [];
}
}
getData(type) {
const index = this.types.indexOf(type);
return index !== -1 ? this.items[index].data : '';
}
setData(type, data) {
const index = this.types.indexOf(type);
if (index !== -1) {
this.items[index].data = data;
} else {
this.types.push(type);
this.items.add(new DataTransferItem(type, data));
}
}
setDragImage(imageElement, x, y) {
// I do nothing here it's just to avoid errrs
}
}
class DataTransferItem {
constructor(type, data) {
this.type = type;
this.data = data;
}
}
class DataTransferItemList extends Array {
add(item) {
this.push(item);
}
}
const EVENT_TYPES = {
DRAG_END: 'dragend',
DRAG_START: 'dragstart',
DROP: 'drop'
}
const dataTransfer = new MyDataTransfer();
function createCustomEvent(type) {
const event = new CustomEvent(type, {
bubbles: true,
cancelable: true
});
event.dataTransfer = dataTransfer;
return event;
}
let events = [];
// let myDataTransfer = new MyDataTransfer();
events.push(createCustomEvent(EVENT_TYPES.DRAG_START));
events.push(createCustomEvent(EVENT_TYPES.DROP));
// Dispatch dragstart and dragend events on the sourceNode
events.forEach((event) => sourceNode.dispatchEvent(event));
const dropEvent = createCustomEvent(EVENT_TYPES.DROP);
destinationNode.dispatchEvent(dropEvent);
}