hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | import { getQuestions, getQuestionById, postQuestion, postQuestionApprove, deleteQuestionById, getQuestionImageUrl, getQuestionImageBlob, postQuestionImage, deleteQuestionImage } from "../../shared"; |
| 3 | import { lastFetchCall, mockFetch204, mockFetchBlob, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 4 | |
| 5 | describe("questions API", () => { |
| 6 | afterEach(() => { |
| 7 | vi.restoreAllMocks(); |
| 8 | }); |
| 9 | |
| 10 | it("getQuestions calls /questions with status and pagination", async () => { |
| 11 | const mockPage = { items: [{ id: "q1" }], total: 1 }; |
| 12 | mockFetchSuccess(mockPage); |
| 13 | |
| 14 | const result = await getQuestions("pending", { limit: 10 }); |
| 15 | expect(result).toEqual(mockPage); // page normalization is handled by client in questions.ts? no, client doesn't normalize, wait, questions.ts doesn't normalize! |
| 16 | |
| 17 | const [url, init] = lastFetchCall(); |
| 18 | expect(url).toBe("/api/questions?status=pending&limit=10"); |
| 19 | expect(init?.method).toBe("GET"); |
| 20 | }); |
| 21 | |
| 22 | it("getQuestionById calls /questions/:id", async () => { |
| 23 | const mockQuestion = { id: "q1" }; |
| 24 | mockFetchSuccess(mockQuestion); |
| 25 | |
| 26 | const result = await getQuestionById("q1"); |
| 27 | expect(result).toEqual(mockQuestion); |
| 28 | |
| 29 | const [url, init] = lastFetchCall(); |
| 30 | expect(url).toBe("/api/questions/q1"); |
| 31 | expect(init?.method).toBe("GET"); |
| 32 | }); |
| 33 | |
| 34 | it("postQuestion calls /questions with data", async () => { |
| 35 | const mockQuestion = { id: "q1" }; |
| 36 | mockFetchSuccess(mockQuestion); |
| 37 | |
| 38 | const data = { title: "Title", body: "Body" }; |
| 39 | const result = await postQuestion(data); |
| 40 | expect(result).toEqual(mockQuestion); |
| 41 | |
| 42 | const [url, init] = lastFetchCall(); |
| 43 | // note: if image is not provided, it only calls POST /questions once |
| 44 | expect(url).toBe("/api/questions"); |
| 45 | expect(init?.method).toBe("POST"); |
| 46 | expect(init?.body).toBe(JSON.stringify(data)); |
| 47 | }); |
| 48 | |
| 49 | it("postQuestionApprove calls /questions/:id/approve", async () => { |
| 50 | const mockQuestion = { id: "q1", status: "approved" }; |
| 51 | mockFetchSuccess(mockQuestion); |
| 52 | |
| 53 | const result = await postQuestionApprove("q1"); |
| 54 | expect(result).toEqual(mockQuestion); |
| 55 | |
| 56 | const [url, init] = lastFetchCall(); |
| 57 | expect(url).toBe("/api/questions/q1/approve"); |
| 58 | expect(init?.method).toBe("POST"); |
| 59 | }); |
| 60 | |
| 61 | it("deleteQuestionById calls /questions/:id", async () => { |
| 62 | mockFetch204(); |
| 63 | |
| 64 | await deleteQuestionById("q1"); |
| 65 | |
| 66 | const [url, init] = lastFetchCall(); |
| 67 | expect(url).toBe("/api/questions/q1"); |
| 68 | expect(init?.method).toBe("DELETE"); |
| 69 | }); |
| 70 | |
| 71 | it("getQuestionImageUrl returns correct URL", () => { |
| 72 | const url = getQuestionImageUrl("q1"); |
| 73 | expect(url).toBe("/api/questions/q1/image"); |
| 74 | }); |
| 75 | |
| 76 | it("getQuestionImageBlob fetches the image bytes from the same URL", async () => { |
| 77 | mockFetchBlob(new Blob([new Uint8Array([0x89, 0x50, 0x4e, 0x47])], { type: "image/png" })); |
| 78 | |
| 79 | const result = await getQuestionImageBlob("q1"); |
| 80 | expect(result).toBeInstanceOf(Blob); |
| 81 | |
| 82 | const [url] = lastFetchCall(); |
| 83 | expect(url).toBe(getQuestionImageUrl("q1")); |
| 84 | }); |
| 85 | |
| 86 | it("postQuestionImage uploads multipart to /questions/:id/image", async () => { |
| 87 | mockFetch204(); |
| 88 | |
| 89 | await postQuestionImage("q1", new File(["x"], "sketch.png", { type: "image/png" })); |
| 90 | |
| 91 | const [url, init] = lastFetchCall(); |
| 92 | expect(url).toBe("/api/questions/q1/image"); |
| 93 | expect(init?.method).toBe("POST"); |
| 94 | expect(init?.body).toBeInstanceOf(FormData); |
| 95 | expect((init?.body as FormData).get("file")).toBeInstanceOf(File); |
| 96 | }); |
| 97 | |
| 98 | it("deleteQuestionImage calls /questions/:id/image", async () => { |
| 99 | mockFetch204(); |
| 100 | |
| 101 | await deleteQuestionImage("q1"); |
| 102 | |
| 103 | const [url, init] = lastFetchCall(); |
| 104 | expect(url).toBe("/api/questions/q1/image"); |
| 105 | expect(init?.method).toBe("DELETE"); |
| 106 | }); |
| 107 | }); |