Open History to see the last commit
1import { MessageId } from "@t3tools/contracts";
2import { act } from "react";
3import { createRoot, type Root } from "react-dom/client";
4import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
5
6import { AppAtomRegistryProvider } from "~/rpc/atomRegistry";
7import { openRevertConfirm, RevertConfirmHost } from "./ChatOverlayHosts";
8
9let root: Root;
10let container: HTMLDivElement;
11
12beforeEach(() => {
13 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
14 container = document.createElement("div");
15 document.body.append(container);
16 root = createRoot(container);
17});
18
19afterEach(async () => {
20 await act(() => root.unmount());
21 container.remove();
22 vi.unstubAllGlobals();
23});
24
25const messageId = MessageId.make("message-1");
26const dialog = () => document.querySelector('[role="alertdialog"]');
27
28describe("RevertConfirmHost", () => {
29 it("shows only its thread's request, confirms it once, and drops it when the thread changes", async () => {
30 const onConfirm = vi.fn();
31 const host = (threadKey: string) => (
32 <AppAtomRegistryProvider>
33 <RevertConfirmHost threadKey={threadKey} onConfirm={onConfirm} />
34 </AppAtomRegistryProvider>
35 );
36 await act(() => root.render(host("env:a")));
37
38 await act(() => openRevertConfirm("env:b", { turnCount: 2, messageId }));
39 expect(dialog()).toBeNull();
40
41 await act(() => openRevertConfirm("env:a", { turnCount: 2, messageId }));
42 const keep = [...(dialog()?.querySelectorAll("button") ?? [])].find(
43 (button) => button.textContent === "Revert and keep changes",
44 );
45 await act(() => keep?.click());
46 expect(onConfirm).toHaveBeenCalledExactlyOnceWith(2, messageId, false);
47 await vi.waitFor(() => expect(dialog()).toBeNull());
48
49 await act(() => openRevertConfirm("env:a", { turnCount: 3, messageId }));
50 expect(dialog()).not.toBeNull();
51 await act(() => root.render(host("env:b")));
52 await vi.waitFor(() => expect(dialog()).toBeNull());
53 });
54});