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

ComposerPendingUserInputPanel.permissions.test.tsx

93 lines2.9 KB

1import { RuntimeRequestId } from "@t3tools/contracts";
2import { act, type ReactNode } from "react";
3import { create, type ReactTestRenderer } from "react-test-renderer";
4import { expect, it, vi } from "vite-plus/test";
5​
6vi.mock("../ui/collapsible", () => {
7 const Children = ({ children }: { children: ReactNode }) => <>{children}</>;
8 return {
9 Collapsible: Children,
10 CollapsiblePanel: Children,
11 CollapsibleTrigger: Children,
12 };
13});
14​
15import { ComposerPendingUserInputPanel } from "./ComposerPendingUserInputPanel";
16​
17it("cancels an answer's pending auto-submit on revocation and resumes after a new grant", async () => {
18 vi.useFakeTimers();
19 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
20 vi.stubGlobal("window", {
21 setTimeout: globalThis.setTimeout,
22 clearTimeout: globalThis.clearTimeout,
23 });
24 vi.stubGlobal("document", { addEventListener: vi.fn(), removeEventListener: vi.fn() });
25 const onToggleOption = vi.fn();
26 const onAdvance = vi.fn();
27 const onDismiss = vi.fn();
28 const prompt = {
29 requestId: RuntimeRequestId.make("scoped-answer"),
30 createdAt: "2026-09-05T00:00:00.000Z",
31 dismissible: false,
32 responseCapability: "live" as const,
33 questions: [
34 {
35 id: "approach",
36 header: "Approach",
37 question: "Which approach?",
38 options: [{ label: "Incremental", description: "One module at a time" }],
39 multiSelect: false,
40 },
41 ],
42 };
43 const panel = (disabled: boolean) => (
44 <ComposerPendingUserInputPanel
45 pendingUserInputs={[prompt]}
46 disabled={disabled}
47 respondingRequestIds={[]}
48 answers={{}}
49 questionIndex={0}
50 onToggleOption={onToggleOption}
51 onAdvance={onAdvance}
52 onDismiss={onDismiss}
53 />
54 );
55 let renderer: ReactTestRenderer | undefined;
56 const chooseOption = () => {
57 const option = renderer!.root
58 .findAllByType("button")
59 .find((button) =>
60 button.findAllByType("span").some((span) => span.children.includes("Incremental")),
61 );
62 expect(option).toBeDefined();
63 option!.props.onClick();
64 };
65 try {
66 await act(async () => {
67 renderer = create(panel(false));
68 });
69 await act(async () => chooseOption());
70 expect(onToggleOption).toHaveBeenCalledWith("approach", "Incremental");
71 expect(onAdvance).not.toHaveBeenCalled();
72​
73 await act(async () => renderer!.update(panel(true)));
74 await act(async () => {
75 chooseOption();
76 vi.runAllTimers();
77 });
78 expect(onToggleOption).toHaveBeenCalledTimes(1);
79 expect(onAdvance).not.toHaveBeenCalled();
80​
81 await act(async () => renderer!.update(panel(false)));
82 await act(async () => {
83 chooseOption();
84 vi.runAllTimers();
85 });
86 expect(onToggleOption).toHaveBeenCalledTimes(2);
87 expect(onAdvance).toHaveBeenCalledTimes(1);
88 } finally {
89 await act(async () => renderer?.unmount());
90 vi.unstubAllGlobals();
91 vi.useRealTimers();
92 }
93});