forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

instances.test.ts171 lines, 5.3 KiBHistoryRawDownload
1import { afterEach, describe, expect, it, vi } from "vitest";
2import {
3 deleteInstanceEnrollmentByUserId,
4 deleteInstanceTeacherByUserId,
5 getInstanceById,
6 getInstanceEnrollments,
7 getInstanceExams,
8 getInstanceHomework,
9 getInstanceSessions,
10 getMyInstances,
11 patchInstanceById,
12 postInstanceEnrollment,
13 postInstanceExam,
14 postInstanceHomework,
15 postInstanceSession,
16 postInstanceTeacher,
17} from "../../instances";
18import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch";
19
20describe("instances API", () => {
21 afterEach(() => {
22 vi.restoreAllMocks();
23 });
24
25 it("getMyInstances GETs /instances/me with pagination", async () => {
26 mockFetchSuccess({ items: [{ id: "i1" }], total: 1 });
27
28 const result = await getMyInstances({ limit: 20, offset: 40 });
29 expect(result.items).toEqual([{ id: "i1" }]);
30
31 const [url, init] = lastFetchCall();
32 expect(url).toBe("/api/instances/me?limit=20&offset=40");
33 expect(init?.method).toBe("GET");
34 });
35
36 it("getInstanceById GETs /instances/:id", async () => {
37 mockFetchSuccess({ id: "i1" });
38
39 await getInstanceById("i1");
40
41 const [url, init] = lastFetchCall();
42 expect(url).toBe("/api/instances/i1");
43 expect(init?.method).toBe("GET");
44 });
45
46 it("patchInstanceById PATCHes /instances/:id with the policy body", async () => {
47 mockFetchSuccess({ id: "i1" });
48 const body = { ders_saati: 4, counts_toward_karne: false };
49
50 await patchInstanceById("i1", body);
51
52 const [url, init] = lastFetchCall();
53 expect(url).toBe("/api/instances/i1");
54 expect(init?.method).toBe("PATCH");
55 expect(init?.body).toBe(JSON.stringify(body));
56 });
57
58 it("getInstanceEnrollments GETs /instances/:id/enrollments with pagination", async () => {
59 mockFetchSuccess({ items: [], total: 0 });
60
61 await getInstanceEnrollments("i1", { limit: 5 });
62
63 const [url] = lastFetchCall();
64 expect(url).toBe("/api/instances/i1/enrollments?limit=5");
65 });
66
67 it("postInstanceEnrollment POSTs /instances/:id/enrollments with the user id", async () => {
68 mockFetchSuccess({ id: "e1" });
69
70 await postInstanceEnrollment("i1", "u1");
71
72 const [url, init] = lastFetchCall();
73 expect(url).toBe("/api/instances/i1/enrollments");
74 expect(init?.method).toBe("POST");
75 expect(init?.body).toBe(JSON.stringify({ user_id: "u1" }));
76 });
77
78 it("deleteInstanceEnrollmentByUserId DELETEs /instances/:id/enrollments/:user", async () => {
79 mockFetch204();
80
81 await deleteInstanceEnrollmentByUserId("i1", "u1");
82
83 const [url, init] = lastFetchCall();
84 expect(url).toBe("/api/instances/i1/enrollments/u1");
85 expect(init?.method).toBe("DELETE");
86 });
87
88 it("getInstanceExams GETs /instances/:id/exams", async () => {
89 mockFetchSuccess({ items: [], total: 0 });
90
91 await getInstanceExams("i1");
92
93 const [url] = lastFetchCall();
94 expect(url).toBe("/api/instances/i1/exams");
95 });
96
97 it("postInstanceExam POSTs /instances/:id/exams with the term", async () => {
98 mockFetchSuccess({ id: "x1" });
99 const body = { title: "Quiz 1", kind: "quiz", term: "t1" };
100
101 await postInstanceExam("i1", body);
102
103 const [url, init] = lastFetchCall();
104 expect(url).toBe("/api/instances/i1/exams");
105 expect(init?.method).toBe("POST");
106 expect(init?.body).toBe(JSON.stringify(body));
107 });
108
109 it("getInstanceHomework GETs /instances/:id/homework", async () => {
110 mockFetchSuccess({ items: [], total: 0 });
111
112 await getInstanceHomework("i1");
113
114 const [url] = lastFetchCall();
115 expect(url).toBe("/api/instances/i1/homework");
116 });
117
118 it("postInstanceHomework POSTs /instances/:id/homework with the body", async () => {
119 mockFetchSuccess({ id: "h1" });
120 const body = { title: "Read ch. 3", subject_id: "s1", due_at: 1900000000000 };
121
122 await postInstanceHomework("i1", body);
123
124 const [url, init] = lastFetchCall();
125 expect(url).toBe("/api/instances/i1/homework");
126 expect(init?.method).toBe("POST");
127 expect(init?.body).toBe(JSON.stringify(body));
128 });
129
130 it("getInstanceSessions GETs /instances/:id/sessions", async () => {
131 mockFetchSuccess({ items: [], total: 0 });
132
133 await getInstanceSessions("i1");
134
135 const [url] = lastFetchCall();
136 expect(url).toBe("/api/instances/i1/sessions");
137 });
138
139 it("postInstanceSession POSTs /instances/:id/sessions with the body", async () => {
140 mockFetchSuccess({ id: "se1" });
141 const body = { starts_at: 1900000000000, topic: "Limits" };
142
143 await postInstanceSession("i1", body);
144
145 const [url, init] = lastFetchCall();
146 expect(url).toBe("/api/instances/i1/sessions");
147 expect(init?.method).toBe("POST");
148 expect(init?.body).toBe(JSON.stringify(body));
149 });
150
151 it("postInstanceTeacher POSTs /instances/:id/teachers with the user id", async () => {
152 mockFetchSuccess({ id: "i1" });
153
154 await postInstanceTeacher("i1", "t1");
155
156 const [url, init] = lastFetchCall();
157 expect(url).toBe("/api/instances/i1/teachers");
158 expect(init?.method).toBe("POST");
159 expect(init?.body).toBe(JSON.stringify({ user_id: "t1" }));
160 });
161
162 it("deleteInstanceTeacherByUserId DELETEs /instances/:id/teachers/:user", async () => {
163 mockFetch204();
164
165 await deleteInstanceTeacherByUserId("i1", "t1");
166
167 const [url, init] = lastFetchCall();
168 expect(url).toBe("/api/instances/i1/teachers/t1");
169 expect(init?.method).toBe("DELETE");
170 });
171});