Files
t3code/apps/web/src/components/chat/ComposerBannerStack.test.tsx
T

104 lines
3.0 KiB
TypeScript

import { cloneElement, type ReactElement, type ReactNode } from "react";
import { act, create, type ReactTestRenderer } from "react-test-renderer";
import { afterEach, expect, it, vi } from "vite-plus/test";
import { ComposerBannerStack } from "./ComposerBannerStack";
vi.mock("../ui/popover", () => ({
Popover: "popover",
PopoverTrigger: ({ render, children }: { render: ReactElement; children: ReactNode }) =>
cloneElement(render, {}, children),
PopoverPopup: "popup",
}));
vi.mock("../ui/button", () => ({ Button: "button" }));
vi.mock("../ui/scroll-area", () => ({ ScrollArea: "div" }));
let renderer: ReactTestRenderer;
afterEach(async () => {
if (renderer) await act(() => renderer.unmount());
vi.unstubAllGlobals();
});
it("only offers notice details when the description cannot fit", async () => {
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
let resize = () => {};
let mutate = () => {};
vi.stubGlobal(
"MutationObserver",
class {
constructor(callback: () => void) {
mutate = callback;
}
observe() {}
disconnect() {}
},
);
vi.stubGlobal(
"ResizeObserver",
class {
constructor(private readonly callback: (entries: { target: unknown }[]) => void) {}
observe(target: unknown) {
resize = () => this.callback([{ target }]);
}
disconnect() {}
},
);
let position = "static";
vi.stubGlobal("getComputedStyle", () => ({ position }));
let availableWidth = 200;
const nested = { clientWidth: 100, scrollWidth: 80 };
const text = {
querySelectorAll: () => [nested],
get clientWidth() {
return (
availableWidth -
(renderer?.root.findAllByProps({ "aria-label": "Show notice details" }).length ? 28 : 0)
);
},
scrollWidth: 80,
};
await act(() => {
renderer = create(
<ComposerBannerStack
items={[
{
id: "usage",
variant: "info",
icon: null,
title: "Usage limits",
description: "OpenCode",
},
]}
/>,
{
createNodeMock: (element) =>
element.type === "span" ? text : element.type === "button" ? { offsetWidth: 24 } : null,
},
);
});
const details = () => renderer.root.findAllByProps({ "aria-label": "Show notice details" });
expect(details()).toHaveLength(0);
text.scrollWidth = 300;
await act(() => resize());
expect(details()).toHaveLength(1);
// It fits without the icon: the icon must not keep its own overflow alive.
availableWidth = 308;
await act(() => resize());
expect(details()).toHaveLength(0);
text.scrollWidth = 80;
await act(() => resize());
expect(details()).toHaveLength(0);
nested.scrollWidth = 500;
await act(() => mutate());
expect(details()).toHaveLength(1);
nested.scrollWidth = 80;
await act(() => mutate());
expect(details()).toHaveLength(0);
position = "absolute";
await act(() => resize());
expect(details()).toHaveLength(1);
position = "static";
await act(() => resize());
expect(details()).toHaveLength(0);
});