Files

70 lines
2.3 KiB
TypeScript

// @vitest-environment jsdom
import "@testing-library/jest-dom/vitest";
import { fireEvent, render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { ChatMentionAutocomplete } from "./ChatMentionAutocomplete";
vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (key: string) => key }) }));
describe("ChatMentionAutocomplete", () => {
it("renders grouped accessible options and delegates pointer selection", () => {
const onSelect = vi.fn();
const onActiveIndexChange = vi.fn();
render(
<ChatMentionAutocomplete
open
loading={false}
activeIndex={1}
listboxId="mention-list"
vm={{
total: 2,
groups: [
{
kind: "user",
labelKey: "chat.mention.groups.users",
items: [
{ kind: "user", id: "user_1", label: "Ada", detail: "ada@example.test", href: null },
{ kind: "user", id: "user_2", label: "Mara", detail: null, href: null },
],
},
],
}}
actions={{ onSelect, onActiveIndexChange }}
/>
);
expect(screen.getByRole("listbox", { name: "chat.mention.label" })).toBeInTheDocument();
expect(screen.getAllByRole("option")[1]).toHaveAttribute("aria-selected", "true");
fireEvent.mouseEnter(screen.getAllByRole("option")[0]!);
expect(onActiveIndexChange).toHaveBeenCalledWith(0);
fireEvent.click(screen.getByRole("option", { name: /Ada/ }));
expect(onSelect).toHaveBeenCalledWith(expect.objectContaining({ kind: "user", id: "user_1" }));
});
it("announces loading and empty states", () => {
const { rerender } = render(
<ChatMentionAutocomplete
open
loading
activeIndex={0}
listboxId="mention-list"
vm={null}
actions={{ onSelect: vi.fn(), onActiveIndexChange: vi.fn() }}
/>
);
expect(screen.getByLabelText("chat.mention.loading")).toBeInTheDocument();
rerender(
<ChatMentionAutocomplete
open
loading={false}
activeIndex={0}
listboxId="mention-list"
vm={{ total: 0, groups: [] }}
actions={{ onSelect: vi.fn(), onActiveIndexChange: vi.fn() }}
/>
);
expect(screen.getByText("chat.mention.empty")).toBeInTheDocument();
});
});