question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

šŸ›Passing options to a PointerEvent through createEvent doesn't work

See original GitHub issue

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 Library version: 7.5.1 (from yarn.lock, I’m using @testing-library/react)
  • node version: 12.16.1
  • yarn 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:closed
  • Created 3 years ago
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
itaylorcommented, Sep 11, 2020

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.

const pointerEventCtorProps = ['clientX', 'clientY', 'pointerType'];
export default class PointerEventFake extends Event {
  constructor(type, props) {
    super(type, props);
    pointerEventCtorProps.forEach((prop) => {
      if (props[prop] != null) {
        this[prop] = props[prop];
      }
    });
  }
}

Then in my test:

 window.PointerEvent = PointerEventFake;
 fireEvent.pointerDown(myElement, new PointerEventFake('pointerdown', {clientX: 20}));
 fireEvent.pointerMove(myElement, new PointerEventFake('pointermove', {clientX: 30}));

And they get fired and hit as I would expect in my React components.

1reaction
danny-does-stuffcommented, Jun 8, 2022

@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 to fireEvent.mouseDown(node, { clientX: 0 }), or use pointerDown with one of the options from the mdn docs, such as fireEvent.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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found