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