forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

payments.test.ts121 lines, 4.4 KiBHistoryRawDownload
1import { afterEach, describe, expect, it, vi } from "vitest";
2import {
3 deletePaymentPlanById,
4 getMyPaymentBalance,
5 getMyPaymentStatement,
6 getPaymentBalanceByUserId,
7 getPaymentLedgerByUserId,
8 getPaymentPlanById,
9 getPaymentPlans,
10 getPaymentStatementByUserId,
11 getPlanAssignments,
12 patchPaymentPlanById,
13 postPaymentCredit,
14 postPaymentPlan,
15 postPaymentRefund,
16 postPaymentReversal,
17 postPlanAssignment,
18} from "../../payments";
19import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch";
20
21describe("payments API", () => {
22 afterEach(() => vi.restoreAllMocks());
23 const json = () => mockFetchSuccess({});
24 const page = () => mockFetchSuccess({ items: [], total: 0 });
25
26 it("creates a fee plan", async () => {
27 json();
28 const body = { name: "2026-2027", installments: [{ amount_minor: 150000, due_at: 1_760_000_000_000 }] };
29 await postPaymentPlan(body);
30 expect(lastFetchCall()).toMatchObject(["/api/payments/plans", { method: "POST", body: JSON.stringify(body) }]);
31 });
32
33 it("lists plans with pagination", async () => {
34 page();
35 await getPaymentPlans({ limit: 12, offset: 24 });
36 expect(lastFetchCall()[0]).toBe("/api/payments/plans?limit=12&offset=24");
37 });
38
39 it("gets a plan by id", async () => {
40 json();
41 await getPaymentPlanById("p/1");
42 expect(lastFetchCall()[0]).toBe("/api/payments/plans/p%2F1");
43 });
44
45 it("updates a plan", async () => {
46 json();
47 const body = { name: "renamed" };
48 await patchPaymentPlanById("p1", body);
49 expect(lastFetchCall()).toMatchObject(["/api/payments/plans/p1", { method: "PATCH", body: JSON.stringify(body) }]);
50 });
51
52 it("deletes a plan", async () => {
53 mockFetch204();
54 await deletePaymentPlanById("p1");
55 expect(lastFetchCall()).toMatchObject(["/api/payments/plans/p1", { method: "DELETE" }]);
56 });
57
58 it("assigns a plan to students", async () => {
59 mockFetchSuccess([]);
60 const body = { student_ids: ["s1", "s2"] };
61 await postPlanAssignment("p1", body);
62 expect(lastFetchCall()).toMatchObject(["/api/payments/plans/p1/assignments", { method: "POST", body: JSON.stringify(body) }]);
63 });
64
65 it("lists plan assignments with pagination", async () => {
66 page();
67 await getPlanAssignments("p1", { limit: 5 });
68 expect(lastFetchCall()[0]).toBe("/api/payments/plans/p1/assignments?limit=5");
69 });
70
71 it("records a payment with an idempotence key", async () => {
72 json();
73 const body = { charge_id: "c1", amount_minor: 50000, method: "havale", request_key: "receipt-2026-114" };
74 await postPaymentCredit(body);
75 expect(lastFetchCall()).toMatchObject(["/api/payments/credits", { method: "POST", body: JSON.stringify(body) }]);
76 });
77
78 it("records a refund with an idempotence key", async () => {
79 json();
80 const body = { credit_id: "cr1", amount_minor: 50000, request_key: "refund-2026-114" };
81 await postPaymentRefund(body);
82 expect(lastFetchCall()).toMatchObject(["/api/payments/refunds", { method: "POST", body: JSON.stringify(body) }]);
83 });
84
85 it("records a reversal", async () => {
86 json();
87 const body = { line_id: "l1", note: "billed in error" };
88 await postPaymentReversal(body);
89 expect(lastFetchCall()).toMatchObject(["/api/payments/reversals", { method: "POST", body: JSON.stringify(body) }]);
90 });
91
92 it("reads a student ledger with pagination", async () => {
93 page();
94 await getPaymentLedgerByUserId("u/1", { limit: 20, offset: 20 });
95 expect(lastFetchCall()[0]).toBe("/api/payments/ledger/u%2F1?limit=20&offset=20");
96 });
97
98 it("reads my statement", async () => {
99 mockFetchSuccess({ student: {}, entries: { items: [], total: 0 }, balance_minor: 0 });
100 await getMyPaymentStatement({ limit: 10 });
101 expect(lastFetchCall()[0]).toBe("/api/payments/statement/me?limit=10");
102 });
103
104 it("reads a student statement", async () => {
105 mockFetchSuccess({ student: {}, entries: { items: [], total: 0 }, balance_minor: 0 });
106 await getPaymentStatementByUserId("u1");
107 expect(lastFetchCall()[0]).toBe("/api/payments/statement/u1");
108 });
109
110 it("reads my balance", async () => {
111 mockFetchSuccess({ student: {}, balance_minor: 0 });
112 await getMyPaymentBalance();
113 expect(lastFetchCall()[0]).toBe("/api/payments/balance/me");
114 });
115
116 it("reads a student balance", async () => {
117 mockFetchSuccess({ student: {}, balance_minor: 0 });
118 await getPaymentBalanceByUserId("u1");
119 expect(lastFetchCall()[0]).toBe("/api/payments/balance/u1");
120 });
121});