-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 Add tests for ActionButtons component #369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,69 @@ | ||||||||||||||||||||||||||||||||||||||
| // @vitest-environment jsdom | ||||||||||||||||||||||||||||||||||||||
| import { render, screen } from "@testing-library/react"; | ||||||||||||||||||||||||||||||||||||||
| import { describe, it, expect, vi } from "vitest"; | ||||||||||||||||||||||||||||||||||||||
| import { ActionButtons } from "./ActionButtons"; | ||||||||||||||||||||||||||||||||||||||
| import userEvent from "@testing-library/user-event"; | ||||||||||||||||||||||||||||||||||||||
| import "@testing-library/jest-dom"; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| describe("ActionButtons", () => { | ||||||||||||||||||||||||||||||||||||||
| const defaultProps = { | ||||||||||||||||||||||||||||||||||||||
| handleCopy: vi.fn(), | ||||||||||||||||||||||||||||||||||||||
| handleDownload: vi.fn(), | ||||||||||||||||||||||||||||||||||||||
| previewUrl: "https://example.com/preview.png", | ||||||||||||||||||||||||||||||||||||||
| copyStatus: "idle" as const, | ||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+8
to
+14
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: src/components/ActionButtons.test.tsx
Line: 8-14
Comment:
`describe` ブロックのトップレベルで `vi.fn()` を一度だけ生成しているため、各テスト間でモックがリセットされません。現在のテストコードでは `defaultProps.handleCopy` / `handleDownload` に対して直接アサーションしていないため実害はありませんが、将来テストを追加した際に呼び出し回数が蓄積されてテストが誤って失敗するリスクがあります。`beforeEach` でモックをクリアするのが望ましいです。
```suggestion
describe("ActionButtons", () => {
const defaultProps = {
handleCopy: vi.fn(),
handleDownload: vi.fn(),
previewUrl: "https://example.com/preview.png",
copyStatus: "idle" as const,
};
beforeEach(() => {
vi.clearAllMocks();
});
```
How can I resolve this? If you propose a fix, please make it concise.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| it("renders the Copy Image and Download PNG buttons", () => { | ||||||||||||||||||||||||||||||||||||||
| render(<ActionButtons {...defaultProps} />); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| expect(screen.getByRole("button", { name: /Copy Image/i })).toBeInTheDocument(); | ||||||||||||||||||||||||||||||||||||||
| expect(screen.getByRole("button", { name: /Download PNG/i })).toBeInTheDocument(); | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| it("renders 'Copied!' text when copyStatus is 'copied'", () => { | ||||||||||||||||||||||||||||||||||||||
| render(<ActionButtons {...defaultProps} copyStatus="copied" />); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| expect(screen.getByRole("button", { name: /Copied!/i })).toBeInTheDocument(); | ||||||||||||||||||||||||||||||||||||||
| expect(screen.queryByRole("button", { name: /Copy Image/i })).not.toBeInTheDocument(); | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| it("disables both buttons when previewUrl is null", () => { | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+22
to
+30
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Prompt To Fix With AIThis is a comment left during a code review.
Path: src/components/ActionButtons.test.tsx
Line: 22-30
Comment:
**`copyStatus="error"` の状態がテストされていない**
`copyStatus` の型は `"idle" | "copied" | "error"` の3値ですが、`"error"` ケースのテストが存在しません。現在のコンポーネント実装では `"error"` は `"idle"` と同じ表示になりますが、将来の変更でエラー用の表示が追加された際にカバレッジが不足します。少なくとも `"error"` の場合に "Copy Image" ボタンが表示されることを確認するテストの追加を推奨します。
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||||||||||||||||||||||||||||||
| render(<ActionButtons {...defaultProps} previewUrl={null} />); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const copyButton = screen.getByRole("button", { name: /Copy Image/i }); | ||||||||||||||||||||||||||||||||||||||
| const downloadButton = screen.getByRole("button", { name: /Download PNG/i }); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| expect(copyButton).toBeDisabled(); | ||||||||||||||||||||||||||||||||||||||
| expect(downloadButton).toBeDisabled(); | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| it("enables both buttons when previewUrl is provided", () => { | ||||||||||||||||||||||||||||||||||||||
| render(<ActionButtons {...defaultProps} previewUrl="https://example.com/image.png" />); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const copyButton = screen.getByRole("button", { name: /Copy Image/i }); | ||||||||||||||||||||||||||||||||||||||
| const downloadButton = screen.getByRole("button", { name: /Download PNG/i }); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| expect(copyButton).not.toBeDisabled(); | ||||||||||||||||||||||||||||||||||||||
| expect(downloadButton).not.toBeDisabled(); | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| it("calls handleCopy when the copy button is clicked", async () => { | ||||||||||||||||||||||||||||||||||||||
| const handleCopyMock = vi.fn(); | ||||||||||||||||||||||||||||||||||||||
| render(<ActionButtons {...defaultProps} handleCopy={handleCopyMock} />); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const copyButton = screen.getByRole("button", { name: /Copy Image/i }); | ||||||||||||||||||||||||||||||||||||||
| await userEvent.click(copyButton); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| expect(handleCopyMock).toHaveBeenCalledTimes(1); | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| it("calls handleDownload when the download button is clicked", async () => { | ||||||||||||||||||||||||||||||||||||||
| const handleDownloadMock = vi.fn(); | ||||||||||||||||||||||||||||||||||||||
| render(<ActionButtons {...defaultProps} handleDownload={handleDownloadMock} />); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| const downloadButton = screen.getByRole("button", { name: /Download PNG/i }); | ||||||||||||||||||||||||||||||||||||||
| await userEvent.click(downloadButton); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| expect(handleDownloadMock).toHaveBeenCalledTimes(1); | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vitest.config.tsですでにenvironment: "jsdom"がグローバル設定されているため、このファイル先頭の// @vitest-environment jsdomディレクティブは冗長です。削除することで設定の重複を避けられます。Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!