Logo Component Testing

Component Testing

Tổ chức
staple-lab
component-testing

Use when writing or reviewing tests for design system components - behavioural tests with Vitest and Testing Library, keyboard and focus tests, axe accessibility assertions, controlled/uncontrolled parity, visual regression, and Storybook play functions. Triggers on "test this component", "a11y test", "visual regression", "storybook test".

Add to TypingMind

Tổng quan

Nhà phát hànhstaple-lab
Kho mãdesign-system-skills
Tên kỹ năngcomponent-testing
Sao
7
Bản fork
0
Tệp đi kèm
Chỉ có hướng dẫn
Giấy phépMIT
Liên kết
  • Hướng dẫn bằng Markdown

    Tệp SKILL.md được mô hình tải khi cần, nên chỉ tốn token khi có yêu cầu thực sự phù hợp.

  • Dùng được với mọi LLM

    Kỹ năng AI dùng hướng dẫn viết bằng Markdown thay vì mã dành riêng cho một nhà cung cấp, nên dùng được với GPT, Claude, Gemini, Grok hoặc mô hình cục bộ.

  • Hướng dẫn có đủ mọi thứ cần thiết

    Mọi thứ mô hình cần đều nằm trong hướng dẫn — không có tệp bổ sung cần đồng bộ.

  • Mã nguồn mở

    Được staple-lab công bố trên GitHub. Hãy đọc các tệp nguồn trước khi cài đặt.

Cài đặt

Cài đặt kỹ năng AI Component Testing trong TypingMind để dùng với bất kỳ LLM nào, hoặc thêm vào một tác nhân AI khác có thể đọc SKILL.md.

1

Cài đặt trong TypingMind

TypingMind cài đặt kỹ năng trực tiếp từ thư mục GitHub — đọc SKILL.md, đóng gói các tệp tài nguyên và lưu kết quả trên máy.

  1. Mở ứng dụng và vào Plugin → Kỹ năng.
  2. Chọn "Cài đặt từ GitHub".
  3. Dán URL của thư mục kỹ năng được hiển thị bên dưới rồi xác nhận.
    https://github.com/staple-lab/design-system-skills/tree/main/plugins/design-system/skills/component-testing
  4. Bật kỹ năng trong từng cuộc trò chuyện mà bạn muốn sử dụng kỹ năng đó.
Plugin → Kỹ năng → Thêm kỹ năng → Từ URL GitHub, sau đó dán URL thư mục và nhấn Tiếp tục.
2

Cài đặt trong tác nhân AI khác

Bất kỳ tác nhân AI nào đọc được định dạng Agent Skills đều có thể dùng kỹ năng này — hãy sao chép thư mục vào thư mục kỹ năng của tác nhân AI đó.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/staple-lab/design-system-skills.git /tmp/design-system-skills
mkdir -p .claude/skills
cp -r /tmp/design-system-skills/plugins/design-system/skills/component-testing .claude/skills/component-testing
Khởi động lại Claude Code sau khi sao chép để ứng dụng nhận kỹ năng mới.

Sử dụng trong TypingMind

Bật Component Testing trong bất kỳ cuộc trò chuyện nào trên TypingMind, mô hình sẽ tự xử lý phần còn lại. Tên và mô tả của kỹ năng nằm trong prompt hệ thống. Ngay khi có yêu cầu phù hợp, mô hình tự tải toàn bộ hướng dẫn — bạn không cần gọi thủ công và kỹ năng không tốn token cho đến khi thực sự được dùng.

Mô hình tự nạp Component Testing ngay khi có yêu cầu phù hợp.

Dùng được với mọi mô hình AI

