# Unit Testing: Automated Checks That Prove Each Component Still Works

> Learn how small automated checks confirm that one design system component, such as a Button, looks and behaves as its spec says in every state.

**In short:** Unit testing for a design system uses small automated checks to confirm that one component, such as a Button, looks and behaves as its spec says in every state.

From "Design systems", a free course by Venkat R. Murty: https://designervenkat.online/learn/design-systems/unit-testing

## What You Will Learn

A design system is a shared set of parts, such as Buttons, text fields, and badges, that many product teams reuse. When one shared part changes, every screen that uses it can change too. Unit testing is a way to catch a broken part before it reaches those teams.

In this lesson you will learn what a unit test checks for one component, how to write a basic one in clear steps, and what makes a set of tests useful for a design system. Here, a unit is one small piece tested by itself, not a whole page.

- **Unit test:** An automated check that tests one small part, such as one Button, all by itself.
- **Component:** A reusable building block, such as a Button or a Dropdown, that teams place into their screens.
- **Props:** Settings you pass into a component to change how it looks or acts, such as turning on its disabled setting.
- **State:** The current condition of a component, such as default, disabled, loading, or error.
- **Variant:** A named version of a component, such as a primary Button or a secondary Button.
- **Render:** To show the component on screen so its output can be checked.
- **Assert:** To check that a result matches what the spec expects. A test passes or fails based on its checks.
- **Regression:** A bug that appears in something that used to work, often after a change made somewhere else.
- **Snapshot:** A saved copy of a component's output that later test runs compare against.
- **Test suite:** The full set of tests a team keeps for its design system.

## What does a unit test check?

Many teams start their automated testing here, because a unit test looks at one small piece, such as a single component, and is the simplest kind of automated check to set up.

For a UI component, a unit test usually asks three questions. Does the component show the right output for the settings it was given? Does it react correctly when a user clicks or presses a key? Does it handle unusual cases? A test can confirm that the right text appears, that clicking works, and that every supported setting gives a predictable result.

- **Expected output:** For a given setting or state, the component shows what the spec says, such as the right label, icon, or color.
- **Correct reactions:** When a user clicks or presses a key, the component does its job. It also does nothing it should not do.
- **Unusual cases:** Teams also ask what happens when a user clicks things out of order, or when a request to a server fails.

### How do you test each state?

A design system component often comes in several states.

- **Default:** The normal look and behavior when no special setting is turned on.
- **Disabled:** The control is switched off. Clicks should do nothing, and users should be able to see that it is off.
- **Loading:** The control is busy, for example while an action is still running. The spec should say whether clicks are blocked.
- **Error:** Something went wrong. The spec should say what the component shows and whether it still accepts input.

Each state needs its own check. A passing test for the default state says nothing about the disabled state. A disabled Button often needs both a visual check and a separate unit test for that same state.

## How do you write a basic component test?

Most unit tests follow the same three-part pattern. First you set up the situation, then you act, then you check the result. Testers often call these arrange, act, and assert.

1. **Set up:** Place the component into the test with the settings you want to check, such as disabled turned on. Write down what the spec says should happen before you start.
2. **Simulate the user:** Act the way a person would. Click the Button, press a key, or type into a field. Testing tools can send these actions for you.
3. **Assert the result:** Check what changed. Is the Button marked as disabled? Did the click action run the number of times the spec allows? Do not check things the spec does not mention.
4. **Repeat for each state:** Change one setting, such as loading, and run the same three parts again. Keep each test focused on one state.

Keep each test independent. A test should not depend on what another test did first, so that any single test can fail or pass on its own.

### Should you test behavior or implementation?

Good tests check behavior, which means what users see and do. Weak tests check implementation details, which means the internal code that builds the component. The difference matters, because internal code gets reorganized often.

- **Behavior (test this):** The label is visible, the Button is marked as disabled, and a click does nothing while it is disabled.
- **Implementation (avoid this):** Internal variable values, the names of internal helpers, or a particular code class that a builder picked.
- **Why it matters:** A test that checks internals can break when someone tidies the code, even if no user sees a change. A test of behavior keeps working through that kind of cleanup.

> **Tip:** If a test fails only because someone renamed something no user can see, the test was checking implementation. Rewrite it to check what the user sees and does.

## Worked Example: Testing a Button in Two States

Here is a real-style check for a design system Button. This example assumes the spec says that a disabled Button ignores clicks and that a loading Button also ignores clicks.

1. **Set up the disabled case:** Place the Button on the test stage with disabled turned on. Attach a click action that counts how many times it runs, starting at zero.
2. **Simulate a click:** Click the Button the way a user would. A disabled Button should not respond.
3. **Assert the disabled result:** The counter should still say zero, and the Button should be marked as disabled so the state can be seen.
4. **Repeat for loading:** Place the Button with loading turned on and the counter back at zero. Simulate the same click.
5. **Assert the loading result:** The counter should stay at zero, and the Button should be marked as disabled, as the spec says.
6. **Add a control check:** Place the Button with both settings off, click it once, and confirm the counter goes to one.

Notice that the test checks what a user can observe: whether the click ran, and whether the Button looks off. It does not check how the Button's internal code decides to block the click.

> **Tip:** Write the expected result from the spec first, then the test. If the spec is silent about loading, ask the design owner before writing the check.

## What makes a design system test suite useful?

