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

PreviewImageContent.browser.test.tsx

100 lines3.7 KB

1import type { ClientSettings } from "@t3tools/contracts";
2import { expect, it, vi } from "vite-plus/test";
3​
4import { act, render } from "../../../test/render";
5import "~/index.css";
6import {
7 __resetClientSettingsPersistenceForTests,
8 updateClientSettings,
9} from "~/hooks/useSettings";
10import { I18nProvider } from "~/i18n/I18nProvider";
11import { startI18n, whenLocaleReady } from "~/i18n/runtime";
12import { ensureLocalApi } from "~/localApi";
13import PreviewImageContent from "./PreviewImageContent";
14​
15function png(width: number, height: number) {
16 const canvas = document.createElement("canvas");
17 canvas.width = width;
18 canvas.height = height;
19 return canvas.toDataURL("image/png");
21​
22it("fits the image inside a pane with a fractional width", async () => {
23 const host = document.createElement("div");
24 host.style.cssText = "display:flex;flex-direction:column;width:500.5px;height:600px";
25 await using _screen = await render(
26 <PreviewImageContent src={png(1600, 900)} name="wide.png" mimeType="image/png" />,
27 { container: host },
28 );
29 const image = host.querySelector("img")!;
30 const padding = 32;
31​
32 await vi.waitFor(() => {
33 const width = image.getBoundingClientRect().width + padding;
34 expect(width).toBeGreaterThan(499.5);
35 expect(width).toBeLessThanOrEqual(500.5);
36 });
37});
38​
39it("keeps a cached image out of view until it is fitted", async () => {
40 const src = png(1600, 900);
41 const host = document.createElement("div");
42 host.style.cssText = "display:flex;flex-direction:column;width:500px;height:600px";
43 const image = () => host.querySelector("img");
44 await using screen = await render(
45 <PreviewImageContent key="first" src={src} name="wide.png" mimeType="image/png" />,
46 { container: host },
47 );
48 await vi.waitFor(() => expect(image()?.getBoundingClientRect().width).toBeGreaterThan(400));
49​
50 // A new key remounts the preview, as a new file revision does. The cached image has its natural
51 // size on insertion, and the observer measures it before the load event can fit it.
52 let insertedWidth: number | undefined;
53 const mounts = new MutationObserver(() => {
54 insertedWidth ??= image()?.getBoundingClientRect().width;
55 });
56 mounts.observe(host, { childList: true, subtree: true });
57 await screen.rerender(
58 <PreviewImageContent key="second" src={src} name="wide.png" mimeType="image/png" />,
59 );
60 mounts.disconnect();
61​
62 expect(insertedWidth).toBeLessThanOrEqual(500);
63 await vi.waitFor(() => expect(image()?.getBoundingClientRect().width).toBeGreaterThan(400));
64});
65​
66it("groups the image size in the app language, not the browser's", async () => {
67 const { persistence } = ensureLocalApi();
68 let savedSettings: ClientSettings | null = null;
69 using _readSettings = vi
70 .spyOn(persistence, "getClientSettings")
71 .mockImplementation(async () => savedSettings);
72 using _writeSettings = vi
73 .spyOn(persistence, "setClientSettings")
74 .mockImplementation(async (settings) => {
75 savedSettings = settings;
76 });
77 __resetClientSettingsPersistenceForTests();
78 const setLanguage = async (language: string) => {
79 await act(async () => {
80 await updateClientSettings({ language });
81 await whenLocaleReady();
82 });
83 };
84 try {
85 startI18n();
86 await setLanguage("de");
87 const host = document.createElement("div");
88 host.style.cssText = "display:flex;flex-direction:column;width:500px;height:600px";
89 await using _screen = await render(
90 <I18nProvider>
91 <PreviewImageContent src={png(1600, 900)} name="wide.png" mimeType="image/png" />
92 </I18nProvider>,
93 { container: host, locale: false },
94 );
95 await vi.waitFor(() => expect(host.textContent).toContain("1.600 × 900"));
96 } finally {
97 await setLanguage("en");
98 __resetClientSettingsPersistenceForTests();
99 }
100});