-
-
Notifications
You must be signed in to change notification settings - Fork 82
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
[test] Use vitest's userEvent
implementation in browser tests
#1314
base: master
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for base-ui ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
@@ -65,15 +65,15 @@ describe('<Accordion.Root />', () => { | |||
expect(trigger).to.have.attribute('aria-expanded', 'false'); | |||
expect(queryByText(PANEL_CONTENT_1)).to.equal(null); | |||
|
|||
await user.pointer({ keys: '[MouseLeft]', target: trigger }); | |||
await user.click(trigger); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Vitest doesn't have the pointer
method but clicks should work the same way in these cases
<Dialog.Trigger>Trigger 1</Dialog.Trigger> | ||
<Dialog.Portal> | ||
<Dialog.Popup data-testid="popup1"> | ||
<button onClick={() => setShowNested(!showNested)}>toggle</button> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Vitest functions are more strict about visibility and accessibility of the element to interact with.
const result = await originalRender(element, options); | ||
await flushMicrotasks(); | ||
return result; | ||
}); | ||
|
||
if (!isJSDOM) { | ||
const { userEvent: vitestUserEvent } = await import('@vitest/browser/context'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This must be imported dynamically as the static import fails when executed outside of the browser environment.
} | ||
|
||
return async (...args: unknown[]) => { | ||
await act(async () => target[prop](...args)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't ideal. Perhaps we could change the implementation of act
to no-op when running in the browser and get rid of this.
Vitest docs don't recommend using React Testing Library's
userEvent
in browser tests (see the warning box in https://vitest.dev/guide/browser/#examples).This PR changes the imported library depending on the environment in which the tests run.