Kỹ năng AI là các hướng dẫn Markdown thuần túy, không phải mã dành riêng cho một nhà cung cấp, nên bạn có thể dùng Component Testing với cả những mô hình khác ngoài mô hình ban đầu. Chỉ cần cài đặt một lần trong TypingMind rồi dùng với GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama hoặc mô hình cục bộ do bạn tự chạy — tất cả đều dùng khóa API của riêng bạn.

  • Chỉ tải khi cần

    Prompt hệ thống chỉ chứa tên và mô tả. Hướng dẫn được tải khi có yêu cầu phù hợp đầu tiên, nên khi kỹ năng chưa được dùng, bạn không tốn token cho phần hướng dẫn.

  • Đổi mô hình ngay trong cuộc trò chuyện

    Vì kỹ năng là hướng dẫn chứ không phải mã, việc đổi mô hình không làm kỹ năng ngừng hoạt động — mô hình tiếp theo vẫn đọc cùng tệp SKILL.md.

Hướng dẫn của kỹ năng

Đây là nội dung SKILL.md mà mô hình tải. Hãy đọc trước khi cài đặt — kỹ năng là những hướng dẫn mà mô hình của bạn sẽ làm theo.

Component testing

A design system component is a contract, and the tests are the executable half of it. Consumers depend on behaviour they never read the source for, so the test suite's job is to make that behaviour impossible to change by accident.

Test behaviour, not implementation

tsx
// Bad — asserts the implementation; breaks on every refactor, catches nothing
expect(wrapper.find('.btn-primary').prop('isOpen')).toBe(true);
expect(container.querySelector('div > span:nth-child(2)')).toBeTruthy();

// Good — asserts what a user experiences
await user.click(screen.getByRole('button', { name: 'Open menu' }));
expect(screen.getByRole('menu')).toBeVisible();
expect(screen.getByRole('menuitem', { name: 'Settings' })).toHaveFocus();

Query by role and accessible name, in this priority: getByRole → getByLabelText → getByText → getByTestId (last resort). This is not stylistic. getByRole('button', { name: 'Save' }) fails when the accessible name breaks, which means your functional tests double as accessibility tests for free. A test suite full of getByTestId passes happily while the component is unusable with a screen reader.

The contract suite

Every component gets these. Copy ${CLAUDE_PLUGIN_ROOT}/templates/testing/Button.contract.test.tsx and adapt — the shape should be identical across components so a reviewer can see at a glance what is missing.

  1. Renders every variant × size × state. Cheap, and catches the CVA key typo that silently renders unstyled.
  2. Keyboard map. Every documented key, including the ones people forget: Escape closes and returns focus to the trigger, Tab order is correct, arrows move within a composite widget, Home/End jump, typeahead selects.
  3. Focus management. Focus moves into a dialog on open and back to the trigger on close. This is the single most commonly broken behaviour in hand-rolled components and the most disorienting for keyboard users.
  4. Controlled and uncontrolled parity. Both modes work; onValueChange fires in both. Test them as two runs of the same assertions.
  5. Ref forwarding. The ref lands on a real DOM node.
  6. className merged, ...rest spread. The escape hatches, as tests — otherwise they rot.
  7. Disabled semantics. Not activatable by click or keyboard, and communicated to assistive tech (disabled or aria-disabled, deliberately chosen — aria-disabled keeps the element focusable, which is often better because a focusable disabled control can explain why).
  8. axe clean, in every variant and both themes.
tsx
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { axe } from 'vitest-axe';

it.each(['primary', 'secondary', 'ghost', 'danger'] as const)('%s variant has no a11y violations', async (variant) => {
  const { container } = render(<Button variant={variant}>Save</Button>);
  expect(await axe(container)).toHaveNoViolations();
});

it('returns focus to the trigger when the dialog closes', async () => {
  const user = userEvent.setup();
  render(<Example />);
  const trigger = screen.getByRole('button', { name: 'Open' });
  await user.click(trigger);
  await user.keyboard('{Escape}');
  expect(trigger).toHaveFocus();
});

