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

userEvent.pointer does not set the button and buttons properties #926

Closed
alkstal opened this issue Apr 13, 2022 · 5 comments · Fixed by #1009
Closed

userEvent.pointer does not set the button and buttons properties #926

alkstal opened this issue Apr 13, 2022 · 5 comments · Fixed by #1009
Labels
environment:jsdom Issue with JSDOM environment released

Comments

@alkstal
Copy link

alkstal commented Apr 13, 2022

Reproduction example

https://codesandbox.io/s/misty-haze-n45bf4?file=/src/App.js

Prerequisites

  1. <button>Touch me</button>
document.querySelector('button').addEventListener('pointerup', (e) => {
    console.log(e.button)
})
await user.pointer({
    tags: '[MouseLeft]',
    target: document.querySelector('button'),
})

Expected behavior

PointerEvent sets event.button and event.buttons to allow us to figure out which button was used.

https://developer.mozilla.org/en-US/docs/Web/API/Pointer_events#determining_button_states

As an example, if the left mouse button was used, the pointerup event event.button is 0.

Actual behavior

The PointerEvent created by userEvent.pointer() does not assign event.button or event.buttons, so both are undefined in a event handler.

This means event handlers using these properties don't work under test.

User-event version

14.0.4

Environment

Testing Library framework:

JS framework:

Test environment:

DOM implementation:

Additional context

No response

@alkstal alkstal added bug Something isn't working needs assessment This needs to be looked at by a team member labels Apr 13, 2022
@ph-fritsche
Copy link
Member

Thanks for the report. ❤️

We add the button to the PointerEventInit here:

init.button = getMouseButton(button ?? 0)

This is an issue with the environment not implementing PointerEvent. This results in the event constructor being just Event which discards the properties from MouseEventInit/PointerEventInit.
https://github.com/testing-library/dom-testing-library/blob/11fc7731e5081fd0994bf2da84d688fdb592eda7/src/events.js#L50

We already apply a workaround for coordinates and other properties here and should maybe add button or (partially) replace it with the workaround below:

// Can not use instanceof, as MouseEvent might be polyfilled.
if (isMouseEvent(type) && init) {
// see https://github.com/testing-library/react-testing-library/issues/268
assignPositionInit(event as MouseEvent, init)
assignPointerInit(event as PointerEvent, init)
}

The following workaround fixes the issue:

if (!window.PointerEvent) {
  window.PointerEvent = window.MouseEvent
}

https://codesandbox.io/s/dreamy-firefly-xf3zke?file=/src/App.test.js:45-117

@ph-fritsche ph-fritsche added environment:jsdom Issue with JSDOM environment and removed bug Something isn't working needs assessment This needs to be looked at by a team member labels Apr 13, 2022
@alkstal
Copy link
Author

alkstal commented Apr 13, 2022

Thanks for the explanation and the suggested workaround!

@github-actions
Copy link

github-actions bot commented Aug 2, 2022

🎉 This issue has been resolved in version 14.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@ph-fritsche
Copy link
Member

@all-contributors add @alkstal bug

@allcontributors
Copy link
Contributor

@ph-fritsche

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
environment:jsdom Issue with JSDOM environment released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants