hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBc35a83aBurak Boduroğlu4 months ago440 commits
| 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | import { getCourses } from "../../courses"; |
| 3 | import { getCourseById } from "../../courses"; |
| 4 | import { postCourse } from "../../courses"; |
| 5 | import { patchCourseById } from "../../courses"; |
| 6 | import { deleteCourseById } from "../../courses"; |
| 7 | import { getCourseEnrollments } from "../../courses"; |
| 8 | import { postCourseEnrollment } from "../../courses"; |
| 9 | import { deleteCourseEnrollmentByUserId } from "../../courses"; |
| 10 | import { getCourseSessions } from "../../courses"; |
| 11 | import { postCourseSession } from "../../courses"; |
| 12 | import { getCourseSubjects } from "../../courses"; |
| 13 | import { postCourseSubject } from "../../courses"; |
| 14 | import { getCourseExams } from "../../courses"; |
| 15 | import { postCourseExam } from "../../courses"; |
| 16 | import { postCourseTeacher } from "../../courses"; |
| 17 | import { deleteCourseTeacherByUserId } from "../../courses"; |
| 18 | import { getCourseHomework } from "../../courses"; |
| 19 | import { postCourseHomework } from "../../courses"; |
| 20 | import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 21 | |
| 22 | describe("courses API", () => { |
| 23 | afterEach(() => { |
| 24 | vi.restoreAllMocks(); |
| 25 | }); |
| 26 | |
| 27 | it("postCourseTeacher calls /courses/:id/teachers", async () => { |
| 28 | const mockCourse = { id: "c1", title: "Course", teachers: [{ id: "t1", username: "teacher1", display_name: "Teacher 1" }] }; |
| 29 | mockFetchSuccess(mockCourse); |
| 30 | |
| 31 | const result = await postCourseTeacher("c1", "t1"); |
| 32 | expect(result).toEqual(mockCourse); |
| 33 | |
| 34 | const [url, init] = lastFetchCall(); |
| 35 | expect(url).toBe("/api/courses/c1/teachers"); |
| 36 | expect(init?.method).toBe("POST"); |
| 37 | expect(init?.body).toBe(JSON.stringify({ user_id: "t1" })); |
| 38 | }); |
| 39 | |
| 40 | it("deleteCourseTeacherByUserId calls /courses/:id/teachers/:userId", async () => { |
| 41 | mockFetch204(); |
| 42 | |
| 43 | await deleteCourseTeacherByUserId("c1", "t1"); |
| 44 | |
| 45 | const [url, init] = lastFetchCall(); |
| 46 | expect(url).toBe("/api/courses/c1/teachers/t1"); |
| 47 | expect(init?.method).toBe("DELETE"); |
| 48 | }); |
| 49 | |
| 50 | |
| 51 | it("getCourses calls /courses with pagination", async () => { |
| 52 | const mockPage = { items: [{ id: "c1" }], total: 1 }; |
| 53 | mockFetchSuccess(mockPage); |
| 54 | |
| 55 | const result = await getCourses({ limit: 10 }); |
| 56 | expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); |
| 57 | |
| 58 | const [url, init] = lastFetchCall(); |
| 59 | expect(url).toBe("/api/courses?limit=10"); |
| 60 | expect(init?.method).toBe("GET"); |
| 61 | }); |
| 62 | |
| 63 | it("getCourses sends server-side kind, search, and term filters", async () => { |
| 64 | mockFetchSuccess({ items: [], total: 0 }); |
| 65 | await getCourses({ limit: 12, offset: 24, kind: "course", q: "math", term_id: "none" }); |
| 66 | expect(lastFetchCall()[0]).toBe("/api/courses?limit=12&offset=24&kind=course&q=math&term_id=none"); |
| 67 | }); |
| 68 | |
| 69 | it("getCourseById calls /courses/:id", async () => { |
| 70 | const mockCourse = { id: "c1", title: "Course" }; |
| 71 | mockFetchSuccess(mockCourse); |
| 72 | |
| 73 | const result = await getCourseById("c1"); |
| 74 | expect(result).toEqual(mockCourse); |
| 75 | |
| 76 | const [url, init] = lastFetchCall(); |
| 77 | expect(url).toBe("/api/courses/c1"); |
| 78 | expect(init?.method).toBe("GET"); |
| 79 | }); |
| 80 | |
| 81 | it("postCourse calls /courses with data", async () => { |
| 82 | const mockCourse = { id: "c1" }; |
| 83 | mockFetchSuccess(mockCourse); |
| 84 | |
| 85 | const data = { title: "New Course", description: "Desc", kind: "course" }; |
| 86 | const result = await postCourse(data); |
| 87 | expect(result).toEqual(mockCourse); |
| 88 | |
| 89 | const [url, init] = lastFetchCall(); |
| 90 | expect(url).toBe("/api/courses"); |
| 91 | expect(init?.method).toBe("POST"); |
| 92 | expect(init?.body).toBe(JSON.stringify(data)); |
| 93 | }); |
| 94 | |
| 95 | it("patchCourseById calls /courses/:id with updates", async () => { |
| 96 | const mockCourse = { id: "c1", title: "Updated" }; |
| 97 | mockFetchSuccess(mockCourse); |
| 98 | |
| 99 | const updates = { title: "Updated" }; |
| 100 | const result = await patchCourseById("c1", updates); |
| 101 | expect(result).toEqual(mockCourse); |
| 102 | |
| 103 | const [url, init] = lastFetchCall(); |
| 104 | expect(url).toBe("/api/courses/c1"); |
| 105 | expect(init?.method).toBe("PATCH"); |
| 106 | expect(init?.body).toBe(JSON.stringify(updates)); |
| 107 | }); |
| 108 | |
| 109 | it("deleteCourseById calls /courses/:id", async () => { |
| 110 | mockFetch204(); |
| 111 | |
| 112 | await deleteCourseById("c1"); |
| 113 | |
| 114 | const [url, init] = lastFetchCall(); |
| 115 | expect(url).toBe("/api/courses/c1"); |
| 116 | expect(init?.method).toBe("DELETE"); |
| 117 | }); |
| 118 | |
| 119 | it("getCourseEnrollments calls /courses/:id/enrollments", async () => { |
| 120 | const mockPage = { items: [{ id: "en1" }], total: 1 }; |
| 121 | mockFetchSuccess(mockPage); |
| 122 | |
| 123 | const result = await getCourseEnrollments("c1"); |
| 124 | expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); |
| 125 | |
| 126 | const [url, init] = lastFetchCall(); |
| 127 | expect(url).toBe("/api/courses/c1/enrollments"); |
| 128 | expect(init?.method).toBe("GET"); |
| 129 | }); |
| 130 | |
| 131 | it("postCourseEnrollment calls /courses/:id/enrollments", async () => { |
| 132 | const mockEnr = { id: "en1", course: "c1" }; |
| 133 | mockFetchSuccess(mockEnr); |
| 134 | |
| 135 | const result = await postCourseEnrollment("c1", "u1"); |
| 136 | expect(result).toEqual(mockEnr); |
| 137 | |
| 138 | const [url, init] = lastFetchCall(); |
| 139 | expect(url).toBe("/api/courses/c1/enrollments"); |
| 140 | expect(init?.method).toBe("POST"); |
| 141 | expect(init?.body).toBe(JSON.stringify({ user_id: "u1" })); |
| 142 | }); |
| 143 | |
| 144 | it("deleteCourseEnrollmentByUserId calls /courses/:id/enrollments/:userId", async () => { |
| 145 | mockFetch204(); |
| 146 | |
| 147 | await deleteCourseEnrollmentByUserId("c1", "u1"); |
| 148 | |
| 149 | const [url, init] = lastFetchCall(); |
| 150 | expect(url).toBe("/api/courses/c1/enrollments/u1"); |
| 151 | expect(init?.method).toBe("DELETE"); |
| 152 | }); |
| 153 | |
| 154 | it("getCourseSessions calls /courses/:id/sessions", async () => { |
| 155 | const mockPage = { items: [{ id: "s1" }], total: 1 }; |
| 156 | mockFetchSuccess(mockPage); |
| 157 | |
| 158 | const result = await getCourseSessions("c1"); |
| 159 | expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); |
| 160 | |
| 161 | const [url, init] = lastFetchCall(); |
| 162 | expect(url).toBe("/api/courses/c1/sessions"); |
| 163 | expect(init?.method).toBe("GET"); |
| 164 | }); |
| 165 | |
| 166 | it("postCourseSession calls /courses/:id/sessions", async () => { |
| 167 | const mockSession = { id: "s1", course: "c1" }; |
| 168 | mockFetchSuccess(mockSession); |
| 169 | |
| 170 | const data = { topic: "Topic", starts_at: 1000 }; |
| 171 | const result = await postCourseSession("c1", data); |
| 172 | expect(result).toEqual(mockSession); |
| 173 | |
| 174 | const [url, init] = lastFetchCall(); |
| 175 | expect(url).toBe("/api/courses/c1/sessions"); |
| 176 | expect(init?.method).toBe("POST"); |
| 177 | expect(init?.body).toBe(JSON.stringify(data)); |
| 178 | }); |
| 179 | |
| 180 | it("getCourseSubjects calls /courses/:id/subjects", async () => { |
| 181 | const mockPage = { items: [{ id: "sub1" }], total: 1 }; |
| 182 | mockFetchSuccess(mockPage); |
| 183 | |
| 184 | const result = await getCourseSubjects("c1"); |
| 185 | expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); |
| 186 | |
| 187 | const [url, init] = lastFetchCall(); |
| 188 | expect(url).toBe("/api/courses/c1/subjects"); |
| 189 | expect(init?.method).toBe("GET"); |
| 190 | }); |
| 191 | |
| 192 | it("postCourseSubject calls /courses/:id/subjects", async () => { |
| 193 | const mockSub = { id: "sub1", course: "c1" }; |
| 194 | mockFetchSuccess(mockSub); |
| 195 | |
| 196 | const data = { name: "Sub", description: "Desc" }; |
| 197 | const result = await postCourseSubject("c1", data); |
| 198 | expect(result).toEqual(mockSub); |
| 199 | |
| 200 | const [url, init] = lastFetchCall(); |
| 201 | expect(url).toBe("/api/courses/c1/subjects"); |
| 202 | expect(init?.method).toBe("POST"); |
| 203 | expect(init?.body).toBe(JSON.stringify(data)); |
| 204 | }); |
| 205 | |
| 206 | it("getCourseExams calls /courses/:id/exams", async () => { |
| 207 | const mockPage = { items: [{ id: "ex1" }], total: 1 }; |
| 208 | mockFetchSuccess(mockPage); |
| 209 | |
| 210 | const result = await getCourseExams("c1"); |
| 211 | expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); |
| 212 | |
| 213 | const [url, init] = lastFetchCall(); |
| 214 | expect(url).toBe("/api/courses/c1/exams"); |
| 215 | expect(init?.method).toBe("GET"); |
| 216 | }); |
| 217 | |
| 218 | it("postCourseExam calls /courses/:id/exams", async () => { |
| 219 | const mockExam = { id: "ex1", course: "c1" }; |
| 220 | mockFetchSuccess(mockExam); |
| 221 | |
| 222 | const data = { title: "Exam", kind: "quiz" }; |
| 223 | const result = await postCourseExam("c1", data); |
| 224 | expect(result).toEqual(mockExam); |
| 225 | |
| 226 | const [url, init] = lastFetchCall(); |
| 227 | expect(url).toBe("/api/courses/c1/exams"); |
| 228 | expect(init?.method).toBe("POST"); |
| 229 | expect(init?.body).toBe(JSON.stringify(data)); |
| 230 | }); |
| 231 | |
| 232 | it("getCourseHomework calls /courses/:id/homework", async () => { |
| 233 | const mockPage = { items: [{ id: "hw1" }], total: 1 }; |
| 234 | mockFetchSuccess(mockPage); |
| 235 | |
| 236 | const result = await getCourseHomework("c1", { limit: 5 }); |
| 237 | expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); |
| 238 | |
| 239 | const [url, init] = lastFetchCall(); |
| 240 | expect(url).toBe("/api/courses/c1/homework?limit=5"); |
| 241 | expect(init?.method).toBe("GET"); |
| 242 | }); |
| 243 | |
| 244 | it("postCourseHomework calls /courses/:id/homework", async () => { |
| 245 | const mockHomework = { id: "hw1", course: "c1" }; |
| 246 | mockFetchSuccess(mockHomework); |
| 247 | |
| 248 | const data = { title: "HW", subject_id: "sub1", due_at: 1900000000000, assigned: ["u1"] }; |
| 249 | const result = await postCourseHomework("c1", data); |
| 250 | expect(result).toEqual(mockHomework); |
| 251 | |
| 252 | const [url, init] = lastFetchCall(); |
| 253 | expect(url).toBe("/api/courses/c1/homework"); |
| 254 | expect(init?.method).toBe("POST"); |
| 255 | expect(init?.body).toBe(JSON.stringify(data)); |
| 256 | }); |
| 257 | }); |