forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

command-palette.test.tsx48 lines, 1.9 KiBHistoryRawDownload
1import { cleanup, fireEvent, render, screen } from "@solidjs/testing-library";
2import { afterEach, beforeEach, expect, test, vi } from "vitest";
3import { CommandPalette } from "@/components/layout/command-palette";
4import { PreferencesProvider } from "@/stores/preferences-context";
5
6vi.mock("@tanstack/solid-router", () => ({ useNavigate: () => vi.fn() }));
7vi.mock("@/stores/auth-context", () => ({
8 useAuth: () => ({
9 user: () => ({ id: "u-1", username: "teacher", role: "teacher" }),
10 loading: () => false,
11 error: () => undefined,
12 }),
13}));
14vi.mock("@/stores/modules-context", () => ({
15 useModules: () => ({ enabled: () => null, loading: () => false, isEnabled: () => true, refresh: () => {} }),
16}));
17vi.mock("@/api/users", () => ({ getUserSearch: vi.fn(async () => ({ items: [], total: 0, limit: 10, offset: 0 })) }));
18vi.mock("@/api/classes", () => ({ getMyClasses: vi.fn(async () => ({ items: [], total: 0, limit: 10, offset: 0 })) }));
19
20beforeEach(() => {
21 vi.spyOn(window, "scrollTo").mockImplementation(() => {});
22 Element.prototype.scrollIntoView = vi.fn();
23});
24afterEach(() => {
25 cleanup();
26 vi.restoreAllMocks();
27});
28
29test("the palette is a combobox whose active option follows the arrow keys", async () => {
30 render(() => (
31 <PreferencesProvider>
32 <CommandPalette open onOpenChange={() => {}} />
33 </PreferencesProvider>
34 ));
35
36 const field = await screen.findByRole("combobox");
37 const list = screen.getByRole("listbox");
38 expect(field.getAttribute("aria-controls")).toBe(list.id);
39
40 const options = screen.getAllByRole("option");
41 expect(options.length).toBeGreaterThan(1);
42 expect(field.getAttribute("aria-activedescendant")).toBe(options[0].id);
43 expect(options[0].getAttribute("aria-selected")).toBe("true");
44
45 fireEvent.keyDown(field, { key: "ArrowDown" });
46 expect(field.getAttribute("aria-activedescendant")).toBe(options[1].id);
47 expect(options[1].getAttribute("aria-selected")).toBe("true");
48});