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

ChatCanvas.test.tsx

118 lines3.4 KB

1// @vitest-environment jsdom
2​
3import { act, useLayoutEffect } from "react";
4import { createRoot, type Root } from "react-dom/client";
5import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
6import { ChatCanvas } from "./ChatCanvas";
7import { useChatCanvas } from "./ChatCanvasContext";
8​
9const resize = vi.hoisted(() => ({ callbacks: new Set<() => void>() }));
10vi.mock("../../lib/observeResize", () => ({
11 observeResize: (_elements: unknown, callback: () => void) => {
12 resize.callbacks.add(callback);
13 return () => resize.callbacks.delete(callback);
14 },
15}));
16​
17let root: Root;
18let container: HTMLDivElement;
19let fontSize = 16;
20let canvas: ReturnType<typeof useChatCanvas>;
21​
22function Preview({ x }: { x: number }) {
23 const current = useChatCanvas();
24 useLayoutEffect(() => {
25 canvas = current;
26 }, [current]);
27 const reportPreview = current?.reportPreview;
28 useLayoutEffect(() => {
29 reportPreview?.({
30 key: "browser:one",
31 width: 240,
32 source: { width: 240, height: 365 },
33 position: { x, y: 12 },
34 lastInteraction: "drag",
35 });
36 }, [reportPreview, x]);
37 return null;
39​
40beforeEach(() => {
41 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
42 fontSize = 16;
43 vi.spyOn(HTMLElement.prototype, "clientWidth", "get").mockReturnValue(1800);
44 vi.spyOn(HTMLElement.prototype, "clientHeight", "get").mockReturnValue(1000);
45 vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockImplementation(function (
46 this: HTMLElement,
47 ) {
48 if (this.hasAttribute("data-thread-find-bar")) {
49 const gap = 0.75 * fontSize;
50 return new DOMRect(
51 200 + 1800 - gap - 17.5 * fontSize,
52 100 + gap,
53 17.5 * fontSize,
54 2.25 * fontSize,
55 );
56 }
57 return new DOMRect(200, 100, 1800, 1000);
58 });
59 vi.spyOn(window, "getComputedStyle").mockReturnValue({
60 paddingLeft: "48px",
61 width: "736px",
62 minWidth: "640px",
63 } as CSSStyleDeclaration);
64 container = document.createElement("div");
65 document.body.append(container);
66 root = createRoot(container);
67});
68​
69afterEach(async () => {
70 await act(() => root.unmount());
71 container.remove();
72 resize.callbacks.clear();
73 vi.restoreAllMocks();
74 vi.unstubAllGlobals();
75});
76​
77async function renderFind(open: boolean, previewX = 1548) {
78 await act(() =>
79 root.render(
80 <ChatCanvas composerOverlayElement={null} detailsCardTopInset={open ? 48 : 0}>
81 {open ? <div data-thread-find-bar /> : null}
82 <Preview x={previewX} />
83 </ChatCanvas>,
84 ),
85 );
87​
88it.each([16, 24])(
89 "keeps a preview below Find and releases its space on close at a %spx root font",
90 async (size) => {
91 fontSize = size;
92 await renderFind(false);
93 expect(canvas?.layout.frame?.y).toBe(12);
94​
95 await renderFind(true);
96 expect(canvas?.detailsCardTopInset).toBe(3 * size);
97 expect(canvas?.layout.frame?.y).toBe(3 * size + 12);
98​
99 await renderFind(false);
100 expect(canvas?.detailsCardTopInset).toBe(0);
101 expect(canvas?.layout.frame?.y).toBe(12);
102​
103 await renderFind(true);
104 expect(canvas?.layout.frame?.y).toBe(3 * size + 12);
105 },
106);
107​
108it("remeasures the open bar when its font size changes without resizing the canvas", async () => {
109 await renderFind(true, 1200);
110 expect(canvas?.layout.frame?.y).toBe(12);
111​
112 fontSize = 24;
113 await act(() => {
114 for (const callback of resize.callbacks) callback();
115 });
116 expect(canvas?.detailsCardTopInset).toBe(72);
117 expect(canvas?.layout.frame?.y).toBe(84);
118});