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 { getSolutions, postSolution, patchSolutionById, deleteSolutionById, getSolutionImageUrl, getSolutionImageBlob, postSolutionImage, deleteSolutionImage } from "../../shared"; |
| 3 | import { lastFetchCall, mockFetch204, mockFetchBlob, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 4 | |
| 5 | describe("solutions API", () => { |
| 6 | afterEach(() => { |
| 7 | vi.restoreAllMocks(); |
| 8 | }); |
| 9 | |
| 10 | it("getSolutions calls /questions/:id/solutions with pagination", async () => { |
| 11 | const mockPage = { items: [{ id: "s1" }], total: 1 }; |
| 12 | mockFetchSuccess(mockPage); |
| 13 | |
| 14 | const result = await getSolutions("q1", { limit: 10 }); |
| 15 | expect(result).toEqual(mockPage); |
| 16 | |
| 17 | const [url, init] = lastFetchCall(); |
| 18 | expect(url).toBe("/api/questions/q1/solutions?limit=10"); |
| 19 | expect(init?.method).toBe("GET"); |
| 20 | }); |
| 21 | |
| 22 | it("postSolution calls /questions/:id/solutions with data", async () => { |
| 23 | const mockSolution = { id: "s1" }; |
| 24 | mockFetchSuccess(mockSolution); |
| 25 | |
| 26 | const data = { body: "Body" }; |
| 27 | const result = await postSolution("q1", data); |
| 28 | expect(result).toEqual(mockSolution); |
| 29 | |
| 30 | const [url, init] = lastFetchCall(); |
| 31 | expect(url).toBe("/api/questions/q1/solutions"); |
| 32 | expect(init?.method).toBe("POST"); |
| 33 | expect(init?.body).toBe(JSON.stringify(data)); |
| 34 | }); |
| 35 | |
| 36 | it("patchSolutionById calls /questions/:id/solutions/:sid with updates", async () => { |
| 37 | const mockSolution = { id: "s1", body: "Updated" }; |
| 38 | mockFetchSuccess(mockSolution); |
| 39 | |
| 40 | const updates = { body: "Updated" }; |
| 41 | const result = await patchSolutionById("q1", "s1", updates); |
| 42 | expect(result).toEqual(mockSolution); |
| 43 | |
| 44 | const [url, init] = lastFetchCall(); |
| 45 | expect(url).toBe("/api/questions/q1/solutions/s1"); |
| 46 | expect(init?.method).toBe("PATCH"); |
| 47 | expect(init?.body).toBe(JSON.stringify(updates)); |
| 48 | }); |
| 49 | |
| 50 | it("deleteSolutionById calls /questions/:id/solutions/:sid", async () => { |
| 51 | mockFetch204(); |
| 52 | |
| 53 | await deleteSolutionById("q1", "s1"); |
| 54 | |
| 55 | const [url, init] = lastFetchCall(); |
| 56 | expect(url).toBe("/api/questions/q1/solutions/s1"); |
| 57 | expect(init?.method).toBe("DELETE"); |
| 58 | }); |
| 59 | |
| 60 | it("getSolutionImageUrl returns correct URL", () => { |
| 61 | const url = getSolutionImageUrl("q1", "s1"); |
| 62 | expect(url).toBe("/api/questions/q1/solutions/s1/image"); |
| 63 | }); |
| 64 | |
| 65 | it("getSolutionImageBlob fetches the image bytes from the same URL", async () => { |
| 66 | mockFetchBlob(new Blob([new Uint8Array([0x89, 0x50, 0x4e, 0x47])], { type: "image/png" })); |
| 67 | |
| 68 | const result = await getSolutionImageBlob("q1", "s1"); |
| 69 | expect(result).toBeInstanceOf(Blob); |
| 70 | |
| 71 | const [url] = lastFetchCall(); |
| 72 | expect(url).toBe(getSolutionImageUrl("q1", "s1")); |
| 73 | }); |
| 74 | |
| 75 | it("postSolutionImage uploads multipart to /questions/:id/solutions/:sid/image", async () => { |
| 76 | mockFetch204(); |
| 77 | |
| 78 | await postSolutionImage("q1", "s1", new File(["x"], "work.png", { type: "image/png" })); |
| 79 | |
| 80 | const [url, init] = lastFetchCall(); |
| 81 | expect(url).toBe("/api/questions/q1/solutions/s1/image"); |
| 82 | expect(init?.method).toBe("POST"); |
| 83 | expect(init?.body).toBeInstanceOf(FormData); |
| 84 | expect((init?.body as FormData).get("file")).toBeInstanceOf(File); |
| 85 | }); |
| 86 | |
| 87 | it("deleteSolutionImage calls /questions/:id/solutions/:sid/image", async () => { |
| 88 | mockFetch204(); |
| 89 | |
| 90 | await deleteSolutionImage("q1", "s1"); |
| 91 | |
| 92 | const [url, init] = lastFetchCall(); |
| 93 | expect(url).toBe("/api/questions/q1/solutions/s1/image"); |
| 94 | expect(init?.method).toBe("DELETE"); |
| 95 | }); |
| 96 | }); |