primitives: [Select] Unable to open select with @testing-library/react
Bug report
Current Behavior
The library cannot open the select when writing unit tests with @testing-library/react against the Select component.
This happens in all three cases:
- using 
fireEvent.clickon the trigger element, - using 
userEvent.clickon the trigger element - using 
userEvent.clickwith the pointer type set totouchon the trigger element (as per suggestion in https://github.com/radix-ui/primitives/issues/1220) 
Expected behavior
The Select component can be opened in jest unit tests.
Reproducible example
https://codesandbox.io/s/elated-gagarin-18cpyl?file=/select.test.jsx
Suggested solution
Additional context
When using fireEvent.click, the Select component simply doesn’t open, and the testing library can’t find any of the items.
In other cases, an exception is thrown: The provided value is not of type 'Element'..
Your environment
| Software | Name(s) | Version | 
|---|---|---|
| Radix Package(s) | react-select | 1.1.2 | 
| React | n/a | 18.2.0 | 
| Browser | n/a | n/a | 
| Assistive tech | n/a | n/a | 
| Node | n/a | |
| npm/yarn | ||
| Operating System | macOS | Monterey | 
About this issue
- Original URL
 - State: closed
 - Created 2 years ago
 - Reactions: 20
 - Comments: 16 (2 by maintainers)
 
Stand alone React App
I offer you my
jesttesting file for a custom component that incorporates theradix-uiSelectcomponentpackage.json excerpt
setupTests.ts
This file can be copy pasted into the test file if you are not using a test runner.
select-component.test.tsx
The issue for me it seems was that targeting the userEvent to a
getByTextwas being blocked by that element’spointer-events: noneCSS. So using agetByRoletarget seems to do the trick to get thecomboboxMy tests worked for a long time with the mock specified in this ticket: https://github.com/radix-ui/primitives/issues/1382#issuecomment-1122069313
Since the last version (1.1.2) and the introduction of this fix https://github.com/radix-ui/primitives/pull/1753, I had this error:
TypeError: target.hasPointerCapture is not a functionSo I fixed it by adding this line in the
setupTest.tsfile:So here is the complete test configuration to use the Select primitive with testing-library:
Here is the solution without any mocking:
Here is a setup with vitest and it seems to work. I hope it helps.
don’t work for me
this is my code
TypeError: target.hasPointerCapture is not a function. (In 'target.hasPointerCapture(event.pointerId)', 'target.hasPointerCapture' is undefined)Same, the solution doesn’t work for me either. Using vitest and happy-dom.
Solution provided: https://github.com/radix-ui/primitives/issues/1822#issuecomment-1820797668
FWIW, I was accidentally doing
userEvent.click()instead ofawait userEvent.click(). Awaiting fixed the problem.Same