mirror of
https://github.com/acedanger/finance.git
synced 2025-12-06 07:00:13 -08:00
refactor: improve account validation in transaction updates and enhance test setup with mock API context
This commit is contained in:
@@ -2,11 +2,12 @@ import { describe, it, expect } from "vitest";
|
||||
import { GET as listAccounts } from "../pages/api/accounts/index";
|
||||
import { GET as getAccount } from "../pages/api/accounts/[id]/index";
|
||||
import { GET as listTransactions } from "../pages/api/accounts/[id]/transactions/index";
|
||||
import { createMockAPIContext } from "./setup";
|
||||
|
||||
describe("Accounts API", () => {
|
||||
describe("GET /api/accounts", () => {
|
||||
it("should return all accounts", async () => {
|
||||
const response = await listAccounts();
|
||||
const response = await listAccounts(createMockAPIContext() as any);
|
||||
const accounts = await response.json();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
@@ -18,7 +19,9 @@ describe("Accounts API", () => {
|
||||
|
||||
describe("GET /api/accounts/:id", () => {
|
||||
it("should return a specific account", async () => {
|
||||
const response = await getAccount({ params: { id: "1" } });
|
||||
const response = await getAccount(
|
||||
createMockAPIContext({ params: { id: "1" } }) as any
|
||||
);
|
||||
const account = await response.json();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
@@ -27,7 +30,9 @@ describe("Accounts API", () => {
|
||||
});
|
||||
|
||||
it("should return 404 for non-existent account", async () => {
|
||||
const response = await getAccount({ params: { id: "999" } });
|
||||
const response = await getAccount(
|
||||
createMockAPIContext({ params: { id: "999" } }) as any
|
||||
);
|
||||
const error = await response.json();
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
@@ -36,8 +41,10 @@ describe("Accounts API", () => {
|
||||
});
|
||||
|
||||
describe("GET /api/accounts/:id/transactions", () => {
|
||||
it("should return transactions for a specific account", async () => {
|
||||
const response = await listTransactions({ params: { id: "1" } });
|
||||
it("should return transactions for an account", async () => {
|
||||
const response = await listTransactions(
|
||||
createMockAPIContext({ params: { id: "1" } }) as any
|
||||
);
|
||||
const transactions = await response.json();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
@@ -46,7 +53,9 @@ describe("Accounts API", () => {
|
||||
});
|
||||
|
||||
it("should return empty array for account with no transactions", async () => {
|
||||
const response = await listTransactions({ params: { id: "999" } });
|
||||
const response = await listTransactions(
|
||||
createMockAPIContext({ params: { id: "999" } }) as any
|
||||
);
|
||||
const transactions = await response.json();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
|
||||
Reference in New Issue
Block a user