import { useState } from "react";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";
import { Button } from "./button";
import {
Command,
CommandDialog,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
CommandSeparator,
CommandShortcut
} from "./command";
describe("Command", () => {
it("renders root, input, list, item, and shortcut slots", async () => {
const user = userEvent.setup();
render(
No results.
Launch release
Open legal review
G L
);
const root = document.querySelector('[data-slot="root"]');
const input = screen.getByPlaceholderText("Search actions");
expect(root).toHaveAttribute("data-slot", "root");
expect(input).toHaveAttribute("data-slot", "input");
expect(document.querySelector('[data-slot="list"]')).toBeInTheDocument();
expect(screen.getByText("Open legal review").closest('[data-slot="item"]')).toBeInTheDocument();
expect(screen.getByText("G L")).toHaveAttribute("data-slot", "shortcut");
await user.type(input, "zzz");
expect(await screen.findByText("No results.")).toHaveAttribute("data-slot", "empty");
});
it("calls onSelect when a command item is chosen", async () => {
const user = userEvent.setup();
const onSelect = vi.fn();
render(
Launch release
);
await user.click(screen.getByText("Launch release"));
expect(onSelect).toHaveBeenCalledWith("launch-release");
});
it("renders inside a dialog and closes through the default close control", async () => {
const user = userEvent.setup();
function CommandDialogExample() {
const [open, setOpen] = useState(true);
return (
Open docs
);
}
render();
expect(screen.getByPlaceholderText("Search across workspace")).toBeInTheDocument();
await user.click(screen.getByRole("button", { name: "Close dialog" }));
await waitFor(() => {
expect(screen.queryByPlaceholderText("Search across workspace")).not.toBeInTheDocument();
});
});
it("supports loading and footer content on the root command surface", async () => {
render(
Manage commands}
label="Workspace palette"
loading
loadingMessage="Searching workspace…"
>
No matching items.
);
expect(screen.getByText("Searching workspace…")).toHaveAttribute("data-slot", "loading");
expect(screen.getByText("Manage commands")).toBeInTheDocument();
});
it("supports dialog title, description, and footer actions for a practical palette shell", async () => {
const user = userEvent.setup();
function CommandDialogExample() {
const [open, setOpen] = useState(true);
return (
Manage shortcuts}
onOpenChange={setOpen}
open={open}
title="Workspace command palette"
>
Recent release brief
);
}
render();
expect(screen.getByText("Workspace command palette")).toBeInTheDocument();
expect(screen.getByText("Jump to docs, recent launches, and operational shortcuts.")).toBeInTheDocument();
expect(screen.getByText("Manage shortcuts")).toBeInTheDocument();
await user.click(screen.getByRole("button", { name: "Close dialog" }));
await waitFor(() => {
expect(screen.queryByText("Workspace command palette")).not.toBeInTheDocument();
});
});
});