chakra-ui / zag

Finite state machines for building accessible design systems and UI components. Works with modern frameworks, and even just Vanilla JS
http://zagjs.com
MIT License
3.97k stars 160 forks source link

Menu trigger does not work in tests #1920

Open bohdanbirdie opened 2 hours ago

bohdanbirdie commented 2 hours ago

πŸ› Bug report

Originally coming from the use of ParkUI, but I narrowed down the bug to zag. When using Menu component and running tests on it, specifically calling userEvent.click() on trigger - preventDefault is causing the menu not to open.

Exact source: https://github.com/chakra-ui/zag/blob/a58f2d03735a6da3f61f3e8f14714f12d497e66a/packages/machines/menu/src/menu.connect.ts#L197

If I manually remove preventDetault() call in node_modules then it works all right.

As an experiment, I created a plain button in React and added onPointerDown and onClick events. When click is triggered with userEvent.click the onPointerDown is called first and then onClick after it. But preventing default forces onClick to never be called. I doubt that issue is on the side of react-testing-library as it works fine with plain HTML buttons. They simulate the click the same was it's done by user. If you add events onClick={console.log} onPointerDown={console.log} to the Menu.Trigger you will see that both events were triggered by manually clicking the button.

πŸ’₯ Steps to reproduce

  1. Get the reproduction project
  2. npm install
  3. npm run test
  4. Observe that tests failed as menu was not opened after a click
  5. npx patch-package to apply patch on the source code
  6. npm run test
  7. Observe that tests passed as now menu is opened

πŸ’» Link to reproduction

Whore repro project: https://github.com/bohdanbirdie/zag-events

Specific test: https://github.com/bohdanbirdie/zag-events/blob/main/src/App.test.tsx

🧐 Expected behavior

ProinterDown even is not prevented on default behavior.

🧭 Possible Solution

Remove prevent default call.

🌍 System information

zag-events@0.0.0 /Users/bohdanptyts/dev/repos/zag-events/zag-events β”œβ”€β”¬ @ark-ui/react@4.1.1 β”‚ └── @zag-js/menu@0.74.2 └─┬ @park-ui/panda-preset@0.42.0 └─┬ @ark-ui/anatomy@3.5.0 └── @zag-js/menu@0.62.1

Software Version(s)
Zag Version above
Browser Any, also JSDom
Operating System MacOS
bohdanbirdie commented 2 hours ago

Current workaround is to use fireEvent instead of userEvent as it directly triggers click event.