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

ComposerCommandMenu.browser.test.tsx

154 lines5.7 KB

1import { useState } from "react";
2import { expect, it, vi } from "vite-plus/test";
3import { userEvent } from "vite-plus/test/browser";
4​
5import "~/index.css";
6import { render } from "../../../test/render";
7import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog";
8import { ComposerCommandMenu, type ComposerCommandItem } from "./ComposerCommandMenu";
9​
10const LONG = "A description long enough to be cut off in a narrow menu row";
11const items: ComposerCommandItem[] = Array.from({ length: 30 }, (_, index) => ({
12 id: `command:${index}`,
13 type: "slash-command",
14 command: "model",
15 label: `/command-${index}`,
16 description: `${LONG} ${index}`,
17}));
18​
19function Menu(props: { activeItemId: string | null; loading?: boolean }) {
20 return (
21 <div className="w-80">
22 <ComposerCommandMenu
23 listId="suggestions"
24 items={props.loading ? [] : items}
25 resolvedTheme="light"
26 isLoading={props.loading ?? false}
27 triggerKind="slash-command"
28 activeItemId={props.activeItemId}
29 onHighlightedItemChange={() => {}}
30 onSelect={() => {}}
31 />
32 </div>
33 );
35​
36const tooltipText = () =>
37 document.querySelector('[data-slot="tooltip-popup"]:not([data-ending-style])')?.textContent ??
38 null;
39const viewport = () => document.querySelector<HTMLElement>('[data-slot="scroll-area-viewport"]')!;
40​
41it("closes the highlighted row's description tooltip when the menu scrolls", async () => {
42 await using _screen = await render(<Menu activeItemId="command:0" />);
43 await vi.waitFor(() => expect(tooltipText()).toBe(`${LONG} 0`));
44​
45 viewport().scrollTop = 40;
46 await vi.waitFor(() => expect(tooltipText()).toBeNull());
47});
48​
49it("keeps the tooltip open when highlighting scrolls a row into view", async () => {
50 await using screen = await render(<Menu activeItemId="command:0" />);
51 await vi.waitFor(() => expect(tooltipText()).toBe(`${LONG} 0`));
52​
53 const onScroll = vi.fn();
54 viewport().addEventListener("scroll", onScroll);
55 await screen.rerender(<Menu activeItemId="command:25" />);
56 await vi.waitFor(() => expect(onScroll).toHaveBeenCalled());
57 expect(tooltipText()).toBe(`${LONG} 25`);
58});
59​
60it("closes the highlighted row's description tooltip when a modal dialog opens over the menu", async () => {
61 await using screen = await render(<Menu activeItemId="command:0" />);
62 await vi.waitFor(() => expect(tooltipText()).toBe(`${LONG} 0`));
63​
64 await screen.rerender(
65 <>
66 <Menu activeItemId="command:0" />
67 <Dialog open>
68 <DialogPopup>
69 <DialogTitle>Usage</DialogTitle>
70 </DialogPopup>
71 </Dialog>
72 </>,
73 );
74 await vi.waitFor(() => expect(document.querySelector('[role="dialog"]')).not.toBeNull());
75 await vi.waitFor(() => expect(tooltipText()).toBeNull());
76});
77​
78it("closes the highlighted row's description tooltip when focus leaves the composer and reopens on return", async () => {
79 await using screen = await render(
80 <>
81 <div>Outside</div>
82 <input aria-label="Composer" aria-controls="suggestions" />
83 <button type="button">Stash</button>
84 <Menu activeItemId="command:0" />
85 </>,
86 );
87 const composer = screen.getByRole("textbox", { name: "Composer" });
88 await userEvent.click(composer);
89 await vi.waitFor(() => expect(tooltipText()).toBe(`${LONG} 0`));
90​
91 await userEvent.click(screen.getByRole("button", { name: "Stash" }));
92 await vi.waitFor(() => expect(tooltipText()).toBeNull());
93​
94 await userEvent.click(composer);
95 await vi.waitFor(() => expect(tooltipText()).toBe(`${LONG} 0`));
96​
97 await userEvent.click(screen.getByText("Outside"));
98 await vi.waitFor(() => expect(tooltipText()).toBeNull());
99});
100​
101it("closes a hovered row's description tooltip when the pointer leaves the menu", async () => {
102 function HoverMenu() {
103 const [activeItemId, setActiveItemId] = useState<string | null>("command:0");
104 return (
105 <>
106 <div>Outside</div>
107 <input aria-label="Composer" aria-controls="suggestions" />
108 <div className="w-80">
109 <ComposerCommandMenu
110 listId="suggestions"
111 items={items}
112 resolvedTheme="light"
113 isLoading={false}
114 triggerKind="slash-command"
115 activeItemId={activeItemId}
116 onHighlightedItemChange={setActiveItemId}
117 onSelect={() => {}}
118 />
119 </div>
120 </>
121 );
122 }
123 await using screen = await render(<HoverMenu />);
124 await userEvent.click(screen.getByRole("textbox", { name: "Composer" }));
125 await userEvent.hover(screen.getByText("/command-2", { exact: true }));
126 await vi.waitFor(() => expect(tooltipText()).toBe(`${LONG} 2`));
127​
128 await userEvent.hover(screen.getByText("Outside"));
129 await vi.waitFor(() => expect(tooltipText()).toBeNull());
130​
131 await userEvent.hover(screen.getByText("/command-2", { exact: true }));
132 await vi.waitFor(() => expect(tooltipText()).toBe(`${LONG} 2`));
133});
134​
135it("keeps descriptions closed when slash search results arrive after focus leaves", async () => {
136 const tree = (loading: boolean) => (
137 <>
138 <input aria-label="Composer" aria-controls={loading ? undefined : "suggestions"} />
139 <button type="button">Outside the composer</button>
140 <Menu activeItemId={loading ? null : "command:0"} loading={loading} />
141 </>
142 );
143 await using screen = await render(tree(true));
144 const outside = screen.getByRole("button", { name: "Outside the composer" });
145 await userEvent.click(screen.getByRole("textbox", { name: "Composer" }));
146 await expect.element(screen.getByText("Searching workspace skills...")).toBeVisible();
147 await userEvent.tab();
148 await expect.element(outside).toHaveFocus();
149​
150 await screen.rerender(tree(false));
151 await expect.element(screen.getByText("/command-0", { exact: true })).toBeVisible();
152 await expect.element(outside).toHaveFocus();
153 expect(tooltipText()).toBeNull();
154});