Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

copyTextWithHaptic.test.ts

104 lines3.5 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2​
3const mocks = vi.hoisted(() => ({
4 impactAsync: vi.fn(),
5 selectionAsync: vi.fn(),
6 setStringAsync: vi.fn(),
7}));
8​
9vi.mock("expo-clipboard", () => ({
10 setStringAsync: mocks.setStringAsync,
11}));
12​
13vi.mock("expo-haptics", () => ({
14 ImpactFeedbackStyle: {
15 Light: "light",
16 },
17 impactAsync: mocks.impactAsync,
18 selectionAsync: mocks.selectionAsync,
19}));
20​
21import {
22 CopyTextClipboardWriteError,
23 CopyTextHapticFeedbackError,
24 copyTextWithHaptic,
25 tryCopyTextWithHaptic,
26} from "./copyTextWithHaptic";
27​
28describe("copyTextWithHaptic", () => {
29 beforeEach(() => {
30 vi.clearAllMocks();
31 mocks.setStringAsync.mockReturnValue(new Promise<void>(() => undefined));
32 mocks.impactAsync.mockResolvedValue(undefined);
33 mocks.selectionAsync.mockResolvedValue(undefined);
34 });
35​
36 afterEach(() => {
37 vi.restoreAllMocks();
38 });
39​
40 it("triggers haptic feedback without waiting for the clipboard promise", () => {
41 copyTextWithHaptic("trace-123");
42​
43 expect(mocks.setStringAsync).toHaveBeenCalledWith("trace-123");
44 expect(mocks.impactAsync).toHaveBeenCalledWith("light");
45 });
46​
47 it("preserves selection feedback for thread work rows", () => {
48 copyTextWithHaptic("work output", {
49 target: "thread-work-row",
50 feedback: "selection",
51 });
52​
53 expect(mocks.setStringAsync).toHaveBeenCalledWith("work output");
54 expect(mocks.selectionAsync).toHaveBeenCalledOnce();
55 expect(mocks.impactAsync).not.toHaveBeenCalled();
56 });
57​
58 it("reports whether the clipboard write succeeded", async () => {
59 mocks.setStringAsync.mockResolvedValueOnce(undefined);
60​
61 await expect(tryCopyTextWithHaptic("thread-123")).resolves.toBe(true);
62 });
63​
64 it("returns false when the clipboard write fails", async () => {
65 vi.spyOn(console, "error").mockImplementation(() => undefined);
66 mocks.setStringAsync.mockRejectedValueOnce(new Error("native clipboard failure"));
67​
68 await expect(tryCopyTextWithHaptic("thread-123")).resolves.toBe(false);
69 });
70​
71 it("reports structured failures without including clipboard contents", async () => {
72 const clipboardCause = new Error("native clipboard failure");
73 const hapticCause = new Error("native haptic failure");
74 const consoleError = vi.spyOn(console, "error").mockImplementation(() => undefined);
75 mocks.setStringAsync.mockRejectedValueOnce(clipboardCause);
76 mocks.impactAsync.mockRejectedValueOnce(hapticCause);
77​
78 copyTextWithHaptic("secret clipboard contents", { target: "connection-trace-id" });
79​
80 await vi.waitFor(() => {
81 expect(consoleError).toHaveBeenCalledTimes(2);
82 });
83​
84 const failures = consoleError.mock.calls.map(([failure]) => failure);
85 const clipboardError = failures.find(
86 (failure) => failure instanceof CopyTextClipboardWriteError,
87 );
88 expect(clipboardError).toBeInstanceOf(CopyTextClipboardWriteError);
89 expect(clipboardError).toMatchObject({
90 target: "connection-trace-id",
91 cause: clipboardCause,
92 });
93 expect((clipboardError as Error).message).not.toContain("secret clipboard contents");
94​
95 const hapticError = failures.find((failure) => failure instanceof CopyTextHapticFeedbackError);
96 expect(hapticError).toBeInstanceOf(CopyTextHapticFeedbackError);
97 expect(hapticError).toMatchObject({
98 target: "connection-trace-id",
99 feedback: "light-impact",
100 cause: hapticCause,
101 });
102 expect((hapticError as Error).message).not.toContain("secret clipboard contents");
103 });
104});