Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/components/ActionButtons.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// @vitest-environment jsdom
import { render, screen } from "@testing-library/react";
Comment on lines +1 to +2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 vitest.config.ts ですでに environment: "jsdom" がグローバル設定されているため、このファイル先頭の // @vitest-environment jsdom ディレクティブは冗長です。削除することで設定の重複を避けられます。

Suggested change
// @vitest-environment jsdom
import { render, screen } from "@testing-library/react";
import { render, screen } from "@testing-library/react";
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/components/ActionButtons.test.tsx
Line: 1-2

Comment:
`vitest.config.ts` ですでに `environment: "jsdom"` がグローバル設定されているため、このファイル先頭の `// @vitest-environment jsdom` ディレクティブは冗長です。削除することで設定の重複を避けられます。

```suggestion
import { render, screen } from "@testing-library/react";
```

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!

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 describe ブロックのトップレベルで vi.fn() を一度だけ生成しているため、各テスト間でモックがリセットされません。現在のテストコードでは defaultProps.handleCopy / handleDownload に対して直接アサーションしていないため実害はありませんが、将来テストを追加した際に呼び出し回数が蓄積されてテストが誤って失敗するリスクがあります。beforeEach でモックをクリアするのが望ましいです。

Suggested change
describe("ActionButtons", () => {
const defaultProps = {
handleCopy: vi.fn(),
handleDownload: vi.fn(),
previewUrl: "https://example.com/preview.png",
copyStatus: "idle" as const,
};
describe("ActionButtons", () => {
const defaultProps = {
handleCopy: vi.fn(),
handleDownload: vi.fn(),
previewUrl: "https://example.com/preview.png",
copyStatus: "idle" as const,
};
beforeEach(() => {
vi.clearAllMocks();
});
Prompt To Fix With AI
This 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 copyStatus="error" の状態がテストされていない

copyStatus の型は "idle" | "copied" | "error" の3値ですが、"error" ケースのテストが存在しません。現在のコンポーネント実装では "error""idle" と同じ表示になりますが、将来の変更でエラー用の表示が追加された際にカバレッジが不足します。少なくとも "error" の場合に "Copy Image" ボタンが表示されることを確認するテストの追加を推奨します。

Prompt To Fix With AI
This 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);
});
});
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default defineConfig({
"src/lib/**/*.ts",
"src/hooks/**/*.ts",
"src/components/ThemeController.tsx",
"src/components/ActionButtons.tsx",
"src/components/ReadmeCardUrlSection.tsx",
"src/components/BusinessCard.tsx",
"src/components/LanguageChart.tsx",
Expand Down
Loading