forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

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