hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | import { postSlots } from "../../appointments"; |
| 3 | import { getSlots } from "../../appointments"; |
| 4 | import { deleteSlotById } from "../../appointments"; |
| 5 | import { deleteSlotSeries } from "../../appointments"; |
| 6 | import { postAppointment } from "../../appointments"; |
| 7 | import { getAppointments } from "../../appointments"; |
| 8 | import { patchApproveAppointment } from "../../appointments"; |
| 9 | import { patchRejectAppointment } from "../../appointments"; |
| 10 | import { patchCancelAppointment } from "../../appointments"; |
| 11 | import { patchRescheduleAppointment } from "../../appointments"; |
| 12 | import { patchAcceptReschedule } from "../../appointments"; |
| 13 | import { patchDeclineReschedule } from "../../appointments"; |
| 14 | import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 15 | |
| 16 | describe("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("deleteSlotById calls /appointments/slots/:id", async () => { |
| 48 | mockFetch204(); |
| 49 | |
| 50 | await deleteSlotById("s1"); |
| 51 | |
| 52 | const [url, init] = lastFetchCall(); |
| 53 | expect(url).toBe("/api/appointments/slots/s1"); |
| 54 | expect(init?.method).toBe("DELETE"); |
| 55 | }); |
| 56 | |
| 57 | it("deleteSlotSeries calls /appointments/slots/series/:series", async () => { |
| 58 | mockFetch204(); |
| 59 | |
| 60 | await deleteSlotSeries("ser1"); |
| 61 | |
| 62 | const [url, init] = lastFetchCall(); |
| 63 | expect(url).toBe("/api/appointments/slots/series/ser1"); |
| 64 | expect(init?.method).toBe("DELETE"); |
| 65 | }); |
| 66 | |
| 67 | it("postAppointment calls /appointments with data", async () => { |
| 68 | const mockAppt = { id: "a1" }; |
| 69 | mockFetchSuccess(mockAppt); |
| 70 | |
| 71 | const data = { slot: "s1", reason: "need help" }; |
| 72 | const result = await postAppointment(data); |
| 73 | expect(result).toEqual(mockAppt); |
| 74 | |
| 75 | const [url, init] = lastFetchCall(); |
| 76 | expect(url).toBe("/api/appointments"); |
| 77 | expect(init?.method).toBe("POST"); |
| 78 | expect(init?.body).toBe(JSON.stringify(data)); |
| 79 | }); |
| 80 | |
| 81 | it("getAppointments calls /appointments with pagination", async () => { |
| 82 | const mockPage = { items: [{ id: "a1" }], total: 1 }; |
| 83 | mockFetchSuccess(mockPage); |
| 84 | |
| 85 | const result = await getAppointments({ limit: 10 }); |
| 86 | expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); |
| 87 | |
| 88 | const [url, init] = lastFetchCall(); |
| 89 | expect(url).toBe("/api/appointments?limit=10"); |
| 90 | expect(init?.method).toBe("GET"); |
| 91 | }); |
| 92 | |
| 93 | it("patchApproveAppointment calls /appointments/:id/approve", async () => { |
| 94 | const mockAppt = { id: "a1", status: "approved" }; |
| 95 | mockFetchSuccess(mockAppt); |
| 96 | |
| 97 | const result = await patchApproveAppointment("a1"); |
| 98 | expect(result).toEqual(mockAppt); |
| 99 | |
| 100 | const [url, init] = lastFetchCall(); |
| 101 | expect(url).toBe("/api/appointments/a1/approve"); |
| 102 | expect(init?.method).toBe("PATCH"); |
| 103 | }); |
| 104 | |
| 105 | it("patchRejectAppointment calls /appointments/:id/reject", async () => { |
| 106 | const mockAppt = { id: "a1", status: "rejected" }; |
| 107 | mockFetchSuccess(mockAppt); |
| 108 | |
| 109 | const result = await patchRejectAppointment("a1"); |
| 110 | expect(result).toEqual(mockAppt); |
| 111 | |
| 112 | const [url, init] = lastFetchCall(); |
| 113 | expect(url).toBe("/api/appointments/a1/reject"); |
| 114 | expect(init?.method).toBe("PATCH"); |
| 115 | expect(init?.body).toBeUndefined(); |
| 116 | }); |
| 117 | |
| 118 | it("patchRejectAppointment sends an optional reason body", async () => { |
| 119 | mockFetchSuccess({ id: "a1", status: "rejected" }); |
| 120 | |
| 121 | await patchRejectAppointment("a1", { reason: "slot no longer available" }); |
| 122 | |
| 123 | const [url, init] = lastFetchCall(); |
| 124 | expect(url).toBe("/api/appointments/a1/reject"); |
| 125 | expect(init?.method).toBe("PATCH"); |
| 126 | expect(init?.body).toBe(JSON.stringify({ reason: "slot no longer available" })); |
| 127 | }); |
| 128 | |
| 129 | it("patchCancelAppointment calls /appointments/:id/cancel", async () => { |
| 130 | const mockAppt = { id: "a1", status: "cancelled" }; |
| 131 | mockFetchSuccess(mockAppt); |
| 132 | |
| 133 | const result = await patchCancelAppointment("a1"); |
| 134 | expect(result).toEqual(mockAppt); |
| 135 | |
| 136 | const [url, init] = lastFetchCall(); |
| 137 | expect(url).toBe("/api/appointments/a1/cancel"); |
| 138 | expect(init?.method).toBe("PATCH"); |
| 139 | expect(init?.body).toBeUndefined(); |
| 140 | }); |
| 141 | |
| 142 | it("patchCancelAppointment sends an optional reason body", async () => { |
| 143 | mockFetchSuccess({ id: "a1", status: "cancelled" }); |
| 144 | |
| 145 | await patchCancelAppointment("a1", { reason: "double booked" }); |
| 146 | |
| 147 | const [url, init] = lastFetchCall(); |
| 148 | expect(url).toBe("/api/appointments/a1/cancel"); |
| 149 | expect(init?.method).toBe("PATCH"); |
| 150 | expect(init?.body).toBe(JSON.stringify({ reason: "double booked" })); |
| 151 | }); |
| 152 | |
| 153 | it("patchRescheduleAppointment calls /appointments/:id/reschedule with data", async () => { |
| 154 | const mockAppt = { id: "a1" }; |
| 155 | mockFetchSuccess(mockAppt); |
| 156 | |
| 157 | const data = { starts_at: 3000, ends_at: 4000 }; |
| 158 | const result = await patchRescheduleAppointment("a1", data); |
| 159 | expect(result).toEqual(mockAppt); |
| 160 | |
| 161 | const [url, init] = lastFetchCall(); |
| 162 | expect(url).toBe("/api/appointments/a1/reschedule"); |
| 163 | expect(init?.method).toBe("PATCH"); |
| 164 | expect(init?.body).toBe(JSON.stringify(data)); |
| 165 | }); |
| 166 | |
| 167 | it("patchAcceptReschedule calls /appointments/:id/reschedule/accept", async () => { |
| 168 | const mockAppt = { id: "a1" }; |
| 169 | mockFetchSuccess(mockAppt); |
| 170 | |
| 171 | const body = { proposed_starts_at: 1000, proposed_ends_at: 2000 }; |
| 172 | const result = await patchAcceptReschedule("a1", body); |
| 173 | expect(result).toEqual(mockAppt); |
| 174 | |
| 175 | const [url, init] = lastFetchCall(); |
| 176 | expect(url).toBe("/api/appointments/a1/reschedule/accept"); |
| 177 | expect(init?.method).toBe("PATCH"); |
| 178 | expect(init?.body).toBe(JSON.stringify(body)); |
| 179 | }); |
| 180 | |
| 181 | it("patchDeclineReschedule calls /appointments/:id/reschedule/decline", async () => { |
| 182 | const mockAppt = { id: "a1" }; |
| 183 | mockFetchSuccess(mockAppt); |
| 184 | |
| 185 | const result = await patchDeclineReschedule("a1"); |
| 186 | expect(result).toEqual(mockAppt); |
| 187 | |
| 188 | const [url, init] = lastFetchCall(); |
| 189 | expect(url).toBe("/api/appointments/a1/reschedule/decline"); |
| 190 | expect(init?.method).toBe("PATCH"); |
| 191 | }); |
| 192 | }); |