# Component Testing > **When to use**: Testing UI components in isolation (rendering, interactions, states) without the full application. Ideal for design systems, shared component libraries, and complex interactive widgets. > **Prerequisites**: [core/configuration.md](configuration.md), [core/fixtures-and-hooks.md](fixtures-and-hooks.md) ## Quick Reference ```tsx // src/components/Button.story.tsx: one named export per scenario import { Button } from './Button'; export const Primary = () => ); export const Empty = () => ; ``` ```typescript test('renders children inside the card body', async ({ mount }) => { const component = await mount('components/Card/WithActions'); await expect(component.getByRole('heading', { name: 'Invoice #42' })).toBeVisible(); await expect(component.getByRole('button', { name: 'Pay now' })).toBeVisible(); }); ``` For slot-heavy Vue scenarios, a single-file story is clearer than render functions: ```vue ``` ### 7. Providers, Context and Plugins **Use when**: Components need a theme, store, router, i18n or query client. Wrap stories in one shared decorator kept next to the stories, not inside the gallery, so each story shows what it depends on and can opt out: ```tsx // src/stories/decorators.tsx import type { ReactNode } from 'react'; import { ThemeProvider } from '../theme'; export function AppScaffold({ children, theme = 'light' }: { children: ReactNode, theme?: 'light' | 'dark' }) { return {children}; } ``` ```tsx // src/components/ThemedBadge.story.tsx import { AppScaffold } from '../stories/decorators'; import { ThemedBadge } from './ThemedBadge'; export const Dark = () => ; ``` - Create clients (React Query, Apollo, Pinia) **inside** the story or decorator, so each `mount()` starts fresh. - Vue plugins that must be installed with `app.use(...)` go in the gallery right after `createApp(...)`, mirroring the app's bootstrap. - The body of `window.mount` replaces the old `beforeMount` / `afterMount` hooks for app-wide setup. ### 8. Network and Dependencies **Use when**: The component fetches data or depends on a module you want to replace. Register `page.route()` **before** `mount()`, because mounting navigates: ```typescript test('shows an error when items fail to load', async ({ mount, page }) => { await page.route('**/api/items', route => route.fulfill({ status: 500 })); const component = await mount('components/ItemList/Default'); await expect(component.getByRole('alert')).toContainText('Something went wrong'); }); test('renders items from the API', async ({ mount, page }) => { await page.route('**/api/items', route => route.fulfill({ json: [{ id: 1, name: 'Blue Mug' }] })); const component = await mount('components/ItemList/Default'); await expect(component.getByRole('listitem')).toHaveText(['Blue Mug']); }); ``` There is no test-side module mocking: the component runs in the browser, built by your dev server. To replace a dependency, pass it in through the story (a prop or provider with a fake implementation), use your bundler's aliasing for the gallery, or start an MSW worker inside a story or decorator. ### 9. Visual Comparison **Use when**: Locking down the look of design-system components and states. Screenshot the returned root locator, not the page, and mount several stories per test cheaply: ```typescript test('button states', async ({ mount }) => { await expect(await mount('components/Button/Primary')).toHaveScreenshot('primary.png'); await expect(await mount('components/Button/Disabled')).toHaveScreenshot('disabled.png'); }); ``` Keep the recording form `hidden` so it stays out of baselines. Baseline rules (OS, fonts, Docker) are in [visual-regression.md](visual-regression.md). ### 10. Component Test vs E2E Test **Use when**: Deciding where a piece of UI behavior should be tested. Component tests verify **how a component behaves in isolation**; E2E tests verify **how the system works together**. A loading button's disabled state and spinner belong in a component test; "saving the form persists the record and shows a toast" belongs in E2E. See [test-architecture.md](test-architecture.md). ## Decision Guide | UI element | Component test | E2E test | Unit test | |---|---|---|---| | **Button** (variants, states, loading) | Yes: every variant, disabled, loading, click recording | Only as part of a larger flow | No: needs real DOM for styling and accessibility | | **Form field** (validation, masking) | Yes: messages, masking, error states | Yes: full submission with backend | Validate-only logic (regex, formatters) | | **Modal / dialog** | Yes: open/close, focus trap, Escape | Yes: the flow that opens it | No | | **Data table** (sort, filter, paginate) | Yes: with routed mock data | Yes: real API data and URL sync | Pure sort/filter functions | | **Navigation / menu** | Partially: dropdown behavior, active states | Yes: real route changes | No | | **Full page** (dashboard, settings) | No: too much context; defeats isolation | Yes | No | | **Chart** | Yes: visual regression of rendered output | Only in critical flows | Data transformation only | | **Design-system primitives** | Yes: the primary use case | No | No | **Rule of thumb**: component tests for behavior and appearance in isolation, E2E for user journeys across components and pages, unit tests for pure logic with no DOM. ## Migrating From the Experimental Packages | `@playwright/experimental-ct-*` | Story gallery (1.62+) | |---|---| | `mount(