forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

command-palette.test.tsx74 lines, 3.1 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 })) }));
19vi.mock("@/lib/command-search", async (importOriginal) => ({
20 ...(await importOriginal<typeof import("@/lib/command-search")>()),
21 loadCommandRecords: vi.fn(async () => [
22 { kind: "course", id: "c1", title: "Biyoloji", context: null, description: null, courseKind: "course", at: null },
23 { kind: "exam", id: "e1", title: "1. Yazılı", context: "Biyoloji — 10-B", description: null, at: null },
24 { kind: "course", id: "c2", title: "Kimya", context: null, description: null, courseKind: "course", at: null },
25 ]),
26}));
27
28beforeEach(() => {
29 vi.spyOn(window, "scrollTo").mockImplementation(() => {});
30 Element.prototype.scrollIntoView = vi.fn();
31});
32afterEach(() => {
33 cleanup();
34 vi.restoreAllMocks();
35});
36
37test("the palette is a combobox whose active option follows the arrow keys", async () => {
38 render(() => (
39 <PreferencesProvider>
40 <CommandPalette open onOpenChange={() => {}} />
41 </PreferencesProvider>
42 ));
43
44 const field = await screen.findByRole("combobox");
45 const list = screen.getByRole("listbox");
46 expect(field.getAttribute("aria-controls")).toBe(list.id);
47
48 const options = screen.getAllByRole("option");
49 expect(options.length).toBeGreaterThan(1);
50 expect(field.getAttribute("aria-activedescendant")).toBe(options[0].id);
51 expect(options[0].getAttribute("aria-selected")).toBe("true");
52
53 fireEvent.keyDown(field, { key: "ArrowDown" });
54 expect(field.getAttribute("aria-activedescendant")).toBe(options[1].id);
55 expect(options[1].getAttribute("aria-selected")).toBe("true");
56});
57
58test("typing a course name lists matching courses and exams as their own groups", async () => {
59 render(() => (
60 <PreferencesProvider>
61 <CommandPalette open onOpenChange={() => {}} />
62 </PreferencesProvider>
63 ));
64
65 const field = await screen.findByRole("combobox");
66 fireEvent.input(field, { target: { value: "biyoloji" } });
67
68 const courseOption = await screen.findByRole("option", { name: /^Biyoloji/ });
69 expect(courseOption).toBeTruthy();
70 expect(screen.getByRole("option", { name: /1\. Yazılı/ })).toBeTruthy();
71 expect(screen.queryByRole("option", { name: /Kimya/ })).toBeNull();
72 expect(screen.getByText("Courses")).toBeTruthy();
73 expect(screen.getByText("Exams")).toBeTruthy();
74});