---
name: strongtie-test
description: "Write Vitest + Testing Library test suites for @strongtie/design-system components, covering rendering, variants, prop forwarding, ref forwarding, interaction, and accessibility. Use when: (1) Creating tests for a new component, (2) Adding tests to an existing component, (3) Fixing a failing component test."
---

# Component Test Skill

Prove a `@strongtie/design-system` component behaves the way its two-layer shape promises — that variants render, that props and the ref reach the real DOM node, and that the accessible surface is clean.

## When to use this skill

Reach for it when your edit lands in `packages/design-system/tests/` — writing the suite for a component you just authored, backfilling coverage on one that shipped without it, or chasing down a red test. The through-line is that you are asserting against a rendered `@strongtie/design-system` component, not testing app logic that merely consumes one.

## When NOT to use this skill

If you are authoring the component itself — the base primitive, its CVA variants, the wrapper — that is `strongtie-component`, and the type/`data-*` conventions live there. If you are writing the story, MDX, or registry example, that is `strongtie-docs`. And if you are testing a feature in a consuming app rather than a library component, the design-system conventions here will over-constrain you; that is general app testing, out of scope for all three authoring skills.

## Read the canonical suite before writing yours

`packages/design-system/tests/button.test.tsx` is the authoritative example, and it is the one you should open first. Do not reconstruct a suite from memory — the Button test already demonstrates every category a good suite covers, in this repo's exact idiom, and mirroring it is faster and more correct than inventing a parallel structure. Notice how it imports the component from the **wrapper** (`../src/components/button`), not the base primitive — you test the public surface a consumer actually gets. The category taxonomy it walks (rendering, variants, sizes, `className` merge, `asChild`, prop forwarding, ref, interaction, accessibility) is written up once in `references/test-patterns.md`; consult it when you need the checklist without re-reading the whole file.

## The conventions that actually carry weight

**Assert the ref reaches the DOM node, not that a callback fired.** The tempting shortcut is a `vi.fn()` ref and an `expect(ref).toHaveBeenCalled()` — but that only proves React invoked the ref callback, which it does even when the ref is wired to the wrong element or to nothing renderable. The real test uses `React.createRef<HTMLButtonElement>()`, renders, then asserts `ref.current?.tagName === "BUTTON"` and `ref.current?.textContent` (`packages/design-system/tests/button.test.tsx:82-89`). That difference matters because ref forwarding is exactly the thing that silently breaks when a wrapper forgets to spread `{...props}`: a callback-fired assertion stays green while the consumer's ref points at nothing. Assert on `ref.current` and the DOM identity, and the test fails the moment the ref stops landing on the element.

**Assert on the semantic class and `data-*`, not on Tailwind utility strings.** A component's public contract is its semantic class (`toHaveClass("button")`) and its `data-variant`/`data-size` hooks, both of which are stable. The utility classes CVA emits are an implementation detail that churns; pinning a test to them makes it brittle without making it stronger. Assert that `className` merges (the custom class *and* the semantic class both survive), and let the visual variants be proven by the story.

**Let vitest-axe carry the accessibility floor, and assert against the matcher, not the array.** The suite renders into a container and runs `axe`, then asserts `expect(results).toHaveNoViolations()` — the matcher registered globally in `packages/design-system/vitest.setup.ts`, which prints the offending nodes on failure. Reaching into `results.violations.length` instead throws that diagnostic away and tells you a count without telling you what broke. Run the axe check for the default render and for any state that changes the accessible tree (disabled, for instance), because that is where a regression hides.

**Test keyboard reachability, not just clicks.** A mouse click passing proves almost nothing about a component an accessible user can operate. The suite tabs to the element and asserts focus, and — for disabled — tabs *past* it to prove it is out of the tab order (`toHaveFocus` on the next element). If your component has its own focus semantics, that is the assertion that catches a regression a click test never would.

## Running tests

```bash
# Run the full suite (Vitest with coverage)
npm run test

# Watch mode while iterating on one component
npm run test:watch

# Run a single file
npx vitest run packages/design-system/tests/button.test.tsx
```

## Related skills

`strongtie-component` authors the component this suite proves — its ref/`data-*`/`asChild` conventions are what these tests assert against. `strongtie-docs` covers the story and MDX that document it. Read the category checklist in `references/test-patterns.md` when you need a fast completeness pass.
