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

PreviewEmptyState.browser.test.tsx

113 lines3.7 KB

1import { EnvironmentId, ThreadId } 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 type { PreviewableServer } from "./useDiscoveredLocalServers";
7​
8const mocks = vi.hoisted(() => ({ servers: [] as PreviewableServer[] }));
9​
10vi.mock(import("./useDiscoveredLocalServers"), async (original) => ({
11 ...(await original()),
12 useDiscoveredLocalServers: () => mocks.servers,
13}));
14​
15import { PreviewEmptyState } from "./PreviewEmptyState";
16​
17const environmentId = EnvironmentId.make("env-1");
18const threadRef = { environmentId, threadId: ThreadId.make("thread-1") };
19const onOpenUrl = vi.fn();
20const onRemoveRecent = vi.fn();
21​
22let root: Root;
23let container: HTMLDivElement;
24​
25function server(port: number): PreviewableServer {
26 return {
27 host: "localhost",
28 port,
29 url: `http://localhost:${port}`,
30 requestedUrl: `http://localhost:${port}`,
31 processName: "node",
32 pid: 1,
33 terminal: null,
34 source: "scanner",
35 } as PreviewableServer;
37​
38beforeEach(() => {
39 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
40 vi.clearAllMocks();
41 mocks.servers = [];
42 container = document.createElement("div");
43 document.body.append(container);
44 root = createRoot(container);
45});
46​
47afterEach(async () => {
48 await act(async () => root.unmount());
49 container.remove();
50 vi.unstubAllGlobals();
51});
52​
53async function render(
54 recentEntries: Array<{ url: string; lastVisitedAt: number; title?: string }>,
55) {
56 await act(async () =>
57 root.render(
58 <PreviewEmptyState
59 threadRef={threadRef}
60 environmentId={environmentId}
61 recentEntries={recentEntries}
62 onRemoveRecent={onRemoveRecent}
63 onOpenUrl={onOpenUrl}
64 />,
65 ),
66 );
68​
69function button(name: string) {
70 const match = [...container.querySelectorAll("button")].find(
71 (element) => element.getAttribute("aria-label") === name || element.textContent?.includes(name),
72 );
73 expect(match, `button ${name}`).toBeDefined();
74 return match!;
76​
77describe("PreviewEmptyState", () => {
78 it("lists a recent entry and a live server matching its host:port, and opens or forgets entries", async () => {
79 mocks.servers = [server(5173)];
80 await render([
81 { url: "https://myapp.test/admin#users", lastVisitedAt: Date.now(), title: "Admin" },
82 { url: "http://localhost:5173/", lastVisitedAt: Date.now(), title: "Recent Local" },
83 ]);
84 const text = container.textContent;
85 expect(text).toContain("Recently used");
86 expect(text).toContain("Local servers");
87 expect(text).toContain("Recent Local");
88 expect(text).toContain("node");
89​
90 await act(async () => button("Admin").click());
91 expect(onOpenUrl).toHaveBeenCalledExactlyOnceWith("https://myapp.test/admin#users");
92​
93 await act(async () => button("Remove myapp.test/admin#users from history").click());
94 expect(onRemoveRecent).toHaveBeenCalledExactlyOnceWith("https://myapp.test/admin#users");
95 });
96​
97 it("shows only the recents group when no servers are found", async () => {
98 await render([{ url: "https://myapp.test/", lastVisitedAt: 0 }]);
99 expect(container.textContent).toContain("Recently used");
100 expect(container.textContent).not.toContain("Local servers");
101 });
102​
103 it("keeps the original empty state when both groups are empty", async () => {
104 await render([]);
105 expect(container.textContent).toContain("No preview yet");
106 });
107​
108 it("shows an entry with an out-of-range lastVisitedAt instead of crashing", async () => {
109 await render([{ url: "https://myapp.test/", lastVisitedAt: 1e20 }]);
110 expect(container.textContent).toContain("myapp.test");
111 button("Remove myapp.test from history");
112 });
113});