hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library"; |
| 2 | import type { JSX } from "solid-js"; |
| 3 | import { afterEach, expect, test, vi } from "vitest"; |
| 4 | import ClassesPage from "@/pages/classes-page"; |
| 5 | import { PreferencesProvider } from "@/stores/preferences-context"; |
| 6 | |
| 7 | const { getClasses, getClassMembers, postClass, getCourses, navigate } = vi.hoisted(() => ({ |
| 8 | getClasses: vi.fn(), |
| 9 | getClassMembers: vi.fn(), |
| 10 | postClass: vi.fn(), |
| 11 | getCourses: vi.fn(), |
| 12 | navigate: vi.fn(), |
| 13 | })); |
| 14 | |
| 15 | vi.mock("@tanstack/solid-router", () => ({ |
| 16 | Link: (props: { to: string; children: JSX.Element; class?: string }) => <a href={props.to} class={props.class}>{props.children}</a>, |
| 17 | Navigate: () => null, |
| 18 | useNavigate: () => navigate, |
| 19 | })); |
| 20 | vi.mock("@/stores/auth-context", () => ({ |
| 21 | useAuth: () => ({ |
| 22 | user: () => ({ id: "u-1", username: "boss", role: "manager" }), |
| 23 | loading: () => false, |
| 24 | error: () => null, |
| 25 | refresh: vi.fn(), |
| 26 | }), |
| 27 | })); |
| 28 | vi.mock("@/api/classes", () => ({ getClasses, getClassMembers, postClass })); |
| 29 | vi.mock("@/api/academic-years", () => ({ getAcademicYears: vi.fn(async () => ({ items: [] })) })); |
| 30 | vi.mock("@/api/limits", () => ({ |
| 31 | getLimits: vi.fn(async () => ({ course: { max_class_name_len: 60, max_class_grade_len: 10 } })), |
| 32 | })); |
| 33 | vi.mock("@/api/courses", () => ({ getCourses })); |
| 34 | vi.mock("@/api/users", () => ({ getUserSearch: vi.fn(async () => ({ items: [] })) })); |
| 35 | |
| 36 | const createdClass = { id: "c-new", creator: null, name: "9-A", grade: "9", year: null, teacher: null }; |
| 37 | |
| 38 | afterEach(() => { |
| 39 | cleanup(); |
| 40 | vi.clearAllMocks(); |
| 41 | }); |
| 42 | |
| 43 | async function openCreateForm() { |
| 44 | getClasses.mockResolvedValue({ items: [] }); |
| 45 | getClassMembers.mockResolvedValue({ items: [], total: 0 }); |
| 46 | render(() => <PreferencesProvider><ClassesPage /></PreferencesProvider>); |
| 47 | |
| 48 | fireEvent.click(await screen.findByRole("button", { name: /New class/ })); |
| 49 | fireEvent.input(await screen.findByLabelText(/^Name/), { target: { value: "9-A" } }); |
| 50 | fireEvent.click(screen.getByRole("button", { name: "Create" })); |
| 51 | } |
| 52 | |
| 53 | // The 201 is an envelope, not the class: opening the route with `undefined` |
| 54 | // 404s the class it just made. |
| 55 | test("creating a class opens the class the server made", async () => { |
| 56 | postClass.mockResolvedValue({ class: createdClass, skipped: [], stocked_from: null }); |
| 57 | |
| 58 | await openCreateForm(); |
| 59 | |
| 60 | await waitFor(() => |
| 61 | expect(navigate).toHaveBeenCalledWith({ to: "/management/classes/$id", params: { id: "c-new" } }), |
| 62 | ); |
| 63 | expect(postClass).toHaveBeenCalledWith({ name: "9-A", grade: undefined, year: undefined, teacher_id: undefined }); |
| 64 | }); |
| 65 | |
| 66 | test("a create whose blueprint left pairs behind holds the page until the report closes", async () => { |
| 67 | postClass.mockResolvedValue({ |
| 68 | class: createdClass, |
| 69 | skipped: [{ class: "c-new", class_name: "9-A", course: "k1", reason: "the class is already at its course ceiling" }], |
| 70 | stocked_from: "9", |
| 71 | }); |
| 72 | getCourses.mockResolvedValue({ items: [{ id: "k1", title: "Algebra" }] }); |
| 73 | |
| 74 | await openCreateForm(); |
| 75 | |
| 76 | expect(await screen.findByText(/could not be attached/)).toBeTruthy(); |
| 77 | expect(navigate).not.toHaveBeenCalled(); |
| 78 | |
| 79 | fireEvent.click(screen.getByRole("button", { name: "See details" })); |
| 80 | expect(await screen.findByText("Not attached")).toBeTruthy(); |
| 81 | expect(await screen.findByText("Algebra")).toBeTruthy(); |
| 82 | |
| 83 | fireEvent.click(screen.getByRole("button", { name: "Close" })); |
| 84 | |
| 85 | await waitFor(() => |
| 86 | expect(navigate).toHaveBeenCalledWith({ to: "/management/classes/$id", params: { id: "c-new" } }), |
| 87 | ); |
| 88 | }); |
| 89 | |
| 90 | test("a blank class name is flagged under the field, not sent", async () => { |
| 91 | getClasses.mockResolvedValue({ items: [] }); |
| 92 | render(() => <PreferencesProvider><ClassesPage /></PreferencesProvider>); |
| 93 | |
| 94 | fireEvent.click(await screen.findByRole("button", { name: /New class/ })); |
| 95 | const field = await screen.findByLabelText(/^Name/); |
| 96 | fireEvent.input(field, { target: { value: " " } }); |
| 97 | fireEvent.click(screen.getByRole("button", { name: "Create" })); |
| 98 | |
| 99 | expect(await screen.findByText(/This field is required|Bu alan zorunlu/)).toBeTruthy(); |
| 100 | expect(field.getAttribute("aria-invalid")).toBe("true"); |
| 101 | expect(field.getAttribute("aria-describedby")).toBe("class-name-error"); |
| 102 | expect(postClass).not.toHaveBeenCalled(); |
| 103 | }); |