-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 [testing improvement] Add tests for ActionButtons component #324
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,106 @@ | ||
| import { render, screen, fireEvent } from "@testing-library/react"; | ||
| import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
| import { ActionButtons } from "./ActionButtons"; | ||
|
|
||
| describe("ActionButtons", () => { | ||
| const mockHandleCopy = vi.fn(); | ||
| const mockHandleDownload = vi.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it("renders both buttons", () => { | ||
| render( | ||
| <ActionButtons | ||
| handleCopy={mockHandleCopy} | ||
| handleDownload={mockHandleDownload} | ||
| previewUrl="https://example.com/image.png" | ||
| copyStatus="idle" | ||
| /> | ||
| ); | ||
|
|
||
| expect(screen.getByRole("button", { name: /copy image/i })).toBeInTheDocument(); | ||
| expect(screen.getByRole("button", { name: /download png/i })).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("disables buttons when previewUrl is null", () => { | ||
| render( | ||
| <ActionButtons | ||
| handleCopy={mockHandleCopy} | ||
| handleDownload={mockHandleDownload} | ||
| previewUrl={null} | ||
| copyStatus="idle" | ||
| /> | ||
| ); | ||
|
|
||
| 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 buttons when previewUrl is provided", () => { | ||
| render( | ||
| <ActionButtons | ||
| handleCopy={mockHandleCopy} | ||
| handleDownload={mockHandleDownload} | ||
| previewUrl="https://example.com/image.png" | ||
| copyStatus="idle" | ||
| /> | ||
| ); | ||
|
|
||
| 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 copy button is clicked", () => { | ||
| render( | ||
| <ActionButtons | ||
| handleCopy={mockHandleCopy} | ||
| handleDownload={mockHandleDownload} | ||
| previewUrl="https://example.com/image.png" | ||
| copyStatus="idle" | ||
| /> | ||
| ); | ||
|
|
||
| const copyButton = screen.getByRole("button", { name: /copy image/i }); | ||
|
Comment on lines
+62
to
+71
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: 62-71
Comment:
**`fireEvent` は実際のユーザー操作を正確に再現しない**
`fireEvent.click` は jsdom のイベントを直接ディスパッチするため、ブラウザネイティブの `disabled` 属性による抑制を迂回します。Testing Library の推奨は `@testing-library/user-event` の `userEvent.click` を使用することで、実際のユーザー操作をより忠実にシミュレートします。同様のパターンが 75〜84 行目にもあります。
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! |
||
| fireEvent.click(copyButton); | ||
|
|
||
| expect(mockHandleCopy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("calls handleDownload when download button is clicked", () => { | ||
| render( | ||
| <ActionButtons | ||
| handleCopy={mockHandleCopy} | ||
| handleDownload={mockHandleDownload} | ||
| previewUrl="https://example.com/image.png" | ||
| copyStatus="idle" | ||
| /> | ||
| ); | ||
|
|
||
| const downloadButton = screen.getByRole("button", { name: /download png/i }); | ||
| fireEvent.click(downloadButton); | ||
|
|
||
| expect(mockHandleDownload).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("shows 'Copied!' text and icon when copyStatus is 'copied'", () => { | ||
| render( | ||
| <ActionButtons | ||
| handleCopy={mockHandleCopy} | ||
| handleDownload={mockHandleDownload} | ||
| previewUrl="https://example.com/image.png" | ||
| copyStatus="copied" | ||
| /> | ||
| ); | ||
|
|
||
| expect(screen.getByRole("button", { name: /copied!/i })).toBeInTheDocument(); | ||
| expect(screen.queryByRole("button", { name: /copy image/i })).not.toBeInTheDocument(); | ||
| }); | ||
| }); | ||
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.
disabled状態でクリックしてもハンドラーが呼ばれないことを検証するテストが不足previewUrlがnullのとき、ボタンがdisabledになることは検証されていますが、その状態でクリックしてもhandleCopy/handleDownloadが呼ばれないことのネガティブテストが存在しません。userEventへの移行と合わせて追加すると、テストの信頼性がより高まります。Prompt To Fix With AI