testing-library / dom-testing-library

🐙 Simple and complete DOM testing utilities that encourage good testing practices.
https://testing-library.com/dom
MIT License
3.25k stars 460 forks source link

fireEvent doesn't work in pointer event #1291

Closed gwBear1 closed 3 months ago

gwBear1 commented 4 months ago

Relevant code or config:

None

What you did:

fireEvent does not fire the pointer event such as onpointerup, onpointerdown

What happened:

fireEvent does not fire the pointer event such as onpointerup, onpointerdown

Reproduction:

const tempPointer = document.createElement('div') tempPointer.onpointerup = () => { console.log('run2') } tempPointer.dispatchEvent(new PointerEvent('pointerup')) // 'run' will be shown

<img width="446" alt="스크린샷 2024-02-23 오후 5 16 39" src="https://github.com/testing-library/dom-testing-library/assets/138835573/2a8decfb-9d75-4cb6-8caf-6367b3d3205e">

- However in the jest environment
```javascript
const tempMouse = document.createElement('div')
tempMouse.onmouseup = () => {
  console.log('run1')
}
fireEvent.mouseUp(tempMouse);
// 'run' be shown.

const tempPointer = document.createElement('div')
tempPointer.onpointerup = () => {
  console.log('run2')
}
fireEvent.pointerUp(tempPointer);
// 'run' will not be shown.
스크린샷 2024-02-23 오후 5 18 04

Problem description:

Suggested solution:

Should work pointer event same as browser.

jlp-craigmorten commented 3 months ago

See also:

The issue is upstream in JSDom not supporting the event properly.

There is somewhat of a workaround in the comments https://github.com/jsdom/jsdom/issues/2527#issuecomment-1488182727 (though with likely caveats).

Alternative workarounds are around trying a different DOM implementation (e.g. happy-dom - haven't tested so might not be workable), or hoisting these tests to a browser environment such as using Playwright (of course, if applicable / practical for your use-case).

MatanBobi commented 3 months ago

Thanks for finding this one @jlp-craigmorten :) I'm closing this issue in favor of the JSDOM one.