npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

drag-mock

v1.4.0

Published

Trigger HTML5 drag & drop events for testing

Downloads

3,445

Readme

drag-mock

Build Status npm version

Trigger HTML5 drag & drop events for testing

Requirements

No 3rd party libraries required. All you need is a plain website and an ES5 compatible browser (e.g. IE9+, Chrome, Firefox, ...).

Setup

Browser

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.dragStart(dragSource).drop(dropTarget);


// AMD
require(['dragMock'], function(dragMock) {
  var dragSource = document.querySelector('.draggable');
  var dropTarget = document.querySelector('.drop-zone');

  dragMock.dragStart(dragSource).drop(dropTarget);
});

Node / Webpack

Install using

npm install drag-mock --save-dev

and require it in your code

var dragMock = require('drag-mock');

Usage

var dragSource  = document.querySelector('.draggable');
var dropTarget  = document.querySelector('.drop-target');
var hoverRegion = document.querySelector('.hover-region');

dragMock
  .dragStart(dragSource)
  .dragEnter(hoverRegion)
  .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);
  });

Testing

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);
  });

});

Additional features

The following events are provided with a fake (but fully functional) dataTransfer object: drag, dragstart, dragover, dragend, dragleave, drop

webdriver.io integration

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. CSS selectors and XPath strings are both accepted as arguments.

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('//*[text()="Drop Zone"]', function(error) {
    if (error) {
      console.error(error);
    }
  });

Development

First run npm install to install all dependencies. gulp dist rebuilds the dist/ contents. gulp test (or npm test) runs the test suite. The default gulp task first runs dist, then test.

License

This software is licensed under the terms of the MIT license. See LICENSE for details.