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

usePermissionStatus.browser.test.tsx

103 lines3.4 KB

1import { act, useEffect } from "react";
2import { createRoot, type Root } from "react-dom/client";
3import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
4​
5import { usePermissionStatus } from "./usePermissionStatus";
6​
7type Status = { screen: boolean; accessibility: boolean };
8const check = vi.fn<() => Promise<Status>>();
9​
10function deferred<T>() {
11 let resolve!: (value: T) => void;
12 const promise = new Promise<T>((done) => {
13 resolve = done;
14 });
15 return { promise, resolve };
17​
18let root: Root | null;
19let current: ReturnType<typeof usePermissionStatus<keyof Status>>;
20let visibility: DocumentVisibilityState;
21​
22function Probe() {
23 const status = usePermissionStatus(check, { screen: false, accessibility: false });
24 useEffect(() => {
25 current = status;
26 });
27 return null;
29​
30beforeEach(() => {
31 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
32 vi.useFakeTimers();
33 visibility = "visible";
34 Object.defineProperty(document, "visibilityState", {
35 configurable: true,
36 get: () => visibility,
37 });
38 check.mockReset().mockResolvedValue({ screen: false, accessibility: false });
39 root = null;
40});
41afterEach(async () => {
42 await unmount();
43 vi.useRealTimers();
44 vi.unstubAllGlobals();
45 Reflect.deleteProperty(document, "visibilityState");
46});
47​
48async function start() {
49 root = createRoot(document.createElement("div"));
50 await act(async () => root!.render(<Probe />));
52async function unmount() {
53 const mounted = root;
54 root = null;
55 if (mounted) await act(async () => mounted.unmount());
57const tick = (ms: number) => act(() => vi.advanceTimersByTimeAsync(ms));
58​
59it("unlocks Continue only for required grants and relocks on revocation", async () => {
60 await start();
61 expect(current.isReady(["screen"])).toBe(false);
62 check.mockResolvedValue({ screen: true, accessibility: false });
63 await tick(1500);
64 expect(current.isReady(["screen"])).toBe(true);
65 expect(current.isReady(["screen", "accessibility"])).toBe(false);
66 check.mockResolvedValue({ screen: true, accessibility: true });
67 await tick(1500);
68 expect(current.isReady(["screen", "accessibility"])).toBe(true);
69 check.mockResolvedValue({ screen: false, accessibility: true });
70 await act(async () => window.dispatchEvent(new Event("focus")));
71 expect(current.isReady(["screen"])).toBe(false);
72});
73​
74it("does not overlap checks and discards completion after closing", async () => {
75 const pending = deferred<Status>();
76 check.mockReturnValue(pending.promise);
77 await start();
78 await tick(4500);
79 window.dispatchEvent(new Event("focus"));
80 expect(check).toHaveBeenCalledTimes(1);
81 const last = current;
82 await unmount();
83 await act(async () => pending.resolve({ screen: true, accessibility: true }));
84 expect(last.isReady(["screen"])).toBe(false);
85 expect(vi.getTimerCount()).toBe(0);
86});
87​
88it("pauses in the background and blocks stale grants after a check failure", async () => {
89 check.mockResolvedValue({ screen: true, accessibility: true });
90 await start();
91 expect(current.isReady(["screen"])).toBe(true);
92 visibility = "hidden";
93 await tick(3000);
94 expect(check).toHaveBeenCalledTimes(1);
95 check.mockRejectedValue(new Error("IPC unavailable"));
96 visibility = "visible";
97 await act(async () => document.dispatchEvent(new Event("visibilitychange")));
98 expect(current.isReady(["screen"])).toBe(false);
99 expect(current.error).not.toBeNull();
100 check.mockResolvedValue({ screen: true, accessibility: true });
101 await tick(1500);
102 expect(current.isReady(["screen"])).toBe(true);
103});