Files
SkyMoney/api/tests/transactions.test.ts

67 lines
1.8 KiB
TypeScript

import request from "supertest";
import { describe, it, expect, beforeAll, beforeEach, afterAll } from "vitest";
import appFactory from "./appFactory";
import { prisma, resetUser, ensureUser, U, cid, closePrisma } from "./helpers";
import type { FastifyInstance } from "fastify";
let app: FastifyInstance;
beforeAll(async () => {
app = await appFactory();
});
afterAll(async () => {
await app.close();
await closePrisma();
});
describe("GET /transactions", () => {
beforeEach(async () => {
await resetUser(U);
await ensureUser(U);
const c = cid("c");
await prisma.variableCategory.create({
data: { id: c, userId: U, name: "Groceries", percent: 100, priority: 1, isSavings: false, balanceCents: 5000n },
});
// seed some transactions of different kinds/dates
await prisma.transaction.createMany({
data: [
{
id: `t_${Date.now()}_1`,
userId: U,
occurredAt: new Date("2025-01-03T12:00:00.000Z"),
kind: "variable_spend",
categoryId: c,
amountCents: 1000n,
},
{
id: `t_${Date.now()}_2`,
userId: U,
occurredAt: new Date("2025-01-10T12:00:00.000Z"),
kind: "fixed_payment",
planId: null,
amountCents: 2000n,
},
],
});
});
afterAll(async () => {
await closePrisma();
});
it("paginates + filters by kind/date", async () => {
const res = await request(app.server)
.get("/transactions?from=2025-01-02&to=2025-01-06&kind=variable_spend&page=1&limit=10")
.set("x-user-id", U);
expect(res.statusCode).toBe(200);
const body = res.body;
expect(Array.isArray(body.items)).toBe(true);
expect(body.items.length).toBe(1);
expect(body.items[0].kind).toBe("variable_spend");
});
});