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 { getEvents } from "../../events"; |
| 3 | import { getEventById } from "../../events"; |
| 4 | import { postEvent } from "../../events"; |
| 5 | import { patchEventById } from "../../events"; |
| 6 | import { deleteEventById } from "../../events"; |
| 7 | import { getEventAttendance } from "../../events"; |
| 8 | import { postEventAttendance } from "../../events"; |
| 9 | import { getEventRoster } from "../../events"; |
| 10 | import { postEventRegister } from "../../events"; |
| 11 | import { deleteEventRegisterByUserId } from "../../events"; |
| 12 | import { deleteEventAttendanceByUserId } from "../../events"; |
| 13 | import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 14 | |
| 15 | describe("events API", () => { |
| 16 | afterEach(() => { |
| 17 | vi.restoreAllMocks(); |
| 18 | }); |
| 19 | |
| 20 | it("getEvents calls /events with pagination", async () => { |
| 21 | const mockPage = { items: [{ id: "e1" }], total: 1 }; |
| 22 | mockFetchSuccess(mockPage); |
| 23 | |
| 24 | const result = await getEvents({ limit: 10 }); |
| 25 | expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); |
| 26 | |
| 27 | const [url, init] = lastFetchCall(); |
| 28 | expect(url).toBe("/api/events?limit=10"); |
| 29 | expect(init?.method).toBe("GET"); |
| 30 | }); |
| 31 | |
| 32 | it("getEvents sends the schedule window and drops undefined params", async () => { |
| 33 | mockFetchSuccess({ items: [{ id: "e1" }], total: 1 }); |
| 34 | |
| 35 | await getEvents({ ends_after: 1_700_000_000_000, limit: 50 }); |
| 36 | expect(lastFetchCall()[0]).toBe("/api/events?limit=50&ends_after=1700000000000"); |
| 37 | |
| 38 | mockFetchSuccess({ items: [], total: 0 }); |
| 39 | await getEvents({ starts_after: 1_700_000_000_000 }); |
| 40 | expect(lastFetchCall()[0]).toBe("/api/events?starts_after=1700000000000"); |
| 41 | |
| 42 | // A blank value (`?ends_after=`) is a hard 400 server-side, so an |
| 43 | // undefined filter must leave the key out of the query string entirely. |
| 44 | mockFetchSuccess({ items: [], total: 0 }); |
| 45 | await getEvents({ limit: 10, ends_after: undefined, starts_after: undefined }); |
| 46 | expect(lastFetchCall()[0]).toBe("/api/events?limit=10"); |
| 47 | expect(lastFetchCall()[0]).not.toContain("ends_after"); |
| 48 | expect(lastFetchCall()[0]).not.toContain("starts_after"); |
| 49 | }); |
| 50 | |
| 51 | it("getEventById calls /events/:id", async () => { |
| 52 | const mockEvent = { id: "e1", title: "Event" }; |
| 53 | mockFetchSuccess(mockEvent); |
| 54 | |
| 55 | const result = await getEventById("e1"); |
| 56 | expect(result).toEqual(mockEvent); |
| 57 | |
| 58 | const [url, init] = lastFetchCall(); |
| 59 | expect(url).toBe("/api/events/e1"); |
| 60 | expect(init?.method).toBe("GET"); |
| 61 | }); |
| 62 | |
| 63 | it("postEvent calls /events with data", async () => { |
| 64 | const mockEvent = { id: "e1" }; |
| 65 | mockFetchSuccess(mockEvent); |
| 66 | |
| 67 | const data = { title: "New Event", description: "Desc", audience: { kind: "school" as const } }; |
| 68 | const result = await postEvent(data); |
| 69 | expect(result).toEqual(mockEvent); |
| 70 | |
| 71 | const [url, init] = lastFetchCall(); |
| 72 | expect(url).toBe("/api/events"); |
| 73 | expect(init?.method).toBe("POST"); |
| 74 | expect(init?.body).toBe(JSON.stringify(data)); |
| 75 | }); |
| 76 | |
| 77 | it("patchEventById calls /events/:id with updates", async () => { |
| 78 | const mockEvent = { id: "e1", title: "Updated" }; |
| 79 | mockFetchSuccess(mockEvent); |
| 80 | |
| 81 | const updates = { title: "Updated" }; |
| 82 | const result = await patchEventById("e1", updates); |
| 83 | expect(result).toEqual(mockEvent); |
| 84 | |
| 85 | const [url, init] = lastFetchCall(); |
| 86 | expect(url).toBe("/api/events/e1"); |
| 87 | expect(init?.method).toBe("PATCH"); |
| 88 | expect(init?.body).toBe(JSON.stringify(updates)); |
| 89 | }); |
| 90 | |
| 91 | it("deleteEventById calls /events/:id", async () => { |
| 92 | mockFetch204(); |
| 93 | |
| 94 | await deleteEventById("e1"); |
| 95 | |
| 96 | const [url, init] = lastFetchCall(); |
| 97 | expect(url).toBe("/api/events/e1"); |
| 98 | expect(init?.method).toBe("DELETE"); |
| 99 | }); |
| 100 | |
| 101 | it("getEventAttendance calls /events/:id/attendance", async () => { |
| 102 | const mockPage = { items: [{ id: "a1", status: "present" }], total: 1 }; |
| 103 | mockFetchSuccess(mockPage); |
| 104 | |
| 105 | const result = await getEventAttendance("e1"); |
| 106 | expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); |
| 107 | |
| 108 | const [url, init] = lastFetchCall(); |
| 109 | expect(url).toBe("/api/events/e1/attendance"); |
| 110 | expect(init?.method).toBe("GET"); |
| 111 | }); |
| 112 | |
| 113 | it("postEventAttendance calls /events/:id/attendance", async () => { |
| 114 | const mockAtt = { id: "a1", status: "present" }; |
| 115 | mockFetchSuccess(mockAtt); |
| 116 | |
| 117 | const data = { user_id: "u1", status: "present" }; |
| 118 | const result = await postEventAttendance("e1", data); |
| 119 | expect(result).toEqual(mockAtt); |
| 120 | |
| 121 | const [url, init] = lastFetchCall(); |
| 122 | expect(url).toBe("/api/events/e1/attendance"); |
| 123 | expect(init?.method).toBe("POST"); |
| 124 | expect(init?.body).toBe(JSON.stringify(data)); |
| 125 | }); |
| 126 | |
| 127 | it("getEventRoster calls /events/:id/roster", async () => { |
| 128 | const mockPage = { items: [{ user: { id: "u1" }, status: "present" }], total: 1 }; |
| 129 | mockFetchSuccess(mockPage); |
| 130 | |
| 131 | const result = await getEventRoster("e1"); |
| 132 | expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); |
| 133 | |
| 134 | const [url, init] = lastFetchCall(); |
| 135 | expect(url).toBe("/api/events/e1/roster"); |
| 136 | expect(init?.method).toBe("GET"); |
| 137 | }); |
| 138 | |
| 139 | it("postEventRegister calls /events/:id/register", async () => { |
| 140 | const mockReg = { event: "e1", user: { id: "u1" } }; |
| 141 | mockFetchSuccess(mockReg); |
| 142 | |
| 143 | const data = { user_id: "u1" }; |
| 144 | const result = await postEventRegister("e1", data); |
| 145 | expect(result).toEqual(mockReg); |
| 146 | |
| 147 | const [url, init] = lastFetchCall(); |
| 148 | expect(url).toBe("/api/events/e1/register"); |
| 149 | expect(init?.method).toBe("POST"); |
| 150 | expect(init?.body).toBe(JSON.stringify(data)); |
| 151 | }); |
| 152 | |
| 153 | it("deleteEventRegisterByUserId calls /events/:id/register/:userId", async () => { |
| 154 | mockFetch204(); |
| 155 | |
| 156 | await deleteEventRegisterByUserId("e1", "u1"); |
| 157 | |
| 158 | const [url, init] = lastFetchCall(); |
| 159 | expect(url).toBe("/api/events/e1/register/u1"); |
| 160 | expect(init?.method).toBe("DELETE"); |
| 161 | }); |
| 162 | |
| 163 | it("deleteEventAttendanceByUserId calls /events/:id/attendance/:userId", async () => { |
| 164 | mockFetch204(); |
| 165 | |
| 166 | await deleteEventAttendanceByUserId("e1", "u1"); |
| 167 | |
| 168 | const [url, init] = lastFetchCall(); |
| 169 | expect(url).toBe("/api/events/e1/attendance/u1"); |
| 170 | expect(init?.method).toBe("DELETE"); |
| 171 | }); |
| 172 | }); |