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

ComposerBannerStack.browser.test.tsx

125 lines4.6 KB

1import { InfoIcon } from "lucide-react";
2import type { ReactNode } from "react";
3import { expect, it, vi } from "vite-plus/test";
4​
5import "~/index.css";
6​
7import { render } from "../../../test/render";
8import { ComposerBanner } from "./ComposerBanner";
9import {
10 ComposerBannerStack,
11 NoticeText,
12 type ComposerBannerStackItem,
13} from "./ComposerBannerStack";
14​
15function notice(title: string, group?: ComposerBannerStackItem["group"]): ComposerBannerStackItem {
16 return { id: title, variant: "default", icon: <InfoIcon />, title, ...(group ? { group } : {}) };
18​
19it("puts machine notices first, whatever order the notices arrive in", async () => {
20 await using screen = await render(
21 <ComposerBannerStack items={[notice("Branch changed"), notice("Offline", "environment")]} />,
22 );
23​
24 expect(screen.container.textContent).toBe("OfflineBranch changed");
25});
26​
27it("leaves the next attachment its own top edge when there are no notices", async () => {
28 await using screen = await render(
29 <ComposerBanner.Column>
30 <ComposerBannerStack items={[]} />
31 <ComposerBanner.Attachment>
32 <ComposerBanner.Root data-testid="drawer">Approval</ComposerBanner.Root>
33 </ComposerBanner.Attachment>
34 </ComposerBanner.Column>,
35 );
36​
37 const surface = getComputedStyle(screen.getByTestId("drawer").element(), "::before");
38 expect(surface.borderTopWidth).toBe("1px");
39 expect(surface.borderTopLeftRadius).not.toBe("0px");
40});
41​
42function noticeRow(width: string, children: ReactNode) {
43 return (
44 <div data-testid="row" className="flex items-center gap-1.5" style={{ width }}>
45 <NoticeText detailsLabel="Show details" className="min-w-0 truncate">
46 {children}
47 </NoticeText>
48 </div>
49 );
51​
52it("leaves out the details button when only a link's hit area reaches past the text", async () => {
53 // Phones lay out in device pixels, so a range can measure one float32 step (2^-17 px at
54 // this size) wider than its element. Headless Chromium's rects agree exactly.
55 const rangeRect = Range.prototype.getBoundingClientRect;
56 using _rangeRect = vi
57 .spyOn(Range.prototype, "getBoundingClientRect")
58 .mockImplementation(function (this: Range) {
59 const rect = rangeRect.call(this);
60 return new DOMRect(rect.x, rect.y, rect.width + 2 ** -17, rect.height);
61 });
62 // The flicker is a details button that mounts and then unmounts, so record every insertion.
63 const inserted: Node[] = [];
64 using observing = new DisposableStack();
65 const mutations = new MutationObserver((records) =>
66 inserted.push(...records.flatMap((record) => [...record.addedNodes])),
67 );
68 mutations.observe(document.body, { childList: true, subtree: true });
69 observing.defer(() => mutations.disconnect());
70 await using screen = await render(
71 noticeRow(
72 "20rem",
73 <>
74 Needs input{" "}
75 <button type="button" className="relative after:absolute after:-inset-x-10 after:inset-y-0">
76 Review
77 </button>
78 </>,
79 ),
80 );
81​
82 await expect.element(screen.getByText("Review")).toBeVisible();
83 const details = [
84 ...inserted,
85 ...mutations.takeRecords().flatMap((record) => [...record.addedNodes]),
86 ].filter(
87 (node) =>
88 node instanceof Element &&
89 (node.matches('[aria-label="Show details"]') ||
90 node.querySelector('[aria-label="Show details"]')),
91 );
92 expect(details).toEqual([]);
93});
94​
95it("shows the details button while text or nested text is cut, until it fits again", async () => {
96 await using screen = await render(noticeRow("4rem", "Waiting on a subagent with a long name"));
97 const details = screen.getByRole("button", { name: "Show details" });
98 await expect.element(details).toBeInTheDocument();
99​
100 await screen.rerender(noticeRow("4rem", "Ready"));
101 await expect.element(details).not.toBeInTheDocument();
102​
103 await screen.rerender(noticeRow("4rem", "Waiting on a subagent with a long name"));
104 await expect.element(details).toBeInTheDocument();
105​
106 screen.getByTestId("row").element().style.width = "40rem";
107 await expect.element(details).not.toBeInTheDocument();
108​
109 await screen.rerender(
110 noticeRow(
111 "40rem",
112 <span className="inline-block max-w-16 truncate align-bottom">A long nested name</span>,
113 ),
114 );
115 await expect.element(details).toBeInTheDocument();
116​
117 await screen.rerender(
118 <ComposerBanner.Root>
119 {noticeRow("4rem", "Waiting on a subagent with a long name")}
120 </ComposerBanner.Root>,
121 );
122 await expect.element(details).toBeInTheDocument();
123 await screen.rerender(<ComposerBanner.Root>{noticeRow("4rem", "Ready")}</ComposerBanner.Root>);
124 await expect.element(details).not.toBeInTheDocument();
125});