Grommet Component Testing Skill
When to Use
- Writing a new
__tests__/ComponentName-test.tsxfile - Adding test cases to an existing test file
- Creating Storybook story files in
stories/ - Reviewing tests against Grommet's coverage and accessibility requirements
Workflow
Tests
- axe first — the very first test in every file must be an axe accessibility check wrapped in
<Grommet>. - Queries — query via
screen.getByRole→getByLabelText→getByText. UsegetByRolewhenever a semantic role is available. - Interactions — use
userEvent.setup()for all user interactions (click, type, keyboard). UsefireEventonly for low-level synthetic events thatuserEventcannot produce. - Snapshots — use
asFragment()for snapshot assertions, notcontainer.innerHTML. - Wrappers — wrap renders in
<Grommet>by default. Omit only when explicitly testing unwrapped/unstyled behavior. - Coverage — target ≥85% for state logic, prop handling, and keyboard navigation.
Stories
- One story per file, CSF-3 format.
- Title:
'ComponentCategory/ComponentName/StoryName'. - Always include a basic controlled and/or uncontrolled usage story when the component has a value contract.
- Include a
Form+FormFieldintegration story when the component participates in forms. - Add a
CustomThemedstory only when the component introduces new theme tokens.
Key Rules
- File extension:
.tsxfor both test files and new story files - Test files live in
__tests__/, story files live instories/ userEvent.setup()notfireEventfor simulating user interactionsscreen.getByRoleas the first query choice — it reflects actual accessibility semantics- Every test file starts with an axe check
- Wrap all renders in
<Grommet>unless testing unwrapped behavior explicitly
See Also
- REFERENCE.md — full code examples for test structure, axe setup, userEvent patterns, story CSF-3 format, and FormField integration