How can I blur an input field using @testing-library/user-event
? I know there’s fireEvent.blur
, but is there an alternative with userEvent
?
You can emulate the Tab
keypress to blur the currently focused input field. For example:
const input = await screen.findByLabelText(/username/i)
await userEvent.type(username, 'johndoe[Tab]')
This will type johndoe
in the input field and then hit the Tab button, making the field lose focus.