mirror of
https://github.com/acedanger/finance.git
synced 2025-12-05 22:50:12 -08:00
feat(tests): add unit tests for accounts and transactions APIs
- Updated package.json to include Vitest for testing and added necessary devDependencies. - Created accounts.test.ts to test the accounts API endpoints for listing and retrieving accounts. - Implemented setup.ts to reset test data before each test run. - Developed transactions.test.ts to cover creating, updating, and deleting transactions through the API. - Added vitest.config.ts for configuring Vitest with appropriate settings and coverage options.
This commit is contained in:
128
src/test/transactions.test.ts
Normal file
128
src/test/transactions.test.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { POST as createTransaction } from "../pages/api/transactions/index";
|
||||
import {
|
||||
PUT as updateTransaction,
|
||||
DELETE as deleteTransaction,
|
||||
} from "../pages/api/transactions/[id]/index";
|
||||
import { accounts, transactions } from "../data/store";
|
||||
|
||||
describe("Transactions API", () => {
|
||||
describe("POST /api/transactions", () => {
|
||||
it("should create a new transaction", async () => {
|
||||
const initialBalance = accounts[0].balance;
|
||||
const newTransaction = {
|
||||
accountId: "1",
|
||||
date: "2025-04-24",
|
||||
description: "Test New Transaction",
|
||||
amount: -25.0,
|
||||
};
|
||||
|
||||
const response = await createTransaction({
|
||||
request: new Request("http://localhost:4321/api/transactions", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(newTransaction),
|
||||
}),
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
expect(response.status).toBe(201);
|
||||
expect(result).toHaveProperty("id");
|
||||
expect(result.description).toBe(newTransaction.description);
|
||||
expect(accounts[0].balance).toBe(initialBalance + newTransaction.amount);
|
||||
});
|
||||
|
||||
it("should reject transaction with missing fields", async () => {
|
||||
const invalidTransaction = {
|
||||
accountId: "1",
|
||||
// Missing required fields
|
||||
};
|
||||
|
||||
const response = await createTransaction({
|
||||
request: new Request("http://localhost:4321/api/transactions", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(invalidTransaction),
|
||||
}),
|
||||
});
|
||||
|
||||
const error = await response.json();
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
expect(error).toHaveProperty("error", "Missing required fields");
|
||||
});
|
||||
});
|
||||
|
||||
describe("PUT /api/transactions/:id", () => {
|
||||
it("should update an existing transaction", async () => {
|
||||
const initialBalance = accounts[0].balance;
|
||||
const originalAmount = transactions[0].amount;
|
||||
const updates = {
|
||||
description: "Updated Description",
|
||||
amount: -75.0,
|
||||
};
|
||||
|
||||
const response = await updateTransaction({
|
||||
params: { id: "1" },
|
||||
request: new Request("http://localhost:4321/api/transactions/1", {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(updates),
|
||||
}),
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(result.description).toBe(updates.description);
|
||||
expect(result.amount).toBe(updates.amount);
|
||||
expect(accounts[0].balance).toBe(
|
||||
initialBalance - originalAmount + updates.amount
|
||||
);
|
||||
});
|
||||
|
||||
it("should reject update for non-existent transaction", async () => {
|
||||
const response = await updateTransaction({
|
||||
params: { id: "999" },
|
||||
request: new Request("http://localhost:4321/api/transactions/999", {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ description: "Test" }),
|
||||
}),
|
||||
});
|
||||
|
||||
const error = await response.json();
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
expect(error).toHaveProperty("error", "Transaction not found");
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/transactions/:id", () => {
|
||||
it("should delete a transaction", async () => {
|
||||
const initialBalance = accounts[0].balance;
|
||||
const transactionAmount = transactions[0].amount;
|
||||
const initialCount = transactions.length;
|
||||
|
||||
const response = await deleteTransaction({
|
||||
params: { id: "1" },
|
||||
});
|
||||
|
||||
expect(response.status).toBe(204);
|
||||
expect(transactions).toHaveLength(initialCount - 1);
|
||||
expect(accounts[0].balance).toBe(initialBalance - transactionAmount);
|
||||
});
|
||||
|
||||
it("should return 404 for non-existent transaction", async () => {
|
||||
const response = await deleteTransaction({
|
||||
params: { id: "999" },
|
||||
});
|
||||
|
||||
const error = await response.json();
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
expect(error).toHaveProperty("error", "Transaction not found");
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user