Replies: 1 comment
-
Setup functions keep the tests tidy. import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
type UI = ReturnType<typeof userEvent>['setup']
function setup(jsx) {
const user = userEvent.setup()
return {
user,
...render(jsx),
}
}
function fillPersonSection(this: UI, /*...*/) {
// ...
}
function setupFormXyz(jsx) {
const setup = setup(<MyForm>{jsx}</MyForm>)
return {
...setup,
fillPersonSection: fillPersonSection.bind(user),
}
}
test('fill form and confirm', async () => {
const { user, fillPersonSection } = setupFormXyz(<MyPersonInput/>)
await fillPersonSection(/* ... */)
await user.click(screen.getByRole('button', {name: 'Verify'}))
await waitFor(() => expect(screen.getByRole('button', {name: 'Confirm'})).toBeEnabled());
}) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This is a follow-up question to this one where @ph-fritsche provided a good explanation of when to use
user
object rather than directly callinguserEvent
. I am wondering how to approach the problem when there are many helper functions in the codebase that usesuserEvent
directly. Should we passuser
object every time we use such functions?It doesn't seem right as we are cluttering parameters. Is there a possibility to define this
user
object globally somehow?Beta Was this translation helpful? Give feedback.
All reactions