One test for one Button is a start. A useful test suite covers the whole component and runs without anyone having to remember it.

- **Every variant and state:** Cover each named version and each state in the spec, not only the most common one. Tests should also check borderline cases, such as the largest allowed text.
- **Runs on every change:** Tests should run automatically whenever someone changes a component. Many teams connect this to their build process, often called CI, short for continuous integration.
- **Catches regressions early:** A regression is a bug in something that used to work. Catching it before a component is released helps keep the broken version from reaching product teams.
- **Records intent:** Good tests also act as notes. They tell the next designer or builder what the component is supposed to do.

> **Tip:** Name each test after the behavior it protects, such as 'disabled Button ignores clicks'. When a test fails, the name tells the team what broke.

## Snapshot and Visual Tests, and Their Limits

Many teams also use snapshot tests and visual tests. A snapshot test saves a copy of the component's output and flags any later difference. It checks that the output stayed the same, not that the output is correct. A visual test compares screenshots of the component as it appears in a browser.

- **Snapshots are quick:** They are quick to write, which also makes them easy to overuse. Keep them for output that is stable and short enough to review.
- **Snapshots are noisy:** They check every detail, including details no user would notice.
- **Snapshots can miss looks:** A saved copy of the code output can still match after a color or layout change, because the saved text does not show the visual style.
- **Snapshots skip behavior:** They do not test clicks, interactions, or accessibility on their own.
- **Visual tests see the screen:** They compare screenshots, which catches style changes, but they are usually slower to run and to maintain.

A sensible rule is to build the base from behavior tests, then add visual checks where looks matter. Some checks still need a person. Unit tests usually run without a real browser, so element sizes, positions, and responsive rules cannot be fully tested that way.

> **Tip:** A passing snapshot does not prove that a Button looks right. Open the component and look at it too.

## Check Your Understanding

**Quick check:** A teammate changes the padding and refactors the internal code of your design system Button. The next day, the Button no longer runs its click action when loading is false. Which kind of unit test would have caught this before release, and what would it assert?

A. A snapshot test that saves the padding value, so any padding change gets flagged.
B. A behavior test that renders the Button with loading false, simulates a click, and asserts that the click action runs once.
C. A test that checks the names of the Button's internal helpers and confirms they still exist.
D. A screenshot test that checks only the Button's color in the default state.

**Answer:** B. The bug is about behavior: a click stopped doing its job. A behavior test with loading off would fail right away. The snapshot would only point at the padding change, and checking internal names says nothing about whether clicks work.

**Quick check:** A designer changes a text field's border from grey to red. The team's snapshot test for that field still passes. What is the most likely reason, and what would catch the change?

A. The snapshot is working as intended, because a border color is not part of a text field.
B. The snapshot stores saved output text, which may not show how the screen looks, so a screenshot comparison is needed to catch a color change.
C. The snapshot test is broken, so the team should delete all tests because no test can catch color changes.
D. The behavior test should have failed, because clicking the field changed its border color.

**Answer:** B. Snapshot tests compare saved output, which can miss a visual style change. A visual test compares what the browser actually paints, so it is the better check for a color change.

## Wrap Up

A unit test checks one component in one state at a time. It acts like a user, checks the result against the spec, and runs again every time someone changes the part. Start with behavior tests for each variant and state, and add snapshot or visual checks where looks matter. Accessibility checks, such as screen reader support and contrast, are covered in a separate lesson.

## Sources

- [Reshaped: Testing design systems in 2025](https://www.reshaped.so/blog/testing-design-systems)
- [Lost Pixel: What is Design System Testing? (2024 guide)](https://www.lost-pixel.com/blog/design-system-testing)
- [ResumeLens: React Testing Library Patterns](https://www.resumelens.org/blog/react/react-testing-library-patterns)
- [NamasteDev: What Is React Testing Library and Why It Changed React Testing](https://namastedev.com/topics/ep-13-time-for-test/what-is-react-testing-library-explained)
- [JumpCloud Engineering: How to Write Good UI Component Tests](https://medium.com/jumpcloud-engineering/how-to-write-good-ui-component-tests-52a13cb4a8a5)
- [Overctrl: A Beginner's Guide to Unit Testing in Frontend Development](https://blog.overctrl.com/a-beginners-guide-to-unit-testing-in-frontend-development/)
- [Plain English: A Primer on Unit Testing in UI](https://plainenglish.io/community/a-primer-on-unit-testing-in-ui-930a4a)
- [Maya Shavin: Effective Visual Regression Testing for Developers: Vitest vs Playwright](https://mayashavin.com/articles/visual-testing-vitest-playwright)
- [Percy: Snapshot Testing: Should You Go For It in 2026?](https://percy.io/blog/snapshot-testing)
- [SpecStory: What is snapshot testing?](https://specstory.com/learning/testing/snapshot-testing)
- [UI Verify: Snapshot testing vs visual regression testing](https://uiverify.ai/blog/snapshot-testing-vs-visual-regression-testing)
- [Meticulous: How to Set up Visual Regression Tests](https://www.meticulous.ai/blog/how-to-set-up-visual-regression-testing)
- [DZone: How Do You Test A Design System? Advanced Topics](https://dzone.com/articles/how-do-you-test-a-design-system-advanced-topics)
- [dev.co: React Testing Library: Behavior-Driven Component Testing](https://dev.co/testing/open-source/react-testing-library)
