šPassing options to a PointerEvent through createEvent doesn't work
See original GitHub issueHey, Iām maintaining react-use-gesture and heavily rely on testing-library for tests (thanks so much btw).
Iām planning to move to PointerEvent instead of a mix of TouchEvent and MouseEvent for our drag recognizer. Our set of tests implies creating events that simulate pointer coordinates in the form of { clientX: number, clientY: number }
.
DOM Testing Library
version: 7.5.1 (from yarn.lock, Iām using @testing-library/react)node
version: 12.16.1yarn
version: 1.22.4
Relevant code or config:
// this works
const event = createEvent.mouseMove(element, { clientX: 30, clientY: 80 })
console.log(event.clientX) // <-- prints 30
// this doesn't work
const event = createEvent.pointerMove(element, { clientX: 30, clientY: 80 })
console.log(event.clientX) // <-- prints undefined
Iāll link to a repro as soon as codesandbox works properly.
Issue Analytics
- State:
- Created 3 years ago
- Comments:14 (3 by maintainers)
Top Results From Across the Web
Document.createEvent() - Web APIs | MDN
Creates an event of the type specified. The returned object should be first initialized and can then be passed to EventTarget.dispatchEvent.
Read more >How to simulate a mouse click using JavaScript?
Something like simulatedClick(document.getElementById('yourButtonId')) would work. You can pass in an object into options to override theĀ ...
Read more >SyntheticEvent - React
Your event handlers will be passed instances of SyntheticEvent ... These focus events work on all elements in the React DOM, not just...
Read more >Pointer Events - W3C
Publication as a Working Draft does not imply endorsement by W3C and its Members. This is a draft document and may be updated,...
Read more >HTML DOM Document createEvent() Method - W3Schools
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I was able to hack around this by adding my own implementation of
PointerEvent
that allows me to pass through the properties I care about for testing on the Event object.Then in my test:
And they get fired and hit as I would expect in my React components.
@hatpick what Iām saying is the options are passed to the the PointerEvent constructor, just that
clientX
Is not a valid option for PointerEvent, as you can see here https://developer.mozilla.org/en-US/docs/Web/API/PointerEvent/PointerEvent. Try switching tofireEvent.mouseDown(node, { clientX: 0 })
, or usepointerDown
with one of the options from the mdn docs, such asfireEvent.pointerDown(node, { pressure: 0 })
. Either way, you should see that those options are set on the event. This can also be confirmed by looking into the createEvent code, where you will see that the options are passed to the constructor for the Event or PointerEvent.