hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 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 { getCourseMembers } from "../../courses"; |
| 8 | import { postCourseMember } from "../../courses"; |
| 9 | import { deleteCourseMemberByUserId } from "../../courses"; |
| 10 | import { getCourseSubjects } from "../../courses"; |
| 11 | import { postCourseSubject } from "../../courses"; |
| 12 | import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 13 | |
| 14 | describe("courses API", () => { |
| 15 | afterEach(() => { |
| 16 | vi.restoreAllMocks(); |
| 17 | }); |
| 18 | |
| 19 | it("getCourses calls /courses with pagination", async () => { |
| 20 | const mockPage = { items: [{ id: "c1" }], total: 1 }; |
| 21 | mockFetchSuccess(mockPage); |
| 22 | |
| 23 | const result = await getCourses({ limit: 10 }); |
| 24 | expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); |
| 25 | |
| 26 | const [url, init] = lastFetchCall(); |
| 27 | expect(url).toBe("/api/courses?limit=10"); |
| 28 | expect(init?.method).toBe("GET"); |
| 29 | }); |
| 30 | |
| 31 | it("getCourses sends pagination only — the endpoint has no kind/term/search filter", async () => { |
| 32 | mockFetchSuccess({ items: [], total: 0 }); |
| 33 | await getCourses({ limit: 12, offset: 24 }); |
| 34 | expect(lastFetchCall()[0]).toBe("/api/courses?limit=12&offset=24"); |
| 35 | }); |
| 36 | |
| 37 | it("getCourseById calls /courses/:id", async () => { |
| 38 | const mockCourse = { id: "c1", title: "Course" }; |
| 39 | mockFetchSuccess(mockCourse); |
| 40 | |
| 41 | const result = await getCourseById("c1"); |
| 42 | expect(result).toEqual(mockCourse); |
| 43 | |
| 44 | const [url, init] = lastFetchCall(); |
| 45 | expect(url).toBe("/api/courses/c1"); |
| 46 | expect(init?.method).toBe("GET"); |
| 47 | }); |
| 48 | |
| 49 | it("postCourse calls /courses with data", async () => { |
| 50 | const mockCourse = { id: "c1" }; |
| 51 | mockFetchSuccess(mockCourse); |
| 52 | |
| 53 | const data = { title: "New Course", description: "Desc", kind: "course" }; |
| 54 | const result = await postCourse(data); |
| 55 | expect(result).toEqual(mockCourse); |
| 56 | |
| 57 | const [url, init] = lastFetchCall(); |
| 58 | expect(url).toBe("/api/courses"); |
| 59 | expect(init?.method).toBe("POST"); |
| 60 | expect(init?.body).toBe(JSON.stringify(data)); |
| 61 | }); |
| 62 | |
| 63 | it("patchCourseById calls /courses/:id with updates", async () => { |
| 64 | const mockCourse = { id: "c1", title: "Updated" }; |
| 65 | mockFetchSuccess(mockCourse); |
| 66 | |
| 67 | const updates = { title: "Updated" }; |
| 68 | const result = await patchCourseById("c1", updates); |
| 69 | expect(result).toEqual(mockCourse); |
| 70 | |
| 71 | const [url, init] = lastFetchCall(); |
| 72 | expect(url).toBe("/api/courses/c1"); |
| 73 | expect(init?.method).toBe("PATCH"); |
| 74 | expect(init?.body).toBe(JSON.stringify(updates)); |
| 75 | }); |
| 76 | |
| 77 | it("deleteCourseById calls /courses/:id", async () => { |
| 78 | mockFetch204(); |
| 79 | |
| 80 | await deleteCourseById("c1"); |
| 81 | |
| 82 | const [url, init] = lastFetchCall(); |
| 83 | expect(url).toBe("/api/courses/c1"); |
| 84 | expect(init?.method).toBe("DELETE"); |
| 85 | }); |
| 86 | |
| 87 | it("getCourseSubjects calls /courses/:id/subjects", async () => { |
| 88 | const mockPage = { items: [{ id: "sub1" }], total: 1 }; |
| 89 | mockFetchSuccess(mockPage); |
| 90 | |
| 91 | const result = await getCourseSubjects("c1"); |
| 92 | expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); |
| 93 | |
| 94 | const [url, init] = lastFetchCall(); |
| 95 | expect(url).toBe("/api/courses/c1/subjects"); |
| 96 | expect(init?.method).toBe("GET"); |
| 97 | }); |
| 98 | |
| 99 | it("postCourseSubject calls /courses/:id/subjects", async () => { |
| 100 | const mockSub = { id: "sub1", course: "c1" }; |
| 101 | mockFetchSuccess(mockSub); |
| 102 | |
| 103 | const data = { name: "Sub", description: "Desc" }; |
| 104 | const result = await postCourseSubject("c1", data); |
| 105 | expect(result).toEqual(mockSub); |
| 106 | |
| 107 | const [url, init] = lastFetchCall(); |
| 108 | expect(url).toBe("/api/courses/c1/subjects"); |
| 109 | expect(init?.method).toBe("POST"); |
| 110 | expect(init?.body).toBe(JSON.stringify(data)); |
| 111 | }); |
| 112 | |
| 113 | it("getCourseMembers calls /courses/:id/members with pagination", async () => { |
| 114 | mockFetchSuccess({ items: [{ id: "m1" }], total: 1 }); |
| 115 | |
| 116 | const result = await getCourseMembers("c1", { limit: 10 }); |
| 117 | expect(result.items).toEqual([{ id: "m1" }]); |
| 118 | |
| 119 | const [url, init] = lastFetchCall(); |
| 120 | expect(url).toBe("/api/courses/c1/members?limit=10"); |
| 121 | expect(init?.method).toBe("GET"); |
| 122 | }); |
| 123 | |
| 124 | it("postCourseMember calls /courses/:id/members with the user id", async () => { |
| 125 | mockFetchSuccess({ id: "m1" }); |
| 126 | |
| 127 | await postCourseMember("c1", "u1"); |
| 128 | |
| 129 | const [url, init] = lastFetchCall(); |
| 130 | expect(url).toBe("/api/courses/c1/members"); |
| 131 | expect(init?.method).toBe("POST"); |
| 132 | expect(init?.body).toBe(JSON.stringify({ user_id: "u1" })); |
| 133 | }); |
| 134 | |
| 135 | it("deleteCourseMemberByUserId calls /courses/:id/members/:userId", async () => { |
| 136 | mockFetch204(); |
| 137 | |
| 138 | await deleteCourseMemberByUserId("c1", "u1"); |
| 139 | |
| 140 | const [url, init] = lastFetchCall(); |
| 141 | expect(url).toBe("/api/courses/c1/members/u1"); |
| 142 | expect(init?.method).toBe("DELETE"); |
| 143 | }); |
| 144 | |
| 145 | }); |