forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

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

appointments.test.ts210 lines, 7.7 KiBHistoryRawDownload
1import { afterEach, describe, expect, it, vi } from "vitest";
2import { postSlots } from "../../appointments";
3import { getSlots } from "../../appointments";
4import { deleteSlotById } from "../../appointments";
5import { deleteSlotSeries } from "../../appointments";
6import { postAppointment } from "../../appointments";
7import { getAppointments } from "../../appointments";
8import { patchApproveAppointment } from "../../appointments";
9import { patchRejectAppointment } from "../../appointments";
10import { patchCancelAppointment } from "../../appointments";
11import { patchRescheduleAppointment } from "../../appointments";
12import { patchAcceptReschedule } from "../../appointments";
13import { patchDeclineReschedule } from "../../appointments";
14import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch";
15
16describe("appointments API", () => {
17 afterEach(() => {
18 vi.restoreAllMocks();
19 });
20
21 it("postSlots calls /appointments/slots with data", async () => {
22 const mockSlots = [{ id: "s1" }];
23 mockFetchSuccess(mockSlots);
24
25 const data = { starts_at: 1000, ends_at: 2000, note: "hi", repeat_weekly: true, until: 5000 };
26 const result = await postSlots(data);
27 expect(result).toEqual(mockSlots);
28
29 const [url, init] = lastFetchCall();
30 expect(url).toBe("/api/appointments/slots");
31 expect(init?.method).toBe("POST");
32 expect(init?.body).toBe(JSON.stringify(data));
33 });
34
35 it("getSlots calls /appointments/slots with pagination", async () => {
36 const mockPage = { items: [{ id: "s1" }], total: 1 };
37 mockFetchSuccess(mockPage);
38
39 const result = await getSlots({ limit: 10 });
40 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 });
41
42 const [url, init] = lastFetchCall();
43 expect(url).toBe("/api/appointments/slots?limit=10");
44 expect(init?.method).toBe("GET");
45 });
46
47 it("getSlots sends teacher and start window when set", async () => {
48 mockFetchSuccess({ items: [], total: 0 });
49 await getSlots({ teacher: "me", starts_after: 1700000000000, limit: 10 });
50 expect(lastFetchCall()[0]).toBe("/api/appointments/slots?limit=10&starts_after=1700000000000&teacher=me");
51 });
52
53 it("deleteSlotById calls /appointments/slots/:id", async () => {
54 mockFetch204();
55
56 await deleteSlotById("s1");
57
58 const [url, init] = lastFetchCall();
59 expect(url).toBe("/api/appointments/slots/s1");
60 expect(init?.method).toBe("DELETE");
61 });
62
63 it("deleteSlotSeries calls /appointments/slots/series/:series", async () => {
64 mockFetch204();
65
66 await deleteSlotSeries("ser1");
67
68 const [url, init] = lastFetchCall();
69 expect(url).toBe("/api/appointments/slots/series/ser1");
70 expect(init?.method).toBe("DELETE");
71 });
72
73 it("postAppointment calls /appointments with data", async () => {
74 const mockAppt = { id: "a1" };
75 mockFetchSuccess(mockAppt);
76
77 const data = { slot: "s1", reason: "need help" };
78 const result = await postAppointment(data);
79 expect(result).toEqual(mockAppt);
80
81 const [url, init] = lastFetchCall();
82 expect(url).toBe("/api/appointments");
83 expect(init?.method).toBe("POST");
84 expect(init?.body).toBe(JSON.stringify(data));
85 });
86
87 it("getAppointments calls /appointments with pagination", async () => {
88 const mockPage = { items: [{ id: "a1" }], total: 1 };
89 mockFetchSuccess(mockPage);
90
91 const result = await getAppointments({ limit: 10 });
92 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 });
93
94 const [url, init] = lastFetchCall();
95 expect(url).toBe("/api/appointments?limit=10");
96 expect(init?.method).toBe("GET");
97 });
98
99 it("getAppointments sends status, teacher and start window when set", async () => {
100 mockFetchSuccess({ items: [], total: 0 });
101 await getAppointments({ status: "pending", teacher: "me", starts_after: 1700000000000, starts_before: 1800000000000, limit: 10, offset: 20 });
102 expect(lastFetchCall()[0]).toBe("/api/appointments?limit=10&offset=20&starts_after=1700000000000&starts_before=1800000000000&status=pending&teacher=me");
103 });
104
105 it("getAppointments omits unset filters — no bare keys", async () => {
106 mockFetchSuccess({ items: [], total: 0 });
107 await getAppointments();
108 expect(lastFetchCall()[0]).toBe("/api/appointments");
109 });
110
111 it("patchApproveAppointment calls /appointments/:id/approve", async () => {
112 const mockAppt = { id: "a1", status: "approved" };
113 mockFetchSuccess(mockAppt);
114
115 const result = await patchApproveAppointment("a1");
116 expect(result).toEqual(mockAppt);
117
118 const [url, init] = lastFetchCall();
119 expect(url).toBe("/api/appointments/a1/approve");
120 expect(init?.method).toBe("PATCH");
121 });
122
123 it("patchRejectAppointment calls /appointments/:id/reject", async () => {
124 const mockAppt = { id: "a1", status: "rejected" };
125 mockFetchSuccess(mockAppt);
126
127 const result = await patchRejectAppointment("a1");
128 expect(result).toEqual(mockAppt);
129
130 const [url, init] = lastFetchCall();
131 expect(url).toBe("/api/appointments/a1/reject");
132 expect(init?.method).toBe("PATCH");
133 expect(init?.body).toBeUndefined();
134 });
135
136 it("patchRejectAppointment sends an optional reason body", async () => {
137 mockFetchSuccess({ id: "a1", status: "rejected" });
138
139 await patchRejectAppointment("a1", { reason: "slot no longer available" });
140
141 const [url, init] = lastFetchCall();
142 expect(url).toBe("/api/appointments/a1/reject");
143 expect(init?.method).toBe("PATCH");
144 expect(init?.body).toBe(JSON.stringify({ reason: "slot no longer available" }));
145 });
146
147 it("patchCancelAppointment calls /appointments/:id/cancel", async () => {
148 const mockAppt = { id: "a1", status: "cancelled" };
149 mockFetchSuccess(mockAppt);
150
151 const result = await patchCancelAppointment("a1");
152 expect(result).toEqual(mockAppt);
153
154 const [url, init] = lastFetchCall();
155 expect(url).toBe("/api/appointments/a1/cancel");
156 expect(init?.method).toBe("PATCH");
157 expect(init?.body).toBeUndefined();
158 });
159
160 it("patchCancelAppointment sends an optional reason body", async () => {
161 mockFetchSuccess({ id: "a1", status: "cancelled" });
162
163 await patchCancelAppointment("a1", { reason: "double booked" });
164
165 const [url, init] = lastFetchCall();
166 expect(url).toBe("/api/appointments/a1/cancel");
167 expect(init?.method).toBe("PATCH");
168 expect(init?.body).toBe(JSON.stringify({ reason: "double booked" }));
169 });
170
171 it("patchRescheduleAppointment calls /appointments/:id/reschedule with data", async () => {
172 const mockAppt = { id: "a1" };
173 mockFetchSuccess(mockAppt);
174
175 const data = { starts_at: 3000, ends_at: 4000 };
176 const result = await patchRescheduleAppointment("a1", data);
177 expect(result).toEqual(mockAppt);
178
179 const [url, init] = lastFetchCall();
180 expect(url).toBe("/api/appointments/a1/reschedule");
181 expect(init?.method).toBe("PATCH");
182 expect(init?.body).toBe(JSON.stringify(data));
183 });
184
185 it("patchAcceptReschedule calls /appointments/:id/reschedule/accept", async () => {
186 const mockAppt = { id: "a1" };
187 mockFetchSuccess(mockAppt);
188
189 const body = { proposed_starts_at: 1000, proposed_ends_at: 2000 };
190 const result = await patchAcceptReschedule("a1", body);
191 expect(result).toEqual(mockAppt);
192
193 const [url, init] = lastFetchCall();
194 expect(url).toBe("/api/appointments/a1/reschedule/accept");
195 expect(init?.method).toBe("PATCH");
196 expect(init?.body).toBe(JSON.stringify(body));
197 });
198
199 it("patchDeclineReschedule calls /appointments/:id/reschedule/decline", async () => {
200 const mockAppt = { id: "a1" };
201 mockFetchSuccess(mockAppt);
202
203 const result = await patchDeclineReschedule("a1");
204 expect(result).toEqual(mockAppt);
205
206 const [url, init] = lastFetchCall();
207 expect(url).toBe("/api/appointments/a1/reschedule/decline");
208 expect(init?.method).toBe("PATCH");
209 });
210});