Open History to see the last commit
1import { RegistryContext } from "@effect/atom-react";
2import { EnvironmentId } from "@t3tools/contracts";
3import { Atom, AtomRegistry } from "effect/reactivity";
4import { expect, it, vi } from "vite-plus/test";
5
6import { render } from "../../test/render";
7import { ProjectFavicon, type ProjectFaviconProject } from "./ProjectFavicon";
8
9const state = vi.hoisted(() => ({
10 favicon: undefined as Atom.Atom<string | null> | undefined,
11 refresh: (() => {}) as () => void,
12}));
13vi.mock(import("../state/assets"), () => ({
14 projectFaviconUrlAtom: () => {
15 if (!state.favicon) throw new Error("Favicon resource not configured");
16 return state.favicon;
17 },
18 refreshProjectFaviconUrl: () => state.refresh(),
19}));
20
21function setup() {
22 state.refresh = () => {};
23 const activeResources = new Set<string>();
24 state.favicon = Atom.make((get) => {
25 activeResources.add("favicon");
26 get.addFinalizer(() => activeResources.delete("favicon"));
27 return null;
28 });
29 return { activeResources };
30}
31
32it("only activates the favicon resource after removing a custom icon", async () => {
33 const { activeResources } = setup();
34 const registry = AtomRegistry.make();
35 const project: ProjectFaviconProject = {
36 environmentId: EnvironmentId.make("test"),
37 workspaceRoot: "/project",
38 title: "Project",
39 faviconPath: "icon.png",
40 projectIcon: { kind: "monogram", text: "PR", color: "blue" },
41 };
42 const view = (projectIcon: ProjectFaviconProject["projectIcon"]) => (
43 <RegistryContext value={registry}>
44 <ProjectFavicon project={{ ...project, projectIcon }} />
45 </RegistryContext>
46 );
47 try {
48 await using screen = await render(view(project.projectIcon));
49 expect(activeResources.size).toBe(0);
50 await screen.rerender(view({ kind: "emoji", emoji: "🌱" }));
51 expect(activeResources.size).toBe(0);
52 await screen.rerender(view({ kind: "lucide", name: "leaf", color: "green" }));
53 expect(activeResources.size).toBe(0);
54 await screen.rerender(view(null));
55 expect(activeResources.size).toBe(1);
56 await screen.rerender(view(project.projectIcon));
57 await screen.rerender(view(null));
58 expect(activeResources.size).toBe(1);
59 } finally {
60 registry.dispose();
61 }
62});
63
64it("asks for a new URL when a remote icon's URL stops loading", async () => {
65 const registry = AtomRegistry.make();
66 const favicon = Atom.make<string | null>("http://127.0.0.1:9/api/assets/expired/favicon.svg");
67 const svg =
68 '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"><rect width="1" height="1"/></svg>';
69 const freshUrl = URL.createObjectURL(new Blob([svg], { type: "image/svg+xml" }));
70 state.favicon = favicon;
71 state.refresh = () => registry.set(favicon, freshUrl);
72 const project: ProjectFaviconProject = {
73 environmentId: EnvironmentId.make("test"),
74 workspaceRoot: "/project",
75 title: "Project",
76 faviconPath: null,
77 projectIcon: null,
78 };
79 try {
80 await using screen = await render(
81 <RegistryContext value={registry}>
82 <ProjectFavicon project={project} />
83 </RegistryContext>,
84 );
85 await expect
86 .poll(() => screen.container.querySelector("img:not(.hidden)")?.getAttribute("src"))
87 .toBe(freshUrl);
88 } finally {
89 URL.revokeObjectURL(freshUrl);
90 registry.dispose();
91 }
92});
93
94it("asks for a new URL only once when the new URL fails too", async () => {
95 const registry = AtomRegistry.make();
96 const favicon = Atom.make<string | null>("http://127.0.0.1:9/api/assets/expired/favicon.svg");
97 const brokenUrl = "http://127.0.0.1:9/api/assets/broken/favicon.svg";
98 let refreshes = 0;
99 state.favicon = favicon;
100 state.refresh = () => {
101 refreshes += 1;
102 registry.set(favicon, brokenUrl);
103 };
104 const project: ProjectFaviconProject = {
105 environmentId: EnvironmentId.make("test"),
106 workspaceRoot: "/project",
107 title: "Project",
108 faviconPath: null,
109 projectIcon: null,
110 };
111 try {
112 await using screen = await render(
113 <RegistryContext value={registry}>
114 <ProjectFavicon project={project} />
115 </RegistryContext>,
116 );
117 await vi.waitFor(() => {
118 const image = screen.container.querySelector<HTMLImageElement>(`img[src="${brokenUrl}"]`);
119 expect(image?.complete).toBe(true);
120 });
121 expect(refreshes).toBe(1);
122 } finally {
123 registry.dispose();
124 }
125});