Always userEvent, never fireEvent. fireEvent.click dispatches one synthetic event; userEvent.click does the pointerdown/mousedown/focus/mouseup/click sequence a real user produces. Components that pass with fireEvent and fail in the browser are almost always failing on that difference.

What axe does and does not catch

Automated a11y testing catches roughly 30–40% of WCAG issues. It is a floor, not a ceiling, and treating a green axe run as "accessible" is the most common mistake in this area.

Caught: missing labels, contrast (when computable), invalid ARIA, duplicate ids, missing landmarks, non-focusable interactive elements.

Not caught, and therefore your job:

  • Focus order being logical rather than merely present
  • Whether an accessible name is useful ("Button 3" passes)
  • Whether an error message is announced when it appears
  • Whether a live region interrupts at the right moment
  • Whether the keyboard path to a feature is reasonable rather than technically possible
  • Whether the component works with an actual screen reader

Write explicit tests for focus order, announcements and keyboard paths. And at least once per release, drive the component with VoiceOver or NVDA — nothing substitutes for hearing it.

Storybook

Storybook 10's Vitest addon replaced the old @storybook/test-runner. Stories become test cases via play functions, and the a11y addon runs axe in the same pass — one artifact serving docs, manual review and automation.

tsx
export const OpensOnClick: Story = {
  play: async ({ canvasElement, step }) => {
    const canvas = within(canvasElement);
    await step('opens the menu', async () => {
      await userEvent.click(canvas.getByRole('button', { name: 'Options' }));
      await expect(canvas.getByRole('menu')).toBeVisible();
    });
  },
};

The division of labour that works: stories for states worth looking at (they are the docs, and they feed visual regression), unit tests for logic and edge cases (faster, and better failure messages).

Visual regression

Behaviour tests do not catch a broken border radius or a token that resolved to the wrong colour. VRT does, and for a design system it earns its keep more than in a product app because one component change touches every screen.

  • Playwright screenshots — free, deterministic, you host the baselines.
  • Chromatic — hosted, handles review workflow and cross-browser, costs money.

Snapshot every component in: each variant, both themes, and at 200% zoom. Disable animations and freeze anything time-based, or you will spend your life triaging flakes:

ts
await page.emulateMedia({ reducedMotion: 'reduce' });

Keep a strict diff threshold. A loose one means real regressions slip through, which is worse than not having VRT at all — it manufactures false confidence.

Token and theme tests

Cheap, and they catch the failures that are hardest to spot by eye:

ts
it('every semantic token is defined in every theme', () => {
  const { themes } = tokens;
  const paths = Object.keys(themes.light);
  for (const [name, theme] of Object.entries(themes)) {
    expect(Object.keys(theme).sort()).toEqual(paths.sort());   // a hole in a theme is a silent bug
  }
});

it('components reference no primitive tokens directly', () => {
  // tier violations kill theming — the lint rule catches source, this catches the built output
  const css = readFileSync('dist/styles.css', 'utf8');
  expect(css).not.toMatch(/var\(--ds-color-(neutral|accent|success|warning|danger)-\d+\)/);
});

The contrast gate already runs in the token build. Do not duplicate it here; make sure it runs in CI.

What not to test

  • Third-party primitive internals — Radix tests Radix. Test your wrapper's contract.
  • Exact class names or CSS output. That is VRT's job, and asserting on classes is what makes refactors painful.
  • Snapshot tests of rendered markup. They fail on every change and get rubber-stamped with -u, at which point they are pure cost. Targeted assertions or a real VRT tool.

CI gates

typecheck → unit + a11y → token build (contrast gate) → lint → VRT → build → publint/attw

Fail the build on: a11y violations, contrast failures, type errors, DS lint errors. Warn on: coverage dip, bundle-size growth beyond a threshold.

Coverage floor around 80% for a design system — higher than a typical app, because consumers cannot see inside and a regression ships everywhere at once. Do not chase 100%: the last 15% is usually error branches that would need contrived setups, and the effort is better spent on a real screen-reader pass.

