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 { getSessionById } from "../../sessions"; |
| 3 | import { patchSessionById } from "../../sessions"; |
| 4 | import { deleteSessionById } from "../../sessions"; |
| 5 | import { getSessionAttendance } from "../../sessions"; |
| 6 | import { postSessionAttendance } from "../../sessions"; |
| 7 | import { deleteSessionAttendanceByUserId } from "../../sessions"; |
| 8 | import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 9 | |
| 10 | describe("sessions API", () => { |
| 11 | afterEach(() => { |
| 12 | vi.restoreAllMocks(); |
| 13 | }); |
| 14 | |
| 15 | it("getSessionById calls /sessions/:id", async () => { |
| 16 | const mockSession = { id: "s1", topic: "Topic" }; |
| 17 | mockFetchSuccess(mockSession); |
| 18 | |
| 19 | const result = await getSessionById("s1"); |
| 20 | expect(result).toEqual(mockSession); |
| 21 | |
| 22 | const [url, init] = lastFetchCall(); |
| 23 | expect(url).toBe("/api/sessions/s1"); |
| 24 | expect(init?.method).toBe("GET"); |
| 25 | }); |
| 26 | |
| 27 | it("patchSessionById calls /sessions/:id with updates", async () => { |
| 28 | const mockSession = { id: "s1", topic: "Updated" }; |
| 29 | mockFetchSuccess(mockSession); |
| 30 | |
| 31 | const updates = { topic: "Updated" }; |
| 32 | const result = await patchSessionById("s1", updates); |
| 33 | expect(result).toEqual(mockSession); |
| 34 | |
| 35 | const [url, init] = lastFetchCall(); |
| 36 | expect(url).toBe("/api/sessions/s1"); |
| 37 | expect(init?.method).toBe("PATCH"); |
| 38 | expect(init?.body).toBe(JSON.stringify(updates)); |
| 39 | }); |
| 40 | |
| 41 | it("deleteSessionById calls /sessions/:id", async () => { |
| 42 | mockFetch204(); |
| 43 | |
| 44 | await deleteSessionById("s1"); |
| 45 | |
| 46 | const [url, init] = lastFetchCall(); |
| 47 | expect(url).toBe("/api/sessions/s1"); |
| 48 | expect(init?.method).toBe("DELETE"); |
| 49 | }); |
| 50 | |
| 51 | it("getSessionAttendance calls /sessions/:id/attendance", async () => { |
| 52 | const mockPage = { items: [{ id: "a1", status: "present" }], total: 1 }; |
| 53 | mockFetchSuccess(mockPage); |
| 54 | |
| 55 | const result = await getSessionAttendance("s1"); |
| 56 | expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); |
| 57 | |
| 58 | const [url, init] = lastFetchCall(); |
| 59 | expect(url).toBe("/api/sessions/s1/attendance"); |
| 60 | expect(init?.method).toBe("GET"); |
| 61 | }); |
| 62 | |
| 63 | it("postSessionAttendance calls /sessions/:id/attendance", async () => { |
| 64 | const mockAtt = { id: "a1", status: "present" }; |
| 65 | mockFetchSuccess(mockAtt); |
| 66 | |
| 67 | const data = { user_id: "u1", status: "present" }; |
| 68 | const result = await postSessionAttendance("s1", data); |
| 69 | expect(result).toEqual(mockAtt); |
| 70 | |
| 71 | const [url, init] = lastFetchCall(); |
| 72 | expect(url).toBe("/api/sessions/s1/attendance"); |
| 73 | expect(init?.method).toBe("POST"); |
| 74 | expect(init?.body).toBe(JSON.stringify(data)); |
| 75 | }); |
| 76 | |
| 77 | it("deleteSessionAttendanceByUserId calls /sessions/:id/attendance/:user", async () => { |
| 78 | mockFetch204(); |
| 79 | |
| 80 | await deleteSessionAttendanceByUserId("s1", "u1"); |
| 81 | |
| 82 | const [url, init] = lastFetchCall(); |
| 83 | expect(url).toBe("/api/sessions/s1/attendance/u1"); |
| 84 | expect(init?.method).toBe("DELETE"); |
| 85 | }); |
| 86 | }); |