@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 160 lines 5.15 KB
Raw
1 import { act, type ReactNode } from "react";
2 import { createRoot, type Root } from "react-dom/client";
3 import { afterEach, expect, it, vi } from "vite-plus/test";
4
5 // Every click handler a button was rendered with, so a test can deliver a
6 // native event that was dispatched against an older render.
7 const renderedClicks = vi.hoisted(() => [] as Array<() => void>);
8
9 vi.mock("@expo/ui/jetpack-compose", () => {
10 const Slot = (props: { readonly children?: ReactNode }) => <div>{props.children}</div>;
11 const Button = (props: { readonly children?: ReactNode; readonly onClick?: () => void }) => {
12 if (props.onClick) renderedClicks.push(props.onClick);
13 return <button onClick={props.onClick}>{props.children}</button>;
14 };
15 return {
16 AlertDialog: Object.assign(Slot, {
17 Title: Slot,
18 Text: Slot,
19 DismissButton: Slot,
20 ConfirmButton: Slot,
21 }),
22 Button,
23 OutlinedButton: Button,
24 Column: Slot,
25 FlowRow: Slot,
26 Icon: () => null,
27 OutlinedTextField: Object.assign(Slot, { Label: Slot, Placeholder: Slot }),
28 Text: Slot,
29 useNativeState: (value: unknown) => ({ get: () => value }),
30 };
31 });
32 vi.mock("./NativeThemeHost.android", () => ({
33 NativeThemeHost: (props: { readonly children?: ReactNode }) => <div>{props.children}</div>,
34 }));
35 vi.mock("../../features/settings/appearance/AppearancePreferencesProvider", () => ({
36 useAppearancePreferences: () => ({ themeVariables: {} }),
37 }));
38 vi.mock("../../features/settings/appearance/useScaledTextRole", () => ({
39 useScaledTextRole: () => ({}),
40 }));
41 vi.mock("../../lib/mobileTheme", () => ({ themeColorWithContrast: () => "#ffffff" }));
42
43 import * as Dialog from "./Dialog.android";
44
45 const roots: Root[] = [];
46
47 function mountHost() {
48 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
49 const container = document.createElement("div");
50 document.body.append(container);
51 const root = createRoot(container);
52 roots.push(root);
53 act(() => root.render(<Dialog.Host />));
54 return { root, container };
55 }
56
57 afterEach(() => {
58 for (const root of roots.splice(0)) act(() => root.unmount());
59 renderedClicks.length = 0;
60 document.body.innerHTML = "";
61 });
62
63 it("keeps presenting dialogs after an older root's Host unmounts", () => {
64 // Android activity replacement mounts the new root before the old one stops.
65 const previous = mountHost();
66 const current = mountHost();
67 roots.splice(roots.indexOf(previous.root), 1);
68 act(() => previous.root.unmount());
69
70 act(() => Dialog.alert("Saved", undefined, [{ text: "OK" }]));
71
72 expect(current.container.textContent).toContain("Saved");
73 });
74
75 it("presents a dialog requested before any Host mounted", () => {
76 Dialog.alert("Could not import shared content", undefined, [{ text: "Dismiss" }]);
77
78 const { container } = mountHost();
79
80 expect(container.textContent).toContain("Could not import shared content");
81 });
82
83 it("keeps a dialog raised while the activity is replaced", () => {
84 const previous = mountHost();
85 act(() => Dialog.alert("Shared content imported", undefined, [{ text: "OK" }]));
86 const current = mountHost();
87 roots.splice(roots.indexOf(previous.root), 1);
88 act(() => previous.root.unmount());
89
90 expect(current.container.textContent).toContain("Shared content imported");
91 });
92
93 it.each([
94 {
95 kind: "alert",
96 present: (onClose: () => void) =>
97 Dialog.alert("Older", undefined, [{ text: "OK" }], { onDismiss: onClose }),
98 },
99 {
100 kind: "confirm",
101 present: (onClose: () => void) =>
102 Dialog.confirm({
103 title: "Remove project",
104 confirmText: "Remove",
105 onConfirm: () => {},
106 onCancel: onClose,
107 }),
108 },
109 {
110 kind: "text-input",
111 present: (onClose: () => void) =>
112 Dialog.prompt({
113 title: "Rename",
114 initialValue: "",
115 confirmText: "Save",
116 onConfirm: () => {},
117 onCancel: onClose,
118 }),
119 },
120 ])("tells a replaced $kind it was closed without an answer", ({ present }) => {
121 mountHost();
122 const onClose = vi.fn();
123 act(() => present(onClose));
124 act(() => Dialog.alert("Newer", undefined, [{ text: "OK" }]));
125
126 expect(onClose).toHaveBeenCalledOnce();
127 });
128
129 it.each([
130 { action: "confirm", clickIndex: -1 },
131 { action: "cancel", clickIndex: -2 },
132 ])("ignores a late $action click from a replaced confirmation", ({ clickIndex }) => {
133 const { container } = mountHost();
134 const onConfirm = vi.fn();
135 const onCancel = vi.fn();
136 act(() =>
137 Dialog.confirm({ title: "Remove project", confirmText: "Remove", onConfirm, onCancel }),
138 );
139 const lateClick = renderedClicks.at(clickIndex)!;
140 act(() => Dialog.alert("Newer", undefined, [{ text: "OK" }]));
141
142 act(() => lateClick());
143
144 expect(onConfirm).not.toHaveBeenCalled();
145 expect(onCancel).toHaveBeenCalledOnce();
146 expect(container.textContent).toContain("Newer");
147 });
148
149 it("ignores a late action from a dialog that a newer one replaced", () => {
150 const { container } = mountHost();
151 const olderAction = vi.fn();
152 act(() => Dialog.alert("Older", undefined, [{ text: "OK", onPress: olderAction }]));
153 const olderClick = renderedClicks.at(-1)!;
154 act(() => Dialog.alert("Newer", undefined, [{ text: "OK" }]));
155
156 act(() => olderClick());
157
158 expect(olderAction).toHaveBeenCalledOnce();
159 expect(container.textContent).toContain("Newer");
160 });