Ui data testid

From dtyq/magic · View interactive version

Ui data testid is an AI agent skill from the GitHub repository dtyq/magic. Add stable `data-testid` attributes when a new or refactored UI flow needs reliable unit or E2E selectors. Use for interactive React/TSX views, menus, or test-driven UI work; do not add selectors to static markup without a test or automation need.

How to use this skill

Skill file

UI Data-testid

Overview

Add predictable data-testid attributes to UI code as part of implementation, not as a later patch.

Keep selectors stable across i18n text changes and visual refactors.

Follow project testing rules in .cursor/rules:

Workflow

  1. Determine the scope prefix from feature/module context (for example: user-menus, organization-switch, settings-profile).
  2. Add data-testid to the component root container.
  3. Add data-testid to all primary interactive nodes:
  1. For config-driven UI (for example menu.items), add "data-testid" in config and forward it to the real clickable DOM node in renderer/wrapper components.
  2. For repeated list rows/items, put the stable data-testid on the row/item container first. Do not add separate unique ids to every child element by default.
  3. Inside a list row/item, reuse the row scope in tests: locate the row by shared row id plus text/business data, then query child controls with within(row) or role/label selectors.
  4. Add child-level data-testid inside repeated rows only when the child cannot be reliably selected from the row scope; if needed, keep the child id shared across rows instead of appending row ids.
  5. Keep existing ids unchanged unless user explicitly asks to rename; never remove existing ids in migration tasks.
  6. For interaction changes, add or update Vitest/RTL tests in colocated __tests__ where feasible.

Naming Rules

Minimum Coverage Checklist

For every newly created UI component, include at least:

Query Priority

When writing or updating tests:

This keeps alignment with .cursor/rules/testing-guide.mdc.

Scenario Playbook

Apply these patterns for stable and accurate element targeting:

  1. Forms
  1. Lists and tables
  1. Menus and dropdowns
  1. Modal and drawer
  1. Async states

Stability Rules

Patterns

Component markup

<div data-testid="user-menus-organization-info">
  <button type="button" data-testid="user-menus-upgrade-button" />
  <button type="button" data-testid="user-menus-recharge-button" />
</div>

Repeated list row

<div data-testid="collaborator-list-content">
  {collaborators.map((collaborator) => (
    <div
      key={collaborator.id}
      data-testid="collaborator-item"
      data-collaborator-id={collaborator.id}
    >
      <span>{collaborator.name}</span>
      <button type="button" data-testid="collaborator-remove-button" />
    </div>
  ))}
</div>

const row = page
  .getByTestId("collaborator-list-content")
  .locator('[data-testid="collaborator-item"]')
  .filter({ hasText: receiverName })

await row.getByTestId("collaborator-remove-button").click()

Config + renderer forwarding

const items = [
  { key: "logout", label: t("logout"), "data-testid": "user-menus-logout" },
]

<ItemComponent data-testid={menuItem["data-testid"]}>{menuItem.label}</ItemComponent>

Done Criteria

Complete only when all new singleton interactive nodes in touched UI files have stable data-testid values and repeated list/table rows have row-level selectors that allow child controls to be found from the row scope.

Confirm no existing data-testid was removed unintentionally in migration/refactor diffs.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.