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

PreviewFaviconIcon.browser.test.tsx

49 lines1.5 KB

1import { act } from "react";
2import { createRoot, type Root } from "react-dom/client";
3import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
4​
5import { FaviconImage } from "./PreviewFaviconIcon";
6​
7const GIF = "data:image/gif;base64,R0lGODlhAQABAAAAACw=";
8const captured = `${GIF}#captured`;
9const remote = `${GIF}#remote`;
10​
11let root: Root;
12let container: HTMLDivElement;
13​
14beforeEach(() => {
15 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
16 container = document.createElement("div");
17 document.body.append(container);
18 root = createRoot(container);
19});
20​
21afterEach(async () => {
22 await act(async () => root.unmount());
23 container.remove();
24 vi.unstubAllGlobals();
25});
26​
27const image = () => container.querySelector("img");
28const failImage = () => act(async () => image()!.dispatchEvent(new Event("error")));
29​
30it("falls through failed favicon sources and retries when the source list changes", async () => {
31 await act(async () =>
32 root.render(<FaviconImage sources={[captured, remote]} fallback={<span>fallback</span>} />),
33 );
34 expect(image()?.getAttribute("src")).toBe(captured);
35​
36 await failImage();
37 expect(image()?.getAttribute("src")).toBe(remote);
38​
39 await failImage();
40 expect(image()).toBeNull();
41 expect(container.textContent).toBe("fallback");
42​
43 await act(async () =>
44 root.render(
45 <FaviconImage sources={[captured, `${GIF}#new-icon`]} fallback={<span>fallback</span>} />,
46 ),
47 );
48 expect(image()?.getAttribute("src")).toBe(captured);
49});