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

copyTextWithHaptic.test.ts

90 lines2.9 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} from "./copyTextWithHaptic";
26​
27describe("copyTextWithHaptic", () => {
28 beforeEach(() => {
29 vi.clearAllMocks();
30 mocks.setStringAsync.mockReturnValue(new Promise<void>(() => undefined));
31 mocks.impactAsync.mockResolvedValue(undefined);
32 mocks.selectionAsync.mockResolvedValue(undefined);
33 });
34​
35 afterEach(() => {
36 vi.restoreAllMocks();
37 });
38​
39 it("triggers haptic feedback without waiting for the clipboard promise", () => {
40 copyTextWithHaptic("trace-123");
41​
42 expect(mocks.setStringAsync).toHaveBeenCalledWith("trace-123");
43 expect(mocks.impactAsync).toHaveBeenCalledWith("light");
44 });
45​
46 it("preserves selection feedback for thread work rows", () => {
47 copyTextWithHaptic("work output", {
48 target: "thread-work-row",
49 feedback: "selection",
50 });
51​
52 expect(mocks.setStringAsync).toHaveBeenCalledWith("work output");
53 expect(mocks.selectionAsync).toHaveBeenCalledOnce();
54 expect(mocks.impactAsync).not.toHaveBeenCalled();
55 });
56​
57 it("reports structured failures without including clipboard contents", async () => {
58 const clipboardCause = new Error("native clipboard failure");
59 const hapticCause = new Error("native haptic failure");
60 const consoleError = vi.spyOn(console, "error").mockImplementation(() => undefined);
61 mocks.setStringAsync.mockRejectedValueOnce(clipboardCause);
62 mocks.impactAsync.mockRejectedValueOnce(hapticCause);
63​
64 copyTextWithHaptic("secret clipboard contents", { target: "connection-trace-id" });
65​
66 await vi.waitFor(() => {
67 expect(consoleError).toHaveBeenCalledTimes(2);
68 });
69​
70 const failures = consoleError.mock.calls.map(([failure]) => failure);
71 const clipboardError = failures.find(
72 (failure) => failure instanceof CopyTextClipboardWriteError,
73 );
74 expect(clipboardError).toBeInstanceOf(CopyTextClipboardWriteError);
75 expect(clipboardError).toMatchObject({
76 target: "connection-trace-id",
77 cause: clipboardCause,
78 });
79 expect((clipboardError as Error).message).not.toContain("secret clipboard contents");
80​
81 const hapticError = failures.find((failure) => failure instanceof CopyTextHapticFeedbackError);
82 expect(hapticError).toBeInstanceOf(CopyTextHapticFeedbackError);
83 expect(hapticError).toMatchObject({
84 target: "connection-trace-id",
85 feedback: "light-impact",
86 cause: hapticCause,
87 });
88 expect((hapticError as Error).message).not.toContain("secret clipboard contents");
89 });
90});