forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

courses.test.ts164 lines, 5.6 KiBHistoryRawDownload
1import { afterEach, describe, expect, it, vi } from "vitest";
2import { getCourses } from "../../courses";
3import { getCourseById } from "../../courses";
4import { postCourse } from "../../courses";
5import { patchCourseById } from "../../courses";
6import { deleteCourseById } from "../../courses";
7import { getCourseMembers } from "../../courses";
8import { postCourseMember } from "../../courses";
9import { deleteCourseMemberByUserId } from "../../courses";
10import { getCourseSubjects } from "../../courses";
11import { postCourseSubject } from "../../courses";
12import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch";
13
14describe("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 keeps pagination when no filter is set", 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("getCourses sends kind and a trimmed q only when set", async () => {
38 mockFetchSuccess({ items: [], total: 0 });
39 await getCourses({ limit: 10, offset: 20, kind: "study", q: " algebra " });
40 expect(lastFetchCall()[0]).toBe("/api/courses?limit=10&offset=20&kind=study&q=algebra");
41 });
42
43 it("getCourses serializes taught as true/false and omits blank q", async () => {
44 mockFetchSuccess({ items: [], total: 0 });
45 // Blank q drops the key entirely — the API 400s on `?q=`.
46 await getCourses({ q: " " });
47 expect(lastFetchCall()[0]).toBe("/api/courses");
48
49 await getCourses({ taught: true, limit: 10 });
50 expect(lastFetchCall()[0]).toBe("/api/courses?limit=10&taught=true");
51
52 await getCourses({ taught: false, offset: 5 });
53 expect(lastFetchCall()[0]).toBe("/api/courses?offset=5&taught=false");
54 });
55
56 it("getCourseById calls /courses/:id", async () => {
57 const mockCourse = { id: "c1", title: "Course" };
58 mockFetchSuccess(mockCourse);
59
60 const result = await getCourseById("c1");
61 expect(result).toEqual(mockCourse);
62
63 const [url, init] = lastFetchCall();
64 expect(url).toBe("/api/courses/c1");
65 expect(init?.method).toBe("GET");
66 });
67
68 it("postCourse calls /courses with data", async () => {
69 const mockCourse = { id: "c1" };
70 mockFetchSuccess(mockCourse);
71
72 const data = { title: "New Course", description: "Desc", kind: "course" };
73 const result = await postCourse(data);
74 expect(result).toEqual(mockCourse);
75
76 const [url, init] = lastFetchCall();
77 expect(url).toBe("/api/courses");
78 expect(init?.method).toBe("POST");
79 expect(init?.body).toBe(JSON.stringify(data));
80 });
81
82 it("patchCourseById calls /courses/:id with updates", async () => {
83 const mockCourse = { id: "c1", title: "Updated" };
84 mockFetchSuccess(mockCourse);
85
86 const updates = { title: "Updated" };
87 const result = await patchCourseById("c1", updates);
88 expect(result).toEqual(mockCourse);
89
90 const [url, init] = lastFetchCall();
91 expect(url).toBe("/api/courses/c1");
92 expect(init?.method).toBe("PATCH");
93 expect(init?.body).toBe(JSON.stringify(updates));
94 });
95
96 it("deleteCourseById calls /courses/:id", async () => {
97 mockFetch204();
98
99 await deleteCourseById("c1");
100
101 const [url, init] = lastFetchCall();
102 expect(url).toBe("/api/courses/c1");
103 expect(init?.method).toBe("DELETE");
104 });
105
106 it("getCourseSubjects calls /courses/:id/subjects", async () => {
107 const mockPage = { items: [{ id: "sub1" }], total: 1 };
108 mockFetchSuccess(mockPage);
109
110 const result = await getCourseSubjects("c1");
111 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 });
112
113 const [url, init] = lastFetchCall();
114 expect(url).toBe("/api/courses/c1/subjects");
115 expect(init?.method).toBe("GET");
116 });
117
118 it("postCourseSubject calls /courses/:id/subjects", async () => {
119 const mockSub = { id: "sub1", course: "c1" };
120 mockFetchSuccess(mockSub);
121
122 const data = { name: "Sub", description: "Desc" };
123 const result = await postCourseSubject("c1", data);
124 expect(result).toEqual(mockSub);
125
126 const [url, init] = lastFetchCall();
127 expect(url).toBe("/api/courses/c1/subjects");
128 expect(init?.method).toBe("POST");
129 expect(init?.body).toBe(JSON.stringify(data));
130 });
131
132 it("getCourseMembers calls /courses/:id/members with pagination", async () => {
133 mockFetchSuccess({ items: [{ id: "m1" }], total: 1 });
134
135 const result = await getCourseMembers("c1", { limit: 10 });
136 expect(result.items).toEqual([{ id: "m1" }]);
137
138 const [url, init] = lastFetchCall();
139 expect(url).toBe("/api/courses/c1/members?limit=10");
140 expect(init?.method).toBe("GET");
141 });
142
143 it("postCourseMember calls /courses/:id/members with the user id", async () => {
144 mockFetchSuccess({ id: "m1" });
145
146 await postCourseMember("c1", "u1");
147
148 const [url, init] = lastFetchCall();
149 expect(url).toBe("/api/courses/c1/members");
150 expect(init?.method).toBe("POST");
151 expect(init?.body).toBe(JSON.stringify({ user_id: "u1" }));
152 });
153
154 it("deleteCourseMemberByUserId calls /courses/:id/members/:userId", async () => {
155 mockFetch204();
156
157 await deleteCourseMemberByUserId("c1", "u1");
158
159 const [url, init] = lastFetchCall();
160 expect(url).toBe("/api/courses/c1/members/u1");
161 expect(init?.method).toBe("DELETE");
162 });
163
164});