Câu hỏi thường gặp

Kỹ năng AI Component Testing làm được gì?

Use when writing or reviewing tests for design system components - behavioural tests with Vitest and Testing Library, keyboard and focus tests, axe accessibility assertions, controlled/uncontrolled parity, visual regression, and Storybook play functions. Triggers on "test this component", "a11y test", "visual regression", "storybook test".

Vì sao nên dùng Component Testing trên TypingMind?

Vì bạn chỉ cần cài một lần rồi dùng với bất kỳ mô hình nào. Component Testing là nội dung Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên cùng một kỹ năng có thể chạy trên GPT-5, Claude, Gemini, Grok hoặc mô hình trên thiết bị — và bạn có thể đổi mô hình ngay trong cuộc trò chuyện mà kỹ năng vẫn hoạt động. TypingMind dùng khóa API riêng của bạn, nên bạn thanh toán trực tiếp cho nhà cung cấp thay vì trả phí thuê bao theo suất người dùng, đồng thời kỹ năng và các cuộc trò chuyện vẫn nằm trong bộ lưu trữ của bạn.

Làm thế nào để cài Component Testing trong TypingMind?

Mở Plugin → Kỹ năng → Cài đặt từ GitHub trong TypingMind rồi dán https://github.com/staple-lab/design-system-skills/tree/main/plugins/design-system/skills/component-testing. TypingMind đọc tệp SKILL.md rồi cài đặt kỹ năng để bạn có thể bật cho từng cuộc trò chuyện.

Những mô hình AI nào có thể dùng Component Testing?

Bất kỳ mô hình nào bạn kết nối trong TypingMind. Kỹ năng AI là các hướng dẫn bằng Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên GPT, Claude, Gemini, Grok và các mô hình chạy trên thiết bị đều có thể tải kỹ năng này khi có yêu cầu phù hợp.

Có thể dùng Component Testing với bao nhiêu mô hình AI?

Bao nhiêu cũng được. Miễn là mô hình hỗ trợ kỹ năng, bạn có thể dùng Component Testing với mô hình đó — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama và nhiều mô hình khác — tất cả đều trên TypingMind với khóa API riêng của bạn.

Kỹ năng AI Component Testing có miễn phí không?

Có. Kỹ năng này được staple-lab phát hành trên GitHub theo giấy phép MIT. Bạn chỉ trả tiền cho nhà cung cấp AI của mình theo số token đã dùng.

Kỹ năng AI là gì?

Kỹ năng AI là một gói hướng dẫn có thể tái sử dụng, chỉ cho mô hình AI cách thực hiện một tác vụ cụ thể. Kỹ năng tuân theo định dạng mở Agent Skills: một tệp SKILL.md chứa tên và mô tả, cùng các tập lệnh, mẫu hoặc tệp tham khảo mà mô hình có thể cần. Mô hình chỉ đọc hướng dẫn khi yêu cầu của bạn phù hợp với kỹ năng, nên kỹ năng đã cài đặt không phát sinh chi phí cho đến khi được sử dụng.

Kỹ năng AI khác gì với plugin hoặc máy chủ MCP?

Plugin hoặc máy chủ MCP cung cấp các công cụ mới mà mô hình có thể gọi. Khi mô hình gọi một công cụ, mã của công cụ đó chạy ở đâu đó và trả về kết quả. Còn kỹ năng AI cung cấp kiến thức và quy trình: cách tiếp cận tác vụ, các bước cần thực hiện và những tiêu chí mà kết quả cần đáp ứng. Kỹ năng chỉ là nội dung Markdown, nên không cần máy chủ, khóa API hay môi trường thực thi và hoạt động với mọi mô hình.

Xem tất cả

Tạo ngay không gian làm việc AI riêng

Đăng ký bản tin của chúng tôi để nhận thông báo về tính năng mới và các đợt tặng quà sắp tới 👇