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

ComposerBannerStack.browser.test.tsx

111 lines3.4 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";
6import { page, userEvent } from "vite-plus/test/browser";
7​
8import { ComposerBannerStack } from "./ComposerBannerStack";
9​
10let root: Root;
11let container: HTMLDivElement;
12​
13beforeEach(() => {
14 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
15 container = document.createElement("div");
16 document.body.append(container);
17 root = createRoot(container);
18});
19​
20afterEach(async () => {
21 await act(async () => root.unmount());
22 container.remove();
23 vi.unstubAllGlobals();
24});
25​
26const nextFrame = () =>
27 act(() => new Promise<void>((resolve) => requestAnimationFrame(() => setTimeout(resolve, 0))));
28​
29async function render(width: number, description: string) {
30 container.style.width = `${width}px`;
31 await act(async () =>
32 root.render(
33 <ComposerBannerStack
34 items={[{ id: "usage", variant: "info", icon: null, title: "Usage limits", description }]}
35 />,
36 ),
37 );
38 await nextFrame();
40​
41const hasDetails = () => container.querySelector('[aria-label="Show notice details"]') !== null;
42​
43it("only offers notice details when the description cannot fit", async () => {
44 await render(700, "OpenCode");
45 expect(hasDetails()).toBe(false);
46​
47 await render(700, "OpenCode ".repeat(40));
48 expect(hasDetails()).toBe(true);
49​
50 container.style.width = "4000px";
51 await nextFrame();
52 expect(hasDetails()).toBe(false);
53​
54 await render(4000, "OpenCode");
55 expect(hasDetails()).toBe(false);
56​
57 // A narrow composer hides the description entirely, so details are the only way to read it.
58 container.style.width = "300px";
59 await nextFrame();
60 expect(hasDetails()).toBe(true);
61});
62​
63it("keeps only the usage limit attached while an active goal waits in the collapsible stack", async () => {
64 await act(async () =>
65 root.render(
66 <ComposerBannerStack
67 items={[
68 {
69 id: "goal",
70 variant: "default",
71 priority: "activity",
72 icon: null,
73 title: "Goal active",
74 description: "Finish the banner fix",
75 },
76 {
77 id: "limit",
78 variant: "default",
79 priority: "urgent",
80 icon: null,
81 title: "Usage limit reached",
82 },
83 ]}
84 />,
85 ),
86 );
87​
88 const visibleBanners = () =>
89 Array.from(container.querySelectorAll('[data-slot="composer-banner"]')).filter((banner) =>
90 banner.checkVisibility({ checkOpacity: true, checkVisibilityCSS: true }),
91 );
92 const limit = page.getByText("Usage limit reached", { exact: true });
93 const goal = page.getByText("Goal active", { exact: true });
94​
95 expect(visibleBanners()).toHaveLength(1);
96 await expect.element(limit).toBeVisible();
97 await expect.element(goal).not.toBeVisible();
98 expect(container.querySelector('[data-slot="composer-banner-attachment"]')?.textContent).toBe(
99 "Usage limit reached",
100 );
101​
102 await act(async () => page.getByRole("button", { name: "Show other notices" }).click());
103 await expect.element(goal).toBeVisible();
104 await expect.element(limit).toBeVisible();
105 await expect.poll(visibleBanners).toHaveLength(2);
106​
107 await act(async () => userEvent.keyboard("{Escape}"));
108 await expect.element(goal).not.toBeVisible();
109 await expect.element(limit).toBeVisible();
110 expect(visibleBanners()).toHaveLength(1);
111});