Testing UI Components with Testing Library & Playwright
Testing design system components ensures that accessibility attributes, variants, and interactive states remain stable across software releases. Component testing prioritizes user-centric queries — searching for elements by accessible role (getByRole('button', { name: /submit/i })) rather than CSS selectors.
Unit and component tests (using React Testing Library) verify prop rendering and event firing, while visual regression tests (using Playwright screenshots) compare visual pixel outputs across browsers to catch unintentional CSS breakages.
Exercise
Write a React Testing Library test for an Accordion component verifying that clicking the header expands the panel and updates aria-expanded.
Check your understanding
Why are role-based queries (getByRole) preferred over CSS selectors in testing?Show answerHide answer
Answer
They test the UI from the user and screen reader's perspective, making tests resilient to refactoring.What is visual regression testing in UI development?Show answerHide answer
Answer
Comparing browser screenshot images of components against baseline reference snapshots to detect visual pixel changes.