|
| 1 | +import { RegistryContext } from "@effect/atom-react"; |
| 2 | +import { EnvironmentId } from "@t3tools/contracts"; |
| 3 | +import { Atom, AtomRegistry } from "effect/reactivity"; |
| 4 | +import { expect, it, vi } from "vite-plus/test"; |
| 5 | + |
| 6 | +import { render } from "../../test/render"; |
| 7 | +import { ProjectFavicon, type ProjectFaviconProject } from "./ProjectFavicon"; |
| 8 | + |
| 9 | +const state = vi.hoisted(() => ({ favicon: undefined as Atom.Atom<string | null> | undefined })); |
| 10 | +vi.mock(import("../state/assets"), () => ({ |
| 11 | + projectFaviconUrlAtom: () => { |
| 12 | + if (!state.favicon) throw new Error("Favicon resource not configured"); |
| 13 | + return state.favicon; |
| 14 | + }, |
| 15 | +})); |
| 16 | + |
| 17 | +it("only activates the favicon resource after removing a custom icon", async () => { |
| 18 | + const registry = AtomRegistry.make(); |
| 19 | + const activeResources = new Set<string>(); |
| 20 | + state.favicon = Atom.make((get) => { |
| 21 | + activeResources.add("favicon"); |
| 22 | + get.addFinalizer(() => activeResources.delete("favicon")); |
| 23 | + return null; |
| 24 | + }); |
| 25 | + const project: ProjectFaviconProject = { |
| 26 | + environmentId: EnvironmentId.make("test"), |
| 27 | + workspaceRoot: "/project", |
| 28 | + title: "Project", |
| 29 | + faviconPath: "icon.png", |
| 30 | + projectIcon: { kind: "monogram", text: "PR", color: "blue" }, |
| 31 | + }; |
| 32 | + const view = (projectIcon: ProjectFaviconProject["projectIcon"]) => ( |
| 33 | + <RegistryContext value={registry}> |
| 34 | + <ProjectFavicon project={{ ...project, projectIcon }} /> |
| 35 | + </RegistryContext> |
| 36 | + ); |
| 37 | + try { |
| 38 | + await using screen = await render(view(project.projectIcon)); |
| 39 | + expect(activeResources.size).toBe(0); |
| 40 | + await screen.rerender(view({ kind: "emoji", emoji: "🌱" })); |
| 41 | + expect(activeResources.size).toBe(0); |
| 42 | + await screen.rerender(view({ kind: "lucide", name: "leaf", color: "green" })); |
| 43 | + expect(activeResources.size).toBe(0); |
| 44 | + await screen.rerender(view(null)); |
| 45 | + expect(activeResources.size).toBe(1); |
| 46 | + await screen.rerender(view(project.projectIcon)); |
| 47 | + await screen.rerender(view(null)); |
| 48 | + expect(activeResources.size).toBe(1); |
| 49 | + } finally { |
| 50 | + registry.dispose(); |
| 51 | + } |
| 52 | +}); |
0 commit comments