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

ProjectFavicon.test.tsx

146 lines4.5 KB

1import type { ComponentType, Dispatch, ReactElement, SetStateAction } from "react";
2import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
3import type { EnvironmentId } from "@t3tools/contracts";
4​
5const testState = vi.hoisted(() => ({
6 faviconUrl: "https://environment.test/api/assets/token-a/v1-20-favicon.svg",
7 lastResource: null as unknown,
8}));
9​
10const hooks = vi.hoisted(() => {
11 let cursor = 0;
12 let slots: unknown[] = [];
13 const nextIndex = () => cursor++;
14​
15 return {
16 beginRender() {
17 cursor = 0;
18 },
19 reset() {
20 cursor = 0;
21 slots = [];
22 },
23 useMemoCache(size: number): unknown[] {
24 const index = nextIndex();
25 if (!slots[index]) {
26 slots[index] = Array.from({ length: size }, () => Symbol.for("react.memo_cache_sentinel"));
27 }
28 return slots[index] as unknown[];
29 },
30 useState<T>(initialValue: T | (() => T)): [T, Dispatch<SetStateAction<T>>] {
31 const index = nextIndex();
32 if (index >= slots.length) {
33 slots[index] =
34 typeof initialValue === "function" ? (initialValue as () => T)() : initialValue;
35 }
36 const setValue: Dispatch<SetStateAction<T>> = (nextValue) => {
37 const previous = slots[index] as T;
38 slots[index] =
39 typeof nextValue === "function" ? (nextValue as (value: T) => T)(previous) : nextValue;
40 };
41 return [slots[index] as T, setValue];
42 },
43 };
44});
45​
46vi.mock("react", async (importOriginal) => {
47 const actual = await importOriginal<typeof import("react")>();
48 return {
49 ...actual,
50 useState: hooks.useState,
51 };
52});
53​
54vi.mock("react/compiler-runtime", () => ({ c: hooks.useMemoCache }));
55vi.mock("../assets/assetUrls", () => ({
56 useAssetUrlState: (_environmentId: unknown, resource: unknown) => {
57 testState.lastResource = resource;
58 return { _tag: "Success", url: testState.faviconUrl };
59 },
60}));
61​
62import { ProjectFavicon } from "./ProjectFavicon";
63​
64type ProjectFaviconImageProps = {
65 readonly cacheKey: string;
66 readonly src: string;
67 readonly className?: string | undefined;
68 readonly fallbackIcon: ComponentType<{ className?: string }>;
69};
70​
71type ImageElement = ReactElement<{
72 readonly src: string;
73 readonly onLoad?: () => void;
74 readonly onError?: () => void;
75}>;
76​
77type ProjectFaviconImageElement = ReactElement<{
78 readonly children: [ReactElement | null, ImageElement | null, ImageElement | null];
79}>;
80​
81function resolveImageComponent(): {
82 readonly Component: (props: ProjectFaviconImageProps) => ProjectFaviconImageElement;
83 readonly props: ProjectFaviconImageProps;
84} {
85 hooks.beginRender();
86 const element = ProjectFavicon({
87 environmentId: "environment-test" as EnvironmentId,
88 cwd: "/workspace-test",
89 }) as ReactElement<ProjectFaviconImageProps>;
90 hooks.reset();
91​
92 return {
93 Component: element.type as (props: ProjectFaviconImageProps) => ProjectFaviconImageElement,
94 props: element.props,
95 };
97​
98function renderImage(
99 Component: (props: ProjectFaviconImageProps) => ProjectFaviconImageElement,
100 props: ProjectFaviconImageProps,
101): ProjectFaviconImageElement {
102 hooks.beginRender();
103 return Component(props);
105​
106describe("ProjectFavicon", () => {
107 beforeEach(() => {
108 hooks.reset();
109 });
110​
111 it("falls back when the displayed favicon fails without discarding a valid older image early", () => {
112 const { Component, props } = resolveImageComponent();
113 const initialLoadingImage = renderImage(Component, props).props.children[2];
114 initialLoadingImage?.props.onLoad?.();
115​
116 const refreshedProps = {
117 ...props,
118 src: "https://environment.test/api/assets/token-b/v1-20-favicon.svg",
119 };
120 const refreshing = renderImage(Component, refreshedProps).props.children;
121 expect(refreshing[1]?.props.src).toBe(props.src);
122 refreshing[2]?.props.onError?.();
123​
124 const afterRefreshError = renderImage(Component, refreshedProps).props.children;
125 expect(afterRefreshError[1]?.props.src).toBe(props.src);
126 afterRefreshError[1]?.props.onError?.();
127​
128 const afterDisplayedError = renderImage(Component, refreshedProps).props.children;
129 expect(afterDisplayedError[0]).not.toBeNull();
130 expect(afterDisplayedError[1]).toBeNull();
131 });
132​
133 it("requests a saved favicon path when one is set", () => {
134 ProjectFavicon({
135 environmentId: "environment-test" as EnvironmentId,
136 cwd: "/workspace-test",
137 faviconPath: "brand/icon.svg",
138 });
139​
140 expect(testState.lastResource).toEqual({
141 _tag: "project-favicon",
142 cwd: "/workspace-test",
143 path: "brand/icon.svg",
144 });
145 });
146});