Agent Skillsdtyq/magic › ui-data-testid

ui-data-testid

GitHub

为 React/TSX UI 组件默认添加稳定 data-testid,确保选择器在 i18n 和重构中保持一致。规范命名、覆盖范围及测试查询策略,支持单元/E2E 测试。

frontend/magic-web/.agents/skills/ui-data-testid/SKILL.md dtyq/magic

Trigger Scenarios

实现新的 UI 组件 重构现有界面逻辑 配置菜单或下拉项 编写交互流程测试

Install

npx skills add dtyq/magic --skill ui-data-testid -g -y
More Options

Non-standard path

npx skills add https://github.com/dtyq/magic/tree/master/frontend/magic-web/.agents/skills/ui-data-testid -g -y

Use without installing

npx skills use dtyq/magic@ui-data-testid

指定 Agent (Claude Code)

npx skills add dtyq/magic --skill ui-data-testid -a claude-code -g -y

安装 repo 全部 skill

npx skills add dtyq/magic --all -g -y

预览 repo 内 skill

npx skills add dtyq/magic --list

SKILL.md

Frontmatter
{
    "name": "ui-data-testid",
    "description": "Add stable `data-testid` attributes by default for new or refactored UI components. Use when implementing React\/TSX views, shadcn\/antd-style components, dropdown\/menu configs, or interactive UI flows that need reliable selectors for unit\/E2E tests."
}

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:

  • preserve existing data-testid during refactor/migration
  • use data-testid-first query strategy in project tests

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:
    • button/link triggers
    • input/select/checkbox/radio controls
    • tabs/menu items/submenu triggers
    • modal/drawer open and confirm actions
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. Keep existing ids unchanged unless user explicitly asks to rename; never remove existing ids in migration tasks.
  9. For interaction changes, add or update Vitest/RTL tests in colocated __tests__ where feasible.

Naming Rules

  • Use lowercase kebab-case only.
  • Use semantic format: <scope>-<entity>-<action>.
  • Keep IDs text-agnostic (do not depend on i18n labels).
  • Avoid dynamic/random values (Date.now, UUID, translated text).
  • Do not embed secrets, emails, phone numbers, or tokens.
  • Use stable suffixes when applicable: trigger, content, button, input, option, item, row, loading, empty, error.

Minimum Coverage Checklist

For every newly created UI component, include at least:

  • one root container test id
  • one primary CTA test id
  • test ids for each secondary action button
  • test ids for each form field group/control
  • test ids for menu item triggers when menus are present
  • for repeated lists/tables, one list container id and one row/item container id; avoid per-cell/per-action dynamic ids unless row-scoped selectors are insufficient
  • preserved historical data-testid in touched files
  • loading/empty/error test ids for async UIs

Query Priority

When writing or updating tests:

  • prefer getByTestId for stable selectors in this project
  • use getByRole, getByLabelText, getByText as complementary assertions
  • avoid container.querySelector(...) selectors for user-facing behavior tests

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

Scenario Playbook

Apply these patterns for stable and accurate element targeting:

  1. Forms
    • add ids for form container, inputs, submit/cancel buttons, and validation errors
  2. Lists and tables
    • add list container id and row container id
    • prefer a shared row id, then select the intended row by text/business data
    • use stable business key for row id suffix only when there is a concrete need for direct row lookup
    • do not add separate dynamic ids to every field/action inside the row; query child actions with within(row) scope
    • if a child action needs a data-testid, use one shared id such as collaborator-remove-button and resolve it from the row scope
  3. Menus and dropdowns
    • add ids for trigger, popup content, and each actionable menu item
    • if menu is config-driven, forward item-level data-testid to rendered node
  4. Modal and drawer
    • add ids for open trigger, modal content, primary action, and close/cancel action
  5. Async states
    • add ids for loading, empty, and error states

Stability Rules

  • Never generate ids from array index if order may change.
  • Never generate ids from random values or timestamps.
  • Keep singleton ids unique on a page.
  • For repeated components, keep shared child ids and scope with within(...).
  • Prefer row-level uniqueness over child-level uniqueness in repeated rows; child ids should not encode row ids unless there is no row container to scope from.

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.

Version History

  • f9973c5 Current 2026-08-20 03:38

    优化列表行 ID 处理策略,优先使用共享 ID 配合 row-scoped 查询;细化子元素 ID 添加条件,避免不必要的动态 ID。

  • 41d7ef4 2026-07-25 09:30

Same Skill Collection

backend/super-magic/agents/skills/agent-info/SKILL.md
backend/super-magic/agents/skills/canvas-designer/SKILL.md
backend/super-magic/agents/skills/chat-history/SKILL.md
backend/super-magic/agents/skills/cli-manager/SKILL.md
backend/super-magic/agents/skills/compact-chat-history/SKILL.md
backend/super-magic/agents/skills/creating-slides/SKILL.md
backend/super-magic/agents/skills/develop-data-analysis-dashboard/SKILL.md
backend/super-magic/agents/skills/document-converter/SKILL.md
backend/super-magic/agents/skills/download/SKILL.md
backend/super-magic/agents/skills/env-manager/SKILL.md
backend/super-magic/agents/skills/im-channels/SKILL.md
backend/super-magic/agents/skills/magic-calendar/SKILL.md
backend/super-magic/agents/skills/magicbase/SKILL.md
backend/super-magic/agents/skills/sandbox-manager/SKILL.md
backend/super-magic/agents/skills/self-media-pre-publish-analyzer/SKILL.md
backend/super-magic/agents/skills/share/SKILL.md
backend/super-magic/agents/skills/skill-vetter/SKILL.md
backend/super-magic/agents/skills/slide-template/SKILL.md
backend/super-magic/agents/skills/subagents/SKILL.md
backend/super-magic/agents/skills/teamshare-cli/SKILL.md
backend/super-magic/agents/skills/user-info/SKILL.md
backend/super-magic/agents/skills/using-cron/SKILL.md
backend/super-magic/agents/skills/using-llm/SKILL.md
backend/super-magic/agents/skills/using-mcp/SKILL.md
frontend/magic-web/.agents/skills/antd-style-to-tailwind/SKILL.md
frontend/magic-web/.agents/skills/code-review-expert/SKILL.md
frontend/magic-web/.agents/skills/dual-edition-module-migration/SKILL.md
frontend/magic-web/.agents/skills/magic-web-error-logging/SKILL.md
frontend/magic-web/.agents/skills/sw-best-practices/SKILL.md
frontend/magic-web/.agents/skills/vercel-composition-patterns/SKILL.md
frontend/magic-web/.agents/skills/vercel-react-best-practices/SKILL.md
backend/super-magic/agents/skills/ai-card-generator/SKILL.md
backend/super-magic/agents/skills/crew-creator/SKILL.md
backend/super-magic/agents/skills/deep-research/SKILL.md
backend/super-magic/agents/skills/dingtalk-cli/SKILL.md
backend/super-magic/agents/skills/html-api-sdk/SKILL.md
backend/super-magic/agents/skills/lark-cli/SKILL.md
backend/super-magic/agents/skills/micro-app-architect/SKILL.md
backend/super-magic/agents/skills/self-media-composer/SKILL.md
backend/super-magic/agents/skills/skill-creator/SKILL.md

Metadata

Files
0
Version
f9973c5
Hash
b7d69373
Indexed
2026-07-25 09:30

inicio - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-20 07:16
浙ICP备14020137号-1 $mapa de visitantes$