hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | import { deleteSchoolBySlug } from "../../schools"; |
| 3 | import { deleteSchoolModule } from "../../schools"; |
| 4 | import { getSchoolBySlug } from "../../schools"; |
| 5 | import { getSchoolModules } from "../../schools"; |
| 6 | import { getSchools } from "../../schools"; |
| 7 | import { patchSchoolBySlug } from "../../schools"; |
| 8 | import { patchSchoolModules } from "../../schools"; |
| 9 | import { postSchool } from "../../schools"; |
| 10 | import { postSchoolAdminPassword } from "../../schools"; |
| 11 | import { postSchoolEnter } from "../../schools"; |
| 12 | import { postSchoolModule } from "../../schools"; |
| 13 | import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 14 | |
| 15 | const school = { slug: "ata-koleji", name: "Ata Koleji", status: "active", created_at: 1, modules: ["courses"] }; |
| 16 | const modules = { enabled: ["courses"], disabled: ["exams"] }; |
| 17 | |
| 18 | describe("schools API", () => { |
| 19 | afterEach(() => { |
| 20 | vi.restoreAllMocks(); |
| 21 | }); |
| 22 | |
| 23 | it("getSchools pages /schools", async () => { |
| 24 | mockFetchSuccess({ items: [school], total: 1, limit: 20, offset: 0 }); |
| 25 | |
| 26 | const result = await getSchools({ limit: 20, offset: 0 }); |
| 27 | expect(result.items).toEqual([school]); |
| 28 | |
| 29 | const [url, init] = lastFetchCall(); |
| 30 | expect(url).toBe("/api/schools?limit=20&offset=0"); |
| 31 | expect(init?.method).toBe("GET"); |
| 32 | }); |
| 33 | |
| 34 | it("getSchoolBySlug reads /schools/{slug}", async () => { |
| 35 | mockFetchSuccess(school); |
| 36 | |
| 37 | await getSchoolBySlug("ata-koleji"); |
| 38 | |
| 39 | const [url, init] = lastFetchCall(); |
| 40 | expect(url).toBe("/api/schools/ata-koleji"); |
| 41 | expect(init?.method).toBe("GET"); |
| 42 | }); |
| 43 | |
| 44 | it("postSchool posts the school and its first admin", async () => { |
| 45 | mockFetchSuccess(school, 201); |
| 46 | const body = { slug: "ata-koleji", name: "Ata Koleji", admin_username: "admin", admin_password: "secret123", modules: ["courses"] }; |
| 47 | |
| 48 | await postSchool(body); |
| 49 | |
| 50 | const [url, init] = lastFetchCall(); |
| 51 | expect(url).toBe("/api/schools"); |
| 52 | expect(init?.method).toBe("POST"); |
| 53 | expect(init?.body).toBe(JSON.stringify(body)); |
| 54 | }); |
| 55 | |
| 56 | it("patchSchoolBySlug patches name and status", async () => { |
| 57 | mockFetchSuccess({ ...school, status: "suspended" }); |
| 58 | |
| 59 | await patchSchoolBySlug("ata-koleji", { status: "suspended" }); |
| 60 | |
| 61 | const [url, init] = lastFetchCall(); |
| 62 | expect(url).toBe("/api/schools/ata-koleji"); |
| 63 | expect(init?.method).toBe("PATCH"); |
| 64 | expect(init?.body).toBe(JSON.stringify({ status: "suspended" })); |
| 65 | }); |
| 66 | |
| 67 | it("deleteSchoolBySlug deletes /schools/{slug}", async () => { |
| 68 | mockFetch204(); |
| 69 | |
| 70 | await deleteSchoolBySlug("ata-koleji"); |
| 71 | |
| 72 | const [url, init] = lastFetchCall(); |
| 73 | expect(url).toBe("/api/schools/ata-koleji"); |
| 74 | expect(init?.method).toBe("DELETE"); |
| 75 | }); |
| 76 | |
| 77 | it("postSchoolAdminPassword re-keys an admin", async () => { |
| 78 | mockFetch204(); |
| 79 | |
| 80 | await postSchoolAdminPassword("ata-koleji", { username: "admin", password: "newsecret" }); |
| 81 | |
| 82 | const [url, init] = lastFetchCall(); |
| 83 | expect(url).toBe("/api/schools/ata-koleji/admin-password"); |
| 84 | expect(init?.method).toBe("POST"); |
| 85 | expect(init?.body).toBe(JSON.stringify({ username: "admin", password: "newsecret" })); |
| 86 | }); |
| 87 | |
| 88 | it("postSchoolEnter enters as a named admin", async () => { |
| 89 | mockFetchSuccess({ id: "u1", username: "admin", role: "admin" }); |
| 90 | |
| 91 | await postSchoolEnter("ata-koleji", { username: "admin" }); |
| 92 | |
| 93 | const [url, init] = lastFetchCall(); |
| 94 | expect(url).toBe("/api/schools/ata-koleji/enter"); |
| 95 | expect(init?.method).toBe("POST"); |
| 96 | expect(init?.body).toBe(JSON.stringify({ username: "admin" })); |
| 97 | }); |
| 98 | |
| 99 | it("getSchoolModules reads both halves of the catalog", async () => { |
| 100 | mockFetchSuccess(modules); |
| 101 | |
| 102 | const result = await getSchoolModules("ata-koleji"); |
| 103 | expect(result).toEqual(modules); |
| 104 | |
| 105 | const [url, init] = lastFetchCall(); |
| 106 | expect(url).toBe("/api/schools/ata-koleji/modules"); |
| 107 | expect(init?.method).toBe("GET"); |
| 108 | }); |
| 109 | |
| 110 | it("patchSchoolModules re-sells the shelf in one write", async () => { |
| 111 | mockFetchSuccess(modules); |
| 112 | const body = { enable: ["exams"], disable_packages: ["ai"] }; |
| 113 | |
| 114 | await patchSchoolModules("ata-koleji", body); |
| 115 | |
| 116 | const [url, init] = lastFetchCall(); |
| 117 | expect(url).toBe("/api/schools/ata-koleji/modules"); |
| 118 | expect(init?.method).toBe("PATCH"); |
| 119 | expect(init?.body).toBe(JSON.stringify(body)); |
| 120 | }); |
| 121 | |
| 122 | it("postSchoolModule enables one module", async () => { |
| 123 | mockFetchSuccess(modules); |
| 124 | |
| 125 | await postSchoolModule("ata-koleji", "exams"); |
| 126 | |
| 127 | const [url, init] = lastFetchCall(); |
| 128 | expect(url).toBe("/api/schools/ata-koleji/modules/exams"); |
| 129 | expect(init?.method).toBe("POST"); |
| 130 | }); |
| 131 | |
| 132 | it("deleteSchoolModule disables one module", async () => { |
| 133 | mockFetchSuccess(modules); |
| 134 | |
| 135 | await deleteSchoolModule("ata-koleji", "exams"); |
| 136 | |
| 137 | const [url, init] = lastFetchCall(); |
| 138 | expect(url).toBe("/api/schools/ata-koleji/modules/exams"); |
| 139 | expect(init?.method).toBe("DELETE"); |
| 140 | }); |
| 141 | }); |