- Notifications
You must be signed in to change notification settings - Fork 0
fix(mockups): report Therapy Compass clipboard copy only on success#667
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { act, renderHook } from "@testing-library/react"; | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import { copyText, useClipboard } from "@/components/therapy-compass/use-clipboard"; | ||
| // The clipboard helper must only report success when the browser actually | ||
| // accepts the write. A rejected `writeText` (permission denied, lost focus, | ||
| // blocked gesture) must resolve to `false` without throwing, so callers never | ||
| // flip to "Copied" for a copy that didn't happen. | ||
| const originalClipboard = Object.getOwnPropertyDescriptor(globalThis.navigator, "clipboard"); | ||
| function setWriteText(writeText: ((text: string) => Promise<void>) | null) { | ||
| Object.defineProperty(globalThis.navigator, "clipboard", { | ||
| value: writeText ? { writeText } : undefined, | ||
| configurable: true, | ||
| writable: true, | ||
| }); | ||
| } | ||
| // Let the two-microtask copyText().then() chain settle inside act(). | ||
| async function settle() { | ||
| await act(async () => { | ||
| await new Promise((resolve) => setTimeout(resolve, 0)); | ||
| }); | ||
| } | ||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| if (originalClipboard) { | ||
| Object.defineProperty(globalThis.navigator, "clipboard", originalClipboard); | ||
| } else { | ||
| setWriteText(null); | ||
| } | ||
| }); | ||
| describe("copyText", () => { | ||
| it("resolves true and writes when the clipboard accepts the text", async () => { | ||
| const writeText = vi.fn().mockResolvedValue(undefined); | ||
| setWriteText(writeText); | ||
| await expect(copyText("hello")).resolves.toBe(true); | ||
| expect(writeText).toHaveBeenCalledWith("hello"); | ||
| }); | ||
| it("resolves false — never throws — when the write rejects", async () => { | ||
| setWriteText(vi.fn().mockRejectedValue(new Error("NotAllowedError"))); | ||
| await expect(copyText("hello")).resolves.toBe(false); | ||
| }); | ||
| it("resolves false for empty text without touching the clipboard", async () => { | ||
| const writeText = vi.fn().mockResolvedValue(undefined); | ||
| setWriteText(writeText); | ||
| await expect(copyText("")).resolves.toBe(false); | ||
| expect(writeText).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| describe("useClipboard", () => { | ||
| it("sets copied to the key only after a successful write", async () => { | ||
| setWriteText(vi.fn().mockResolvedValue(undefined)); | ||
| const { result } = renderHook(() => useClipboard()); | ||
| expect(result.current.copied).toBeNull(); | ||
| act(() => result.current.copy("hello", "step-1")); | ||
| await settle(); | ||
| expect(result.current.copied).toBe("step-1"); | ||
| }); | ||
| it("leaves copied unset when the write rejects", async () => { | ||
| setWriteText(vi.fn().mockRejectedValue(new Error("NotAllowedError"))); | ||
| const { result } = renderHook(() => useClipboard()); | ||
| act(() => result.current.copy("hello", "step-1")); | ||
| await settle(); | ||
| expect(result.current.copied).toBeNull(); | ||
| }); | ||
| it("ignores a stale out-of-order completion and keeps the most recent copy's key", async () => { | ||
| const resolvers: Array<() => void> = []; | ||
| setWriteText(vi.fn().mockImplementation(() => new Promise<void>((resolve) => resolvers.push(resolve)))); | ||
| const { result } = renderHook(() => useClipboard()); | ||
| act(() => result.current.copy("first", "k-first")); | ||
| act(() => result.current.copy("second", "k-second")); | ||
| expect(resolvers).toHaveLength(2); | ||
| // The most recent (second) write resolves first — its key wins. | ||
| await act(async () => { | ||
| resolvers[1](); | ||
| await Promise.resolve(); | ||
| }); | ||
| expect(result.current.copied).toBe("k-second"); | ||
| // The stale (first) write resolves later — it must NOT overwrite the newer feedback. | ||
| await act(async () => { | ||
| resolvers[0](); | ||
| await Promise.resolve(); | ||
| await Promise.resolve(); | ||
| }); | ||
| expect(result.current.copied).toBe("k-second"); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.