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 { |
| 3 | postClass, |
| 4 | getClasses, |
| 5 | getClassById, |
| 6 | getMyClasses, |
| 7 | getClassesByUserId, |
| 8 | patchClassById, |
| 9 | deleteClassById, |
| 10 | postClassMember, |
| 11 | getClassMembers, |
| 12 | deleteClassMember, |
| 13 | postClassInstance, |
| 14 | getClassInstances, |
| 15 | deleteClassInstance, |
| 16 | postClassBlueprint, |
| 17 | getClassBlueprints, |
| 18 | getClassBlueprintByGrade, |
| 19 | patchClassBlueprintByGrade, |
| 20 | deleteClassBlueprintByGrade, |
| 21 | postClassBlueprintApply, |
| 22 | getClassBlueprintStatus, |
| 23 | } from "../../classes"; |
| 24 | import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 25 | |
| 26 | describe("classes API", () => { |
| 27 | afterEach(() => { |
| 28 | vi.restoreAllMocks(); |
| 29 | }); |
| 30 | |
| 31 | // POST /classes answers with an envelope, not the bare class: the created |
| 32 | // row plus the pairs its grade's blueprint could not attach. |
| 33 | it("postClass POSTs /classes and returns the create envelope", async () => { |
| 34 | const envelope = { class: { id: "c1" }, skipped: [], stocked_from: null }; |
| 35 | mockFetchSuccess(envelope); |
| 36 | const body = { name: "9-A", grade: "9", year: "y1" }; |
| 37 | const created = await postClass(body); |
| 38 | expect(created).toEqual(envelope); |
| 39 | expect(created.class.id).toBe("c1"); |
| 40 | const [url, init] = lastFetchCall(); |
| 41 | expect(url).toBe("/api/classes"); |
| 42 | expect(init?.method).toBe("POST"); |
| 43 | expect(init?.body).toBe(JSON.stringify(body)); |
| 44 | }); |
| 45 | |
| 46 | it("getClasses GETs /classes with pagination", async () => { |
| 47 | mockFetchSuccess({ items: [{ id: "c1" }], total: 1 }); |
| 48 | await getClasses({ limit: 5, offset: 10 }); |
| 49 | const [url, init] = lastFetchCall(); |
| 50 | expect(url).toBe("/api/classes?limit=5&offset=10"); |
| 51 | expect(init?.method ?? "GET").toBe("GET"); |
| 52 | }); |
| 53 | |
| 54 | it("getClassById GETs /classes/:id", async () => { |
| 55 | mockFetchSuccess({ id: "c1" }); |
| 56 | await getClassById("c1"); |
| 57 | const [url] = lastFetchCall(); |
| 58 | expect(url).toBe("/api/classes/c1"); |
| 59 | }); |
| 60 | |
| 61 | it("getMyClasses GETs /classes/me with pagination", async () => { |
| 62 | mockFetchSuccess({ items: [{ id: "c1" }], total: 1 }); |
| 63 | await getMyClasses({ limit: 5, offset: 10 }); |
| 64 | const [url, init] = lastFetchCall(); |
| 65 | expect(url).toBe("/api/classes/me?limit=5&offset=10"); |
| 66 | expect(init?.method ?? "GET").toBe("GET"); |
| 67 | }); |
| 68 | |
| 69 | it("getClassesByUserId GETs /classes/user/:id with pagination", async () => { |
| 70 | mockFetchSuccess({ items: [{ id: "c1" }], total: 1 }); |
| 71 | await getClassesByUserId("u1", { limit: 5, offset: 10 }); |
| 72 | const [url] = lastFetchCall(); |
| 73 | expect(url).toBe("/api/classes/user/u1?limit=5&offset=10"); |
| 74 | }); |
| 75 | |
| 76 | it("patchClassById PATCHes /classes/:id with the body", async () => { |
| 77 | mockFetchSuccess({ id: "c1" }); |
| 78 | const body = { name: "9-B", grade: null, term_id: null }; |
| 79 | await patchClassById("c1", body); |
| 80 | const [url, init] = lastFetchCall(); |
| 81 | expect(url).toBe("/api/classes/c1"); |
| 82 | expect(init?.method).toBe("PATCH"); |
| 83 | expect(init?.body).toBe(JSON.stringify(body)); |
| 84 | }); |
| 85 | |
| 86 | it("deleteClassById DELETEs /classes/:id", async () => { |
| 87 | mockFetch204(); |
| 88 | await deleteClassById("c1"); |
| 89 | const [url, init] = lastFetchCall(); |
| 90 | expect(url).toBe("/api/classes/c1"); |
| 91 | expect(init?.method).toBe("DELETE"); |
| 92 | }); |
| 93 | |
| 94 | it("postClassMember POSTs /classes/:id/members with the body", async () => { |
| 95 | mockFetchSuccess({ id: "m1" }); |
| 96 | const body = { user_id: "u1" }; |
| 97 | await postClassMember("c1", body); |
| 98 | const [url, init] = lastFetchCall(); |
| 99 | expect(url).toBe("/api/classes/c1/members"); |
| 100 | expect(init?.method).toBe("POST"); |
| 101 | expect(init?.body).toBe(JSON.stringify(body)); |
| 102 | }); |
| 103 | |
| 104 | it("getClassMembers GETs /classes/:id/members with pagination", async () => { |
| 105 | mockFetchSuccess({ items: [], total: 0 }); |
| 106 | await getClassMembers("c1", { limit: 5, offset: 10 }); |
| 107 | const [url] = lastFetchCall(); |
| 108 | expect(url).toBe("/api/classes/c1/members?limit=5&offset=10"); |
| 109 | }); |
| 110 | |
| 111 | it("deleteClassMember DELETEs /classes/:id/members/:user", async () => { |
| 112 | mockFetch204(); |
| 113 | await deleteClassMember("c1", "u1"); |
| 114 | const [url, init] = lastFetchCall(); |
| 115 | expect(url).toBe("/api/classes/c1/members/u1"); |
| 116 | expect(init?.method).toBe("DELETE"); |
| 117 | }); |
| 118 | |
| 119 | it("postClassInstance POSTs /classes/:id/instances with the body", async () => { |
| 120 | mockFetchSuccess({ id: "cc1" }); |
| 121 | const body = { course_id: "co1" }; |
| 122 | await postClassInstance("c1", body); |
| 123 | const [url, init] = lastFetchCall(); |
| 124 | expect(url).toBe("/api/classes/c1/instances"); |
| 125 | expect(init?.method).toBe("POST"); |
| 126 | expect(init?.body).toBe(JSON.stringify(body)); |
| 127 | }); |
| 128 | |
| 129 | it("getClassInstances GETs /classes/:id/instances with pagination", async () => { |
| 130 | mockFetchSuccess({ items: [], total: 0 }); |
| 131 | await getClassInstances("c1", { limit: 5, offset: 10 }); |
| 132 | const [url] = lastFetchCall(); |
| 133 | expect(url).toBe("/api/classes/c1/instances?limit=5&offset=10"); |
| 134 | }); |
| 135 | |
| 136 | // The second segment is the instance id, not the course id — two şubeler |
| 137 | // teaching the same course hold two instances. |
| 138 | it("deleteClassInstance DELETEs /classes/:id/instances/:instance", async () => { |
| 139 | mockFetch204(); |
| 140 | await deleteClassInstance("c1", "i1"); |
| 141 | const [url, init] = lastFetchCall(); |
| 142 | expect(url).toBe("/api/classes/c1/instances/i1"); |
| 143 | expect(init?.method).toBe("DELETE"); |
| 144 | }); |
| 145 | |
| 146 | it("postClassBlueprint POSTs /classes/blueprints with the body", async () => { |
| 147 | mockFetchSuccess({ blueprint: { grade: "9", courses: [], creator: {} }, matched: 2, skipped: [] }); |
| 148 | const body = { grade: "9", course_ids: ["co1", "co2"] }; |
| 149 | await postClassBlueprint(body); |
| 150 | const [url, init] = lastFetchCall(); |
| 151 | expect(url).toBe("/api/classes/blueprints"); |
| 152 | expect(init?.method).toBe("POST"); |
| 153 | expect(init?.body).toBe(JSON.stringify(body)); |
| 154 | }); |
| 155 | |
| 156 | it("getClassBlueprints GETs /classes/blueprints with pagination", async () => { |
| 157 | mockFetchSuccess({ items: [], total: 0 }); |
| 158 | await getClassBlueprints({ limit: 20, offset: 40 }); |
| 159 | const [url] = lastFetchCall(); |
| 160 | expect(url).toBe("/api/classes/blueprints?limit=20&offset=40"); |
| 161 | }); |
| 162 | |
| 163 | it("getClassBlueprintByGrade encodes the grade, which is the record key", async () => { |
| 164 | mockFetchSuccess({ grade: "9/A", courses: [], creator: {} }); |
| 165 | await getClassBlueprintByGrade("9/A"); |
| 166 | const [url] = lastFetchCall(); |
| 167 | expect(url).toBe("/api/classes/blueprints/9%2FA"); |
| 168 | }); |
| 169 | |
| 170 | it("patchClassBlueprintByGrade PATCHes the whole course set", async () => { |
| 171 | mockFetchSuccess({ blueprint: { grade: "9", courses: [], creator: {} }, matched: 2, skipped: [] }); |
| 172 | const body = { course_ids: ["co1"] }; |
| 173 | await patchClassBlueprintByGrade("9", body); |
| 174 | const [url, init] = lastFetchCall(); |
| 175 | expect(url).toBe("/api/classes/blueprints/9"); |
| 176 | expect(init?.method).toBe("PATCH"); |
| 177 | expect(init?.body).toBe(JSON.stringify(body)); |
| 178 | }); |
| 179 | |
| 180 | it("deleteClassBlueprintByGrade DELETEs /classes/blueprints/:grade", async () => { |
| 181 | mockFetch204(); |
| 182 | await deleteClassBlueprintByGrade("9/A"); |
| 183 | const [url, init] = lastFetchCall(); |
| 184 | expect(url).toBe("/api/classes/blueprints/9%2FA"); |
| 185 | expect(init?.method).toBe("DELETE"); |
| 186 | }); |
| 187 | |
| 188 | it("postClassBlueprintApply POSTs /classes/:id/blueprint with no body", async () => { |
| 189 | mockFetchSuccess({ skipped: [] }); |
| 190 | await postClassBlueprintApply("c1"); |
| 191 | const [url, init] = lastFetchCall(); |
| 192 | expect(url).toBe("/api/classes/c1/blueprint"); |
| 193 | expect(init?.method).toBe("POST"); |
| 194 | expect(init?.body).toBeUndefined(); |
| 195 | }); |
| 196 | |
| 197 | it("getClassBlueprintStatus GETs /classes/blueprints/:grade/status", async () => { |
| 198 | const mockStatus = { grade: "9/A", courses: ["co1"], matched: 3, sections: [] }; |
| 199 | mockFetchSuccess(mockStatus); |
| 200 | const result = await getClassBlueprintStatus("9/A"); |
| 201 | expect(result).toEqual(mockStatus); |
| 202 | const [url, init] = lastFetchCall(); |
| 203 | expect(url).toBe("/api/classes/blueprints/9%2FA/status"); |
| 204 | expect(init?.method).toBe("GET"); |
| 205 | }); |
| 206 | }); |