forked from Lilly-Protocol/lily-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy-to-clipboard.test.ts
More file actions
70 lines (57 loc) · 2.47 KB
/
Copy pathcopy-to-clipboard.test.ts
File metadata and controls
70 lines (57 loc) · 2.47 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { copyToClipboard } from "./copy-to-clipboard";
describe("copyToClipboard", () => {
beforeEach(() => {
// Mock navigator.clipboard
Object.assign(navigator, {
clipboard: {
writeText: vi.fn().mockResolvedValue(undefined),
},
});
});
afterEach(() => {
vi.restoreAllMocks();
});
it("should copy text using navigator.clipboard when available", async () => {
const result = await copyToClipboard("test text");
expect(result.success).toBe(true);
expect(navigator.clipboard.writeText).toHaveBeenCalledWith("test text");
});
it("should call onSuccess callback on successful copy", async () => {
const onSuccess = vi.fn();
await copyToClipboard("test text", { onSuccess });
expect(onSuccess).toHaveBeenCalled();
});
it("should return success false and call onError when clipboard fails", async () => {
const error = new Error("Clipboard failed");
vi.mocked(navigator.clipboard.writeText).mockRejectedValue(error);
const onError = vi.fn();
const result = await copyToClipboard("test text", { onError });
expect(result.success).toBe(false);
expect(result.error).toBe(error);
expect(onError).toHaveBeenCalledWith(error);
});
it("should fallback to execCommand when clipboard API is unavailable", async () => {
// Remove clipboard API
Object.assign(navigator, { clipboard: undefined });
// Mock execCommand which may not exist in JSDOM
document.execCommand = vi.fn().mockReturnValue(true);
// Mock document methods
const mockTextarea = {
value: "",
style: {} as Record<string, string>,
select: vi.fn(),
};
const createElementSpy = vi.spyOn(document, "createElement").mockReturnValue(mockTextarea as unknown as HTMLTextAreaElement);
const appendChildSpy = vi.spyOn(document.body, "appendChild").mockImplementation((node) => node);
const removeChildSpy = vi.spyOn(document.body, "removeChild").mockImplementation((node) => node);
const result = await copyToClipboard("fallback text");
expect(result.success).toBe(true);
expect(createElementSpy).toHaveBeenCalledWith("textarea");
expect(mockTextarea.value).toBe("fallback text");
expect(mockTextarea.select).toHaveBeenCalled();
expect(document.execCommand).toHaveBeenCalledWith("copy");
expect(appendChildSpy).toHaveBeenCalled();
expect(removeChildSpy).toHaveBeenCalled();
});
});