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

PreviewEmptyState.test.tsx

89 lines2.6 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { renderToStaticMarkup } from "react-dom/server";
3import { describe, expect, it, vi } from "vite-plus/test";
4​
5const mocks = vi.hoisted(() => ({
6 servers: [] as Array<{
7 host: string;
8 port: number;
9 url: string;
10 requestedUrl: string;
11 processName: string | null;
12 pid: number | null;
13 terminal: null;
14 source: "scanner";
15 listening: boolean;
16 }>,
17}));
18​
19vi.mock("./useDiscoveredLocalServers", () => ({
20 useDiscoveredLocalServers: () => mocks.servers,
21}));
22​
23import { PreviewEmptyState } from "./PreviewEmptyState";
24​
25const environmentId = EnvironmentId.make("env-1");
26​
27function server(port: number) {
28 return {
29 host: "localhost",
30 port,
31 url: `http://localhost:${port}`,
32 requestedUrl: `http://localhost:${port}`,
33 processName: "node",
34 pid: 1,
35 terminal: null,
36 source: "scanner" as const,
37 listening: true,
38 };
40​
41function render(recentEntries: Array<{ url: string; lastVisitedAt: number; title?: string }>) {
42 return renderToStaticMarkup(
43 <PreviewEmptyState
44 environmentId={environmentId}
45 recentEntries={recentEntries}
46 onRemoveRecent={() => undefined}
47 onOpenUrl={() => undefined}
48 />,
49 );
51​
52describe("PreviewEmptyState", () => {
53 it("renders a history entry in both groups when its host:port matches a live server", () => {
54 mocks.servers = [server(5173)];
55 const html = render([
56 { url: "https://myapp.test/admin#users", lastVisitedAt: Date.now(), title: "Admin" },
57 { url: "http://localhost:5173/", lastVisitedAt: Date.now(), title: "Recent Local" },
58 ]);
59 expect(html).toContain("Recently used");
60 expect(html).toContain("Local servers");
61 expect(html).toContain("myapp.test/admin#users");
62 expect(html).toContain("Admin");
63 expect(html).toContain("Recent Local");
64 expect(html).toContain("node");
65 });
66​
67 it("renders only the recents group when no servers are found", () => {
68 mocks.servers = [];
69 const html = render([{ url: "https://myapp.test/", lastVisitedAt: 0 }]);
70 expect(html).toContain("Recently used");
71 expect(html).not.toContain("Local servers");
72 });
73​
74 it("keeps the original empty state when both groups are empty", () => {
75 mocks.servers = [];
76 const html = render([]);
77 expect(html).toContain("No preview yet");
78 });
79​
80 it("renders an out-of-range lastVisitedAt entry without throwing", () => {
81 mocks.servers = [];
82 let html = "";
83 expect(() => {
84 html = render([{ url: "https://myapp.test/", lastVisitedAt: 1e20 }]);
85 }).not.toThrow();
86 expect(html).toContain("myapp.test");
87 expect(html).toContain("Remove");
88 });
89});