mirror of
https://github.com/acedanger/finance.git
synced 2025-12-06 07:00:13 -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:
56
src/test/accounts.test.ts
Normal file
56
src/test/accounts.test.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
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";
|
||||
|
||||
describe("Accounts API", () => {
|
||||
describe("GET /api/accounts", () => {
|
||||
it("should return all accounts", async () => {
|
||||
const response = await listAccounts();
|
||||
const accounts = await response.json();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(accounts).toHaveLength(2);
|
||||
expect(accounts[0]).toHaveProperty("id", "1");
|
||||
expect(accounts[1]).toHaveProperty("id", "2");
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/accounts/:id", () => {
|
||||
it("should return a specific account", async () => {
|
||||
const response = await getAccount({ params: { id: "1" } });
|
||||
const account = await response.json();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(account).toHaveProperty("id", "1");
|
||||
expect(account).toHaveProperty("name", "Test Checking");
|
||||
});
|
||||
|
||||
it("should return 404 for non-existent account", async () => {
|
||||
const response = await getAccount({ params: { id: "999" } });
|
||||
const error = await response.json();
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
expect(error).toHaveProperty("error", "Account not found");
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/accounts/:id/transactions", () => {
|
||||
it("should return transactions for a specific account", async () => {
|
||||
const response = await listTransactions({ params: { id: "1" } });
|
||||
const transactions = await response.json();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(transactions).toHaveLength(1);
|
||||
expect(transactions[0]).toHaveProperty("accountId", "1");
|
||||
});
|
||||
|
||||
it("should return empty array for account with no transactions", async () => {
|
||||
const response = await listTransactions({ params: { id: "999" } });
|
||||
const transactions = await response.json();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(transactions).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user