![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Trigger HTML5 drag & drop events for testing
No 3rd party libraries required. All you need is a plain website and an ES5 compatible browser (e.g. IE9+, Chrome, Firefox, ...).
Just add the library script to your page
<script src="http://andywer.github.io/drag-mock/drag-mock.min.js"></script>
You can then use it as a window global or as an AMD module
// plain javascript
var dragSource = document.querySelector('.draggable');
var dropTarget = document.querySelector('.drop-zone');
dragMock.drag(dragSource).drop(dropTarget);
// AMD
require(['dragMock'], function(dragMock) {
var dragSource = document.querySelector('.draggable');
var dropTarget = document.querySelector('.drop-zone');
dragMock.drag(dragSource).drop(dropTarget);
});
Install using
npm install drag-mock --save-dev
and require it in your code
var dragMock = require('drag-mock');
var dragSource = document.querySelector('.draggable');
var dropTarget = document.querySelector('.drop-target');
var hoverRegion = document.querySelector('.hover-region');
dragMock
.dragStart(dragSource)
.dragOver(hoverRegion)
.dragLeave(hoverRegion)
.delay(500)
.drop(dropTarget);
If you would like to set some properties on the event object you may pass an optional properties object as second parameter:
var dragSource = document.querySelector('.draggable');
var dropTarget = document.querySelector('.drop-target');
dragMock
.dragStart(dragSource, { clientX: 300, clientY: 400 })
.drop(dropTarget, { clientX: 200, clientY: 500 });
You can also customize the events by passing an optional callback function. The callback is called after creating the event, but before dispatching it. If your callback takes less than two arguments then it will be called once for the dragstart/drop event. It will be called for all events created if the callback takes two arguments:
dragMock
.dragStart(dragSource, function(event, eventName) {
// will be called for all created events (mousedown, dragstart), because the callback takes two arguments
dragStartEvent.dataTransfer.setData('application/json', { hello: 'world' });
})
.drop(dropTarget, function(dropEvent) {
// a callback with less than two parameters will only be called once for the primary ('drop') event
var data = dropEvent.dataTransfer.getData('application/json');
console.log('Hello ' + data.hello);
});
Use it with the testing framework of your choice.
describe('My fancy mail app', function() {
it('moves mails properly', function(done) {
var mail = document.querySelector('.mail[data-id=1234]');
var folder = document.querySelector('.folder[data-id=5678]');
dragMock.dragStart(mail).drop(folder);
var mailsInFolder = MailFolderService.getFolder(5678).getMailCount();
expect(mailsInFolder).to.equal(1);
});
});
The following events are provided with a fake (but fully functional) dataTransfer object:
drag
, dragstart
, dragover
, dragend
, dragleave
, drop
If you are running Selenium tests using webdriver.io and you need drag & drop functionality beyond Selenium's dragAndDrop() you can easily integrate drag-mock into webdriver:
var dragMock = require('drag-mock');
var webdriverio = require('webdriverio');
var webdriver = webdriverio.remote({ desiredCapabilities: { browserName: 'chrome' } }).init();
// set up webdriver.dragStart(), webdriver.dragOver(), webdriver.dragLeave() and webdriver.drop()
dragMock.extendWebdriver(webdriver);
// load the drag-mock library into the browser context
dragMock.loadLibrary(webdriver);
// drag and drop
webdriver
.dragStart('#my-drag-source', { clientX: 200, clientY: 300 })
.delay(250)
.drop('#drop-zone', function(error) {
if (error) {
console.error(error);
}
});
This software is licensed under the terms of the MIT license. See LICENSE for details.
FAQs
Trigger HTML5 drag & drop events for testing
The npm package drag-mock receives a total of 618 weekly downloads. As such, drag-mock popularity was classified as not popular.
We found that drag-mock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.