dom-testing-library: šPassing options to a PointerEvent through createEvent doesn't work
Hey, 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 Libraryversion: 7.5.1 (from yarn.lock, Iām using @testing-library/react)nodeversion: 12.16.1yarnversion: 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.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15 (3 by maintainers)
I was able to hack around this by adding my own implementation of
PointerEventthat 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
clientXIs 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 usepointerDownwith 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.Adding that to my fireEvent didnt work but adding your class to my setupTests.js file along with
worked a treat! Thank you!