Do you write unit tests for UI components?

November 16, 2026 5 answers
Asked by p.593!hingequill

I can see the value of testing business logic and custom components, but I'm less sure about ordinary pages and third-party UI components. What kinds of UI behavior do you test, and at what level?

Answers

q__foldspindle.47

I'd test the behavior your application owns. For a stateful component, that might mean checking state transitions, conditional rendering, calculations, formatting, and whether an action triggers the right result. Basic accessibility checks can be useful too.

A test that only proves a library button displays the label passed to it usually isn't buying much. If you've copied or heavily modified a component, though, its behavior has become your responsibility and a regression test may make sense.

Reply to this answer
_t46+switchcrayon.3

Use a few levels. Unit test the complicated logic, component test the custom behavior, and keep a small set of browser smoke tests for critical user journeys. A login test that goes through the page and authentication system is closer to an end-to-end test than a unit test.

Reply to this answer
b.719~drainpocket

Permissions are a good reason to test the UI. Check that each role sees the actions it should and that the relevant flows work. But hiding a button isn't authorization, so also test protected API routes directly with roles that should and shouldn't have access.

_d47!ferrygauge.8:

That split helps. The UI check covers what the user is offered, while the API check covers whether the restriction is actually enforced.

Reply to this answer
6y~tramgauge_312

Visual tests can be worth considering when appearance matters. Screenshot comparisons let you review what changed visually, while large DOM snapshots can produce noisy failures that are easy to dismiss.

With mocked network responses, screenshots can cover different UI states without running every case through a full end-to-end flow. I'd still keep a few smoke tests for the real journey.

Reply to this answer
p.574+socketmixer

There's also a time budget to consider. A small team using mostly standard components might get more value from testing core logic and walking through the UI than from building a large component test suite. Focus automated coverage on the failures that would hurt the app most.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions