-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 add test for navigator.clipboard.writeText failure #348
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 | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -114,6 +114,28 @@ describe("useCopyToClipboard", () => { | |||||||||||||||||||||||||||||||
| expect(result.current.copied).toBe(false); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| it("should catch and store error if navigator.clipboard.writeText rejects", async () => { | ||||||||||||||||||||||||||||||||
| const error = new Error("writeText failed"); | ||||||||||||||||||||||||||||||||
| vi.mocked(navigator.clipboard.writeText).mockRejectedValue(error); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // We also need to mock execCommand to return true so the fallback succeeds | ||||||||||||||||||||||||||||||||
| // or false so it fails, but we want to specifically ensure the catch block was hit. | ||||||||||||||||||||||||||||||||
| vi.mocked(document.execCommand).mockReturnValue(false); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const { result } = renderHook(() => useCopyToClipboard()); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| await act(async () => { | ||||||||||||||||||||||||||||||||
| await result.current.copyToClipboard("test text"); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| expect(navigator.clipboard.writeText).toHaveBeenCalledWith("test text"); | ||||||||||||||||||||||||||||||||
| expect(logger.error).toHaveBeenCalledWith( | ||||||||||||||||||||||||||||||||
| "Failed to copy", | ||||||||||||||||||||||||||||||||
| error, | ||||||||||||||||||||||||||||||||
| expect.any(Error) | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||
|
Comment on lines
+117
to
+137
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/hooks/__tests__/useCopyToClipboard.test.ts
Line: 117-137
Comment:
**既存テストと重複したシナリオ**
このテストのセットアップ(`writeText` が reject し `execCommand` が `false` を返す)は、直下の既存テスト "should log error if both clipboard and fallback fail"(行 139-155)と完全に同じコードパスを実行します。新テストが加えている差分は `logger.error` の第 2 引数をより具体的な `error` 参照で検証する点のみです。既存テストの該当アサーションを更新するだけで同等の検証が 1 つのテストにまとめられます。
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!
Comment on lines
+131
to
+137
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/hooks/__tests__/useCopyToClipboard.test.ts
Line: 131-137
Comment:
**`copied` 状態の最終値が未検証**
他の失敗系テストではすべて `expect(result.current.copied).toBe(false)` を確認していますが、このテストにはその検証がありません。エラーパスで `copied` が誤って `true` になるリグレッションがあった場合、このテストでは気づけません。
```suggestion
expect(navigator.clipboard.writeText).toHaveBeenCalledWith("test text");
expect(result.current.copied).toBe(false);
expect(logger.error).toHaveBeenCalledWith(
"Failed to copy",
error,
expect.any(Error)
);
});
```
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
Comment on lines
+117
to
+138
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. This new test is redundant with the existing test 'should log error if both clipboard and fallback fail' (lines 139-155). Both tests set up the exact same scenario where navigator.clipboard.writeText rejects and document.execCommand returns false. To avoid duplicate test cases and reduce maintenance overhead, we should remove this new test. If you want to verify that the exact error is passed to the logger, you can update the existing test to assert the specific error instead of using expect.any(Error). |
||||||||||||||||||||||||||||||||
| it("should log error if both clipboard and fallback fail", async () => { | ||||||||||||||||||||||||||||||||
| vi.mocked(navigator.clipboard.writeText).mockRejectedValue(new Error("Clipboard error")); | ||||||||||||||||||||||||||||||||
| vi.mocked(document.execCommand).mockReturnValue(false); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
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.
コメント "return true so the fallback succeeds or false so it fails" は
true/falseどちらでもよいように読めますが、直後のコードはfalseに固定されています。trueと解釈して変更するとlogger.errorが呼ばれずテストが失敗します。「fallback も失敗させることでエラーログを確認する」という意図に修正すると誤解が防げます。Prompt To Fix With AI