Component tests
What you need to know
Component tests exercise a React component through its rendered interface, rather than its private state variables. Query the accessible name of a control and assert what changes after interaction.
By the end, you can: test accessible interaction; avoid testing implementation details.
An accessible quantity picker
'use client';import {useState} from 'react';export default function Quantity(){ const [n,setN]=useState(1); return <section aria-label="Quantity"><p role="status">{n} selected</p><button disabled={n===3} onClick={()=>setN(v=>Math.min(3,v+1))}>Add one</button></section>;}Expected behavior: Starts at 1 selected. Two activations reach 3 selected and disable Add one.
How it works
Use a DOM-oriented test environment such as jsdom with reviewed React Testing Library dependencies. A user-event helper approximates common interaction sequences more closely than calling a handler directly. Select controls by role and accessible name when meaningful. After clicking, assert visible state, disabled state or status feedback. Configure cleanup between tests so one rendered tree cannot contaminate the next. Vitest and Jest both support many synchronous React components with appropriate transforms; the exact setup depends on dependencies. A DOM model cannot reliably prove layout, focus visibility, browser navigation, real hydration or production CSS. The documented Next.js Vitest and Jest setups do not support rendering async Server Components as ordinary unit tests. Use framework-aware integration or end-to-end testing for those components; synchronous component tests remain useful.
Test through the rendered contract
// tests/quantity.test.tsximport {test,expect} from 'vitest';import {render,screen} from '@testing-library/react';import userEvent from '@testing-library/user-event';import Quantity from '../app/quantity';test('stops at three',async()=>{ render(<Quantity/>);const user=userEvent.setup(); const add=screen.getByRole('button',{name:'Add one'}); await user.click(add);await user.click(add); expect(screen.getByRole('status').textContent).toBe('3 selected'); expect((add as HTMLButtonElement).disabled).toBe(true);});Expected behavior: Expected pass with compatible React, Vitest, jsdom and cleanup setup; not executed here.
Common mistake
Avoid testing hook call counts or internal variable names. Such tests break on harmless refactoring while missing an inaccessible or incorrectly labeled control.
Practice and verification
Add a fresh-render assertion for 1 selected and a disabled-at-three check. Then plan a real keyboard interaction test for the production fixture.
Scope: Proposed Next.js 16.x / React 19, Cache Components disabled unless noted. Framework, browser and component examples are staged; local standalone tests are reported separately. Exact dependency patches and platform contracts pending.