forked from Lilly-Protocol/lily-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy-button.test.tsx
More file actions
53 lines (42 loc) · 1.52 KB
/
Copy pathcopy-button.test.tsx
File metadata and controls
53 lines (42 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { CopyButton } from "./copy-button";
describe("CopyButton", () => {
const originalClipboard = navigator.clipboard;
afterEach(() => {
vi.restoreAllMocks();
Object.defineProperty(navigator, "clipboard", {
configurable: true,
value: originalClipboard,
});
});
it("copies text and shows the confirmation state", async () => {
const writeText = vi.fn().mockResolvedValue(undefined);
Object.defineProperty(navigator, "clipboard", {
configurable: true,
value: { writeText },
});
render(<CopyButton text="LILLY_API_KEY" />);
await userEvent.click(screen.getByRole("button", { name: /copy/i }));
expect(writeText).toHaveBeenCalledWith("LILLY_API_KEY");
expect(
screen.getByRole("button", { name: /copied/i }),
).toBeInTheDocument();
});
it("shows a failure state when clipboard copy fails", async () => {
const writeText = vi.fn().mockRejectedValue(new Error("denied"));
Object.defineProperty(navigator, "clipboard", {
configurable: true,
value: { writeText },
});
Object.defineProperty(document, "execCommand", {
configurable: true,
value: vi.fn().mockReturnValue(false),
});
render(<CopyButton text="blocked" />);
await userEvent.click(screen.getByRole("button", { name: /copy/i }));
expect(
screen.getByRole("button", { name: /copy failed/i }),
).toBeInTheDocument();
});
});