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 { |
| 3 | deleteMealBookingById, |
| 4 | deleteMealDishById, |
| 5 | deleteMealMenuById, |
| 6 | getDietaryProfileByUserId, |
| 7 | getMealAttendanceByUserId, |
| 8 | getMealBalanceByUserId, |
| 9 | getMealLedgerByUserId, |
| 10 | getMealMenuBookings, |
| 11 | getMealMenuAttendance, |
| 12 | getMealMenuById, |
| 13 | getMealMenus, |
| 14 | getMyDietaryProfile, |
| 15 | getMyMealBalance, |
| 16 | getMyMealBookings, |
| 17 | patchDietaryProfileByUserId, |
| 18 | patchMealDishById, |
| 19 | patchMealMenuById, |
| 20 | postMealAttendance, |
| 21 | postMealBooking, |
| 22 | postMealCredit, |
| 23 | postMealDish, |
| 24 | postMealMenu, |
| 25 | } from "../../meals"; |
| 26 | import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 27 | |
| 28 | describe("meals API", () => { |
| 29 | afterEach(() => vi.restoreAllMocks()); |
| 30 | const json = () => mockFetchSuccess({}); |
| 31 | const page = () => mockFetchSuccess({ items: [], total: 0 }); |
| 32 | |
| 33 | it("lists menus with dates and pagination", async () => { |
| 34 | page(); await getMealMenus({ from: "2026-07-28", to: "2026-08-01", limit: 12, offset: 12 }); |
| 35 | expect(lastFetchCall()[0]).toBe("/api/meals/menus?limit=12&offset=12&from=2026-07-28&to=2026-08-01"); |
| 36 | }); |
| 37 | it("gets a menu", async () => { json(); await getMealMenuById("m/1"); expect(lastFetchCall()[0]).toBe("/api/meals/menus/m%2F1"); }); |
| 38 | it("publishes a menu", async () => { |
| 39 | json(); const body = { date: "2026-07-28", slot: "lunch", capacity: 10 }; await postMealMenu(body); |
| 40 | expect(lastFetchCall()).toMatchObject(["/api/meals/menus", { method: "POST", body: JSON.stringify(body) }]); |
| 41 | }); |
| 42 | it("patches a menu", async () => { |
| 43 | json(); await patchMealMenuById("m1", { capacity: null }); |
| 44 | expect(lastFetchCall()).toMatchObject(["/api/meals/menus/m1", { method: "PATCH", body: JSON.stringify({ capacity: null }) }]); |
| 45 | }); |
| 46 | it("deletes a menu", async () => { mockFetch204(); await deleteMealMenuById("m1"); expect(lastFetchCall()).toMatchObject(["/api/meals/menus/m1", { method: "DELETE" }]); }); |
| 47 | it("adds a dish", async () => { |
| 48 | json(); const body = { name: "Soup", description: null, price_minor: 4500, tags: ["vegan"] }; await postMealDish("m1", body); |
| 49 | expect(lastFetchCall()).toMatchObject(["/api/meals/menus/m1/dishes", { method: "POST", body: JSON.stringify(body) }]); |
| 50 | }); |
| 51 | it("patches a dish", async () => { json(); await patchMealDishById("d1", { price_minor: 5000 }); expect(lastFetchCall()).toMatchObject(["/api/meals/dishes/d1", { method: "PATCH", body: JSON.stringify({ price_minor: 5000 }) }]); }); |
| 52 | it("deletes a dish", async () => { mockFetch204(); await deleteMealDishById("d1"); expect(lastFetchCall()).toMatchObject(["/api/meals/dishes/d1", { method: "DELETE" }]); }); |
| 53 | it("gets own profile", async () => { json(); await getMyDietaryProfile(); expect(lastFetchCall()[0]).toBe("/api/meals/profiles/me"); }); |
| 54 | it("gets a student profile", async () => { json(); await getDietaryProfileByUserId("u1"); expect(lastFetchCall()[0]).toBe("/api/meals/profiles/u1"); }); |
| 55 | it("patches a student profile", async () => { |
| 56 | json(); const body = { tags: ["nuts"], note: null }; await patchDietaryProfileByUserId("u1", body); |
| 57 | expect(lastFetchCall()).toMatchObject(["/api/meals/profiles/u1", { method: "PATCH", body: JSON.stringify(body) }]); |
| 58 | }); |
| 59 | it("books a child seat", async () => { |
| 60 | json(); await postMealBooking("m1", "u1"); |
| 61 | expect(lastFetchCall()).toMatchObject(["/api/meals/menus/m1/bookings", { method: "POST", body: JSON.stringify({ student_id: "u1" }) }]); |
| 62 | }); |
| 63 | it("lists own bookings", async () => { page(); await getMyMealBookings({ limit: 20 }); expect(lastFetchCall()[0]).toBe("/api/meals/bookings/me?limit=20"); }); |
| 64 | it("cancels a booking", async () => { json(); await deleteMealBookingById("b1"); expect(lastFetchCall()).toMatchObject(["/api/meals/bookings/b1", { method: "DELETE" }]); }); |
| 65 | it("lists booking audit", async () => { page(); await getMealMenuBookings("m1", { limit: 20 }); expect(lastFetchCall()[0]).toBe("/api/meals/menus/m1/bookings?limit=20"); }); |
| 66 | it("lists menu attendance", async () => { page(); await getMealMenuAttendance("m1", { limit: 20 }); expect(lastFetchCall()[0]).toBe("/api/meals/menus/m1/attendance?limit=20"); }); |
| 67 | it("marks service", async () => { |
| 68 | json(); await postMealAttendance("m1", "u1", "served"); |
| 69 | expect(lastFetchCall()).toMatchObject(["/api/meals/menus/m1/attendance", { method: "POST", body: JSON.stringify({ student_id: "u1", status: "served" }) }]); |
| 70 | }); |
| 71 | it("gets attendance history", async () => { page(); await getMealAttendanceByUserId("u1", { from: "2026-07-01", limit: 20 }); expect(lastFetchCall()[0]).toBe("/api/meals/attendance/u1?limit=20&from=2026-07-01"); }); |
| 72 | it("gets own balance", async () => { json(); await getMyMealBalance(); expect(lastFetchCall()[0]).toBe("/api/meals/balance/me"); }); |
| 73 | it("gets student balance", async () => { json(); await getMealBalanceByUserId("u1"); expect(lastFetchCall()[0]).toBe("/api/meals/balance/u1"); }); |
| 74 | it("gets student ledger", async () => { page(); await getMealLedgerByUserId("u1", { limit: 20 }); expect(lastFetchCall()[0]).toBe("/api/meals/ledger/u1?limit=20"); }); |
| 75 | it("records append-only credit", async () => { |
| 76 | json(); const body = { student_id: "u1", amount_minor: 10000, method: "cash", note: "receipt" }; await postMealCredit(body); |
| 77 | expect(lastFetchCall()).toMatchObject(["/api/meals/credits", { method: "POST", body: JSON.stringify(body) }]); |
| 78 | }); |
| 79 | }); |