forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

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

events.test.ts183 lines, 6.6 KiBHistoryRawDownload
1import { afterEach, describe, expect, it, vi } from "vitest";
2import { getEvents } from "../../events";
3import { getEventById } from "../../events";
4import { postEvent } from "../../events";
5import { patchEventById } from "../../events";
6import { deleteEventById } from "../../events";
7import { getEventAttendance } from "../../events";
8import { postEventAttendance } from "../../events";
9import { getEventRoster } from "../../events";
10import { postEventRegister } from "../../events";
11import { deleteEventRegisterByUserId } from "../../events";
12import { deleteEventAttendanceByUserId } from "../../events";
13import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch";
14
15describe("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 // Upper bounds serialize after the earlier keys, in declaration order.
51 mockFetchSuccess({ items: [], total: 0 });
52 await getEvents({ starts_after: 1, ends_after: 2, starts_before: 3, ends_before: 4 });
53 expect(lastFetchCall()[0]).toBe("/api/events?starts_after=1&ends_after=2&starts_before=3&ends_before=4");
54
55 mockFetchSuccess({ items: [], total: 0 });
56 await getEvents({ limit: 10, starts_before: undefined, ends_before: undefined });
57 expect(lastFetchCall()[0]).toBe("/api/events?limit=10");
58 expect(lastFetchCall()[0]).not.toContain("starts_before");
59 expect(lastFetchCall()[0]).not.toContain("ends_before");
60 });
61
62 it("getEventById calls /events/:id", async () => {
63 const mockEvent = { id: "e1", title: "Event" };
64 mockFetchSuccess(mockEvent);
65
66 const result = await getEventById("e1");
67 expect(result).toEqual(mockEvent);
68
69 const [url, init] = lastFetchCall();
70 expect(url).toBe("/api/events/e1");
71 expect(init?.method).toBe("GET");
72 });
73
74 it("postEvent calls /events with data", async () => {
75 const mockEvent = { id: "e1" };
76 mockFetchSuccess(mockEvent);
77
78 const data = { title: "New Event", description: "Desc", audience: { kind: "school" as const } };
79 const result = await postEvent(data);
80 expect(result).toEqual(mockEvent);
81
82 const [url, init] = lastFetchCall();
83 expect(url).toBe("/api/events");
84 expect(init?.method).toBe("POST");
85 expect(init?.body).toBe(JSON.stringify(data));
86 });
87
88 it("patchEventById calls /events/:id with updates", async () => {
89 const mockEvent = { id: "e1", title: "Updated" };
90 mockFetchSuccess(mockEvent);
91
92 const updates = { title: "Updated" };
93 const result = await patchEventById("e1", updates);
94 expect(result).toEqual(mockEvent);
95
96 const [url, init] = lastFetchCall();
97 expect(url).toBe("/api/events/e1");
98 expect(init?.method).toBe("PATCH");
99 expect(init?.body).toBe(JSON.stringify(updates));
100 });
101
102 it("deleteEventById calls /events/:id", async () => {
103 mockFetch204();
104
105 await deleteEventById("e1");
106
107 const [url, init] = lastFetchCall();
108 expect(url).toBe("/api/events/e1");
109 expect(init?.method).toBe("DELETE");
110 });
111
112 it("getEventAttendance calls /events/:id/attendance", async () => {
113 const mockPage = { items: [{ id: "a1", status: "present" }], total: 1 };
114 mockFetchSuccess(mockPage);
115
116 const result = await getEventAttendance("e1");
117 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 });
118
119 const [url, init] = lastFetchCall();
120 expect(url).toBe("/api/events/e1/attendance");
121 expect(init?.method).toBe("GET");
122 });
123
124 it("postEventAttendance calls /events/:id/attendance", async () => {
125 const mockAtt = { id: "a1", status: "present" };
126 mockFetchSuccess(mockAtt);
127
128 const data = { user_id: "u1", status: "present" };
129 const result = await postEventAttendance("e1", data);
130 expect(result).toEqual(mockAtt);
131
132 const [url, init] = lastFetchCall();
133 expect(url).toBe("/api/events/e1/attendance");
134 expect(init?.method).toBe("POST");
135 expect(init?.body).toBe(JSON.stringify(data));
136 });
137
138 it("getEventRoster calls /events/:id/roster", async () => {
139 const mockPage = { items: [{ user: { id: "u1" }, status: "present" }], total: 1 };
140 mockFetchSuccess(mockPage);
141
142 const result = await getEventRoster("e1");
143 expect(result).toEqual({ ...mockPage, limit: null, offset: 0 });
144
145 const [url, init] = lastFetchCall();
146 expect(url).toBe("/api/events/e1/roster");
147 expect(init?.method).toBe("GET");
148 });
149
150 it("postEventRegister calls /events/:id/register", async () => {
151 const mockReg = { event: "e1", user: { id: "u1" } };
152 mockFetchSuccess(mockReg);
153
154 const data = { user_id: "u1" };
155 const result = await postEventRegister("e1", data);
156 expect(result).toEqual(mockReg);
157
158 const [url, init] = lastFetchCall();
159 expect(url).toBe("/api/events/e1/register");
160 expect(init?.method).toBe("POST");
161 expect(init?.body).toBe(JSON.stringify(data));
162 });
163
164 it("deleteEventRegisterByUserId calls /events/:id/register/:userId", async () => {
165 mockFetch204();
166
167 await deleteEventRegisterByUserId("e1", "u1");
168
169 const [url, init] = lastFetchCall();
170 expect(url).toBe("/api/events/e1/register/u1");
171 expect(init?.method).toBe("DELETE");
172 });
173
174 it("deleteEventAttendanceByUserId calls /events/:id/attendance/:userId", async () => {
175 mockFetch204();
176
177 await deleteEventAttendanceByUserId("e1", "u1");
178
179 const [url, init] = lastFetchCall();
180 expect(url).toBe("/api/events/e1/attendance/u1");
181 expect(init?.method).toBe("DELETE");
182 });
183});