一键导入
pf-unit-test-generator
Generate a unit test file for a React component using Testing Library. Use when adding test coverage to new or existing components.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Generate a unit test file for a React component using Testing Library. Use when adding test coverage to new or existing components.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | pf-unit-test-generator |
| description | Generate a unit test file for a React component using Testing Library. Use when adding test coverage to new or existing components. |
Generate a comprehensive unit test file for the given React component.
The user will provide a component file path or component code. Read the component source before generating tests.
Determine if the component is part of a component library (patternfly-react, patternfly-chatbot, or similar) or a consumer application. Check whether the component lives in a library source tree or an application codebase. This determines the testing approach — see "Component Library Contributions" below for adjustments.
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
Organize tests into describe blocks by behavior: rendering, user interactions, conditional rendering, async operations, accessibility — only include sections that apply.
Queries — use in this order:
getByRole (always first choice)getByLabelTextgetByTextgetByTestId (last resort only)Interactions — always userEvent, never fireEvent:
const user = userEvent.setup();
await user.click(screen.getByRole("button", { name: "Save" }));
Mocking — mock at the network boundary:
jest.clearAllMocks() in beforeEachAsync — prefer findBy* over waitFor for waiting on elements:
expect(await screen.findByText("Success")).toBeInTheDocument();
Use waitFor only for non-query assertions:
await waitFor(() => {
expect(onComplete).toHaveBeenCalled();
});
What to test:
What NOT to test:
CSS classes — test with toHaveClass when classes are part of the component's public API (e.g., modifier classes, conditional styling). Don't test internal or structural classes that are implementation details.
When testing a component within a component library (patternfly-react, patternfly-chatbot, etc.), these adjustments apply. The "user" of a library component is a developer consuming its API — so test the API contract.
Mocking child components — default to mocking child components for unit isolation:
jest.mock('../Header', () => ({
Header: ({ children, ...props }) => <h1 {...props}>{children}</h1>
}));
Exception: don't mock when the parent-child interaction is the behavior being tested (e.g., a composite component where orchestration between children is the point).
Coverage checklist — cover these for every library component:
Snapshots — use for component structure and element ordering. Do not use snapshots to verify CSS classes — use toHaveClass instead:
const { asFragment } = render(<MyLayout />);
expect(asFragment()).toMatchSnapshot();
File organization — one test file per exported component, colocated with the source:
Button/
├── Button.tsx
├── Button.test.tsx
├── ButtonVariant.tsx
└── ButtonVariant.test.tsx
PatternFly-specific conventions — follow the PatternFly testing wiki. Use test() for top-level tests and it() inside describe() blocks:
test('renders with default props', () => { ... });
describe('when disabled', () => {
it('has disabled attribute', () => { ... });
it('does not fire onClick', () => { ... });
});
Output the complete test file ready to save. Name it ComponentName.test.tsx matching the source file.
Integrate @patternfly/design-comments into React apps for on-page design feedback, pinned comment threads, GitHub Issues sync, and Jira linking. Use when adding design comments, review overlays, or removing the commenting system from a PatternFly React project.
Check Figma designs against PatternFly v6 standards for colors, typography, spacing, and component usage. Use when validating a design before handoff, auditing existing mockups for compliance, or reviewing design token usage. Requires Figma MCP.
Identify PatternFly icons in Figma mockups and provide the correct React import statements. Use when implementing a design from Figma, verifying icon usage in a prototype, or finding the correct icon imports for React components. Requires Figma MCP.
Find raw color values (hex, rgb, hsl) in code and suggest PatternFly design token replacements. Use when auditing stylesheets for hardcoded colors or enforcing token compliance.
Audit designs against the PatternFly 6 token architecture and bridge Figma styles to PF semantic tokens. Use when validating token usage, mapping Figma variables to PF tokens, or checking designs for token compliance.
Apply Red Hat's AI design language to AI-powered features — chatbots, assistants, generation UIs. Use when building AI experiences that should follow Red Hat brand and UX patterns.