Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(events): Add dataTransfer event property option #554

Merged

Conversation

jamsinclair
Copy link
Contributor

What:
Resolves #551

Adds a convenience option to approximate the dataTransfer event property when firing events

Why:

Provides a consistent and convenient way to populate dataTransfer properties, much like populating target properties. This would help developers better simulate testing for drag and drop interactions.

There was previous discussion and feature validation over in testing-library/react-testing-library#339

How:

Update the fireEvent.* functions to populate the dataTransfer property for events only when defined in the eventProperties (second argument).

The property is assigned to the event object in a simple way. It does not construct the DataTransfer object and for good reasons. DataTransfer is privately handled and constructed by the browser and is not supported by jsdom.

MDN also warns:

it is not possible to create a useful DataTransfer object from script, since DataTransfer objects have a processing and security model that is coordinated by the browser during drag-and-drops.
https://developer.mozilla.org/en-US/docs/Web/API/DragEvent

Checklist:

@codesandbox-ci
Copy link

codesandbox-ci bot commented May 7, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 94d4eaa:

Sandbox Source
compassionate-visvesvaraya-fdvzm Configuration

@codecov
Copy link

codecov bot commented May 7, 2020

Codecov Report

Merging #554 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #554   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           23        23           
  Lines          454       459    +5     
  Branches       111       113    +2     
=========================================
+ Hits           454       459    +5     
Impacted Files Coverage Δ
src/events.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 752ff66...94d4eaa. Read the comment docs.

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Just one thing.

src/events.js Outdated
}

// Approximate dataTransfer on the event object
// jsdom does not support DataTransfer constructor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't only support JSDOM, so let's try the DataTransfer constructor first and if that's unsupported then we can do this workaround. Sound ok?

That being the case, we'll probably need to add /* istanbul ignore if */ or /* istanbul ignore else */ to maintain 100% code coverage.

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super 👍

src/events.js Show resolved Hide resolved
@kentcdodds kentcdodds merged commit 13bfce3 into testing-library:master May 7, 2020
@kentcdodds
Copy link
Member

@all-contributors please add @jamsinclair for code and tests

@allcontributors
Copy link
Contributor

@kentcdodds

I've put up a pull request to add @jamsinclair! 🎉

@kentcdodds
Copy link
Member

🎉 This PR is included in version 7.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

💡Add dataTransfer to fireEvent eventProperties
2 participants