What happens differently between user.click and element.click?
#1278
|
I understand that the documented and recommended way seems to be the following: const user = userEvent.setup()
render(...);
await user.click(screen.getByRole('button'));I still have many tests written that way though: render(...);
screen.getByRole('button').click();But struggle to see any practical difference in my tests, and while the documentation covers well |
Replies: 1 comment 1 reply
|
element.click() fires one click event. user.click() is a pointer interaction: hover (unless skipHover), then pointerdown/mousedown, focus, pointerup/mouseup, then click. Native click skips two checks that matter for the disabled-button case:
jsdom's Use |
element.click() fires one click event. user.click() is a pointer interaction: hover (unless skipHover), then pointerdown/mousedown, focus, pointerup/mouseup, then click.
Native click skips two checks that matter for the disabled-button case:
pointer-events: nonethrows (assertPointerEvents).isDisabledin the mouse up path). The handler is not called.jsdom's
HTMLElement.click()often still dispatches click on a disabled button. A real user cannot do that, anduser.click()will not fire the callback.Use
user.click()for those tests. Keepelement.click()only when you want a single synthetic click with no pointer checks.