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

ComposerBannerStack.browser.test.tsx

60 lines1.7 KB

1import "../../index.css";
2​
3import { act } from "react";
4import { createRoot, type Root } from "react-dom/client";
5import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
6​
7import { ComposerBannerStack } from "./ComposerBannerStack";
8​
9let root: Root;
10let container: HTMLDivElement;
11​
12beforeEach(() => {
13 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
14 container = document.createElement("div");
15 document.body.append(container);
16 root = createRoot(container);
17});
18​
19afterEach(async () => {
20 await act(async () => root.unmount());
21 container.remove();
22 vi.unstubAllGlobals();
23});
24​
25const nextFrame = () =>
26 act(() => new Promise<void>((resolve) => requestAnimationFrame(() => setTimeout(resolve, 0))));
27​
28async function render(width: number, description: string) {
29 container.style.width = `${width}px`;
30 await act(async () =>
31 root.render(
32 <ComposerBannerStack
33 items={[{ id: "usage", variant: "info", icon: null, title: "Usage limits", description }]}
34 />,
35 ),
36 );
37 await nextFrame();
39​
40const hasDetails = () => container.querySelector('[aria-label="Show notice details"]') !== null;
41​
42it("only offers notice details when the description cannot fit", async () => {
43 await render(700, "OpenCode");
44 expect(hasDetails()).toBe(false);
45​
46 await render(700, "OpenCode ".repeat(40));
47 expect(hasDetails()).toBe(true);
48​
49 container.style.width = "4000px";
50 await nextFrame();
51 expect(hasDetails()).toBe(false);
52​
53 await render(4000, "OpenCode");
54 expect(hasDetails()).toBe(false);
55​
56 // A narrow composer hides the description entirely, so details are the only way to read it.
57 container.style.width = "300px";
58 await nextFrame();
59 expect(hasDetails()).toBe(true);
60});