Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 17.4 — Simulating Users with user-event
React · Example 1 of 3

Testing a login form with type, selectOptions, click and Enter

Note the password input: it has no textbox role (screen readers don't expose password fields as text boxes), so the test finds it by its label. {Enter} inside type presses Enter, which submits the form.

user-event plays the full sequence of browser events a real person causes (pointer, focus, keys, input, click), so tests catch bugs that a single fireEvent would hide; always setup() it and await every action.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write a test (live helper style) for a Newsletter component: typing an email and pressing Enter calls onSubscribe with the email.

02

Write a test that checks a dialog's "Close" button gets focus first when the dialog opens, using user.tab().

Explain it without notes

01

Why does Testing Library recommend user-event over fireEvent?

02

Why must you await user-event calls, and why call userEvent.setup()?

03

How do you test keyboard navigation and focus with user-event?

04

When would you still use fireEvent?

Testing a login form with type, selectOptions, click and Enter

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

✓ the button is disabled until both fields are filled
✓ submits the typed values
✓ Enter in a field submits the form
✓ clear() empties a field