@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 50 lines 1.94 KB
Raw
1 import { describe, expect, it, vi } from "vite-plus/test";
2
3 import { render } from "../../../test/render";
4 import { usePermissionStatus } from "./usePermissionStatus";
5
6 function PermissionProbe({
7 check,
8 label,
9 enabled = true,
10 }: {
11 readonly check: () => Promise<{ screen: boolean }>;
12 readonly label: string;
13 readonly enabled?: boolean;
14 }) {
15 const { status } = usePermissionStatus(async () => check(), { screen: false }, enabled);
16 return (
17 <p>
18 {label}: {status.screen ? "granted" : "missing"}
19 </p>
20 );
21 }
22
23 describe("usePermissionStatus", () => {
24 it("does not start a new check when the component re-renders", async () => {
25 // A check that never settles keeps the interval from adding calls, so any
26 // extra call can only come from the subscription restarting.
27 const check = vi.fn(() => new Promise<{ screen: boolean }>(() => {}));
28 await using screen = await render(<PermissionProbe check={check} label="first" />, {
29 router: false, // the hook does not touch routing
30 });
31 await screen.rerender(<PermissionProbe check={check} label="second" />);
32 await screen.rerender(<PermissionProbe check={check} label="third" />);
33
34 await expect.element(screen.getByText("third: missing")).toBeInTheDocument();
35 expect(check).toHaveBeenCalledTimes(1);
36 });
37
38 it("checks with the latest check after the caller passes a new one", async () => {
39 const first = vi.fn(() => new Promise<{ screen: boolean }>(() => {}));
40 const second = vi.fn(async () => ({ screen: true }));
41 await using screen = await render(<PermissionProbe check={first} label="probe" />, {
42 router: false, // the hook does not touch routing
43 });
44 await screen.rerender(<PermissionProbe check={second} label="probe" enabled={false} />);
45 await screen.rerender(<PermissionProbe check={second} label="probe" />);
46
47 await expect.element(screen.getByText("probe: granted")).toBeInTheDocument();
48 expect(first).toHaveBeenCalledTimes(1);
49 });
50 });