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

PreviewFaviconIcon.test.tsx

43 lines1.5 KB

1import { act } from "react";
2import { create, type ReactTestRenderer } from "react-test-renderer";
3import { afterEach, expect, it, vi } from "vite-plus/test";
4​
5vi.mock("~/browserFaviconStore", () => ({ useFaviconForThreadUrl: () => null }));
6​
7import { FaviconImage } from "./PreviewFaviconIcon";
8​
9let renderer: ReactTestRenderer | undefined;
10​
11afterEach(async () => {
12 await act(async () => renderer?.unmount());
13 vi.unstubAllGlobals();
14});
15​
16it("falls through failed favicon sources and retries when the source list changes", async () => {
17 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
18 const captured = "data:image/png;base64,AAAA";
19 const remote = "https://public.example/icon";
20 await act(async () => {
21 renderer = create(
22 <FaviconImage sources={[captured, remote]} fallback={<span>fallback</span>} />,
23 );
24 });
25 expect(renderer!.root.findByType("img").props.src).toBe(captured);
26​
27 await act(async () => renderer!.root.findByType("img").props.onError());
28 expect(renderer!.root.findByType("img").props.src).toBe(remote);
29​
30 await act(async () => renderer!.root.findByType("img").props.onError());
31 expect(renderer!.root.findAllByType("img")).toHaveLength(0);
32 expect(renderer!.root.findByType("span").children).toEqual(["fallback"]);
33​
34 await act(async () => {
35 renderer!.update(
36 <FaviconImage
37 sources={[captured, "https://public.example/new-icon"]}
38 fallback={<span>fallback</span>}
39 />,
40 );
41 });
42 expect(renderer!.root.findByType("img").props.src).toBe(captured);
43});