hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { cleanup, fireEvent, render, screen } from "@solidjs/testing-library"; |
| 2 | import { afterEach, beforeEach, expect, test, vi } from "vitest"; |
| 3 | import { CommandPalette } from "@/components/layout/command-palette"; |
| 4 | import { PreferencesProvider } from "@/stores/preferences-context"; |
| 5 | |
| 6 | vi.mock("@tanstack/solid-router", () => ({ useNavigate: () => vi.fn() })); |
| 7 | vi.mock("@/stores/auth-context", () => ({ |
| 8 | useAuth: () => ({ |
| 9 | user: () => ({ id: "u-1", username: "teacher", role: "teacher" }), |
| 10 | loading: () => false, |
| 11 | error: () => undefined, |
| 12 | }), |
| 13 | })); |
| 14 | vi.mock("@/stores/modules-context", () => ({ |
| 15 | useModules: () => ({ enabled: () => null, loading: () => false, isEnabled: () => true, refresh: () => {} }), |
| 16 | })); |
| 17 | vi.mock("@/api/users", () => ({ getUserSearch: vi.fn(async () => ({ items: [], total: 0, limit: 10, offset: 0 })) })); |
| 18 | vi.mock("@/api/classes", () => ({ getMyClasses: vi.fn(async () => ({ items: [], total: 0, limit: 10, offset: 0 })) })); |
| 19 | vi.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 | |
| 28 | beforeEach(() => { |
| 29 | vi.spyOn(window, "scrollTo").mockImplementation(() => {}); |
| 30 | Element.prototype.scrollIntoView = vi.fn(); |
| 31 | }); |
| 32 | afterEach(() => { |
| 33 | cleanup(); |
| 34 | vi.restoreAllMocks(); |
| 35 | }); |
| 36 | |
| 37 | test("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 | |
| 58 | test("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 | }); |