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 { getSubjectById } from "../../subjects"; |
| 3 | import { patchSubjectById } from "../../subjects"; |
| 4 | import { deleteSubjectById } from "../../subjects"; |
| 5 | import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 6 | |
| 7 | describe("subjects API", () => { |
| 8 | afterEach(() => { |
| 9 | vi.restoreAllMocks(); |
| 10 | }); |
| 11 | |
| 12 | it("getSubjectById calls /subjects/:id", async () => { |
| 13 | const mockSubject = { id: "sub1", name: "Subject" }; |
| 14 | mockFetchSuccess(mockSubject); |
| 15 | |
| 16 | const result = await getSubjectById("sub1"); |
| 17 | expect(result).toEqual(mockSubject); |
| 18 | |
| 19 | const [url, init] = lastFetchCall(); |
| 20 | expect(url).toBe("/api/subjects/sub1"); |
| 21 | expect(init?.method).toBe("GET"); |
| 22 | }); |
| 23 | |
| 24 | it("patchSubjectById calls /subjects/:id with updates", async () => { |
| 25 | const mockSubject = { id: "sub1", name: "Updated" }; |
| 26 | mockFetchSuccess(mockSubject); |
| 27 | |
| 28 | const updates = { name: "Updated" }; |
| 29 | const result = await patchSubjectById("sub1", updates); |
| 30 | expect(result).toEqual(mockSubject); |
| 31 | |
| 32 | const [url, init] = lastFetchCall(); |
| 33 | expect(url).toBe("/api/subjects/sub1"); |
| 34 | expect(init?.method).toBe("PATCH"); |
| 35 | expect(init?.body).toBe(JSON.stringify(updates)); |
| 36 | }); |
| 37 | |
| 38 | it("deleteSubjectById calls /subjects/:id", async () => { |
| 39 | mockFetch204(); |
| 40 | |
| 41 | await deleteSubjectById("sub1"); |
| 42 | |
| 43 | const [url, init] = lastFetchCall(); |
| 44 | expect(url).toBe("/api/subjects/sub1"); |
| 45 | expect(init?.method).toBe("DELETE"); |
| 46 | }); |
| 47 | }); |