mirror of
https://github.com/acedanger/finance.git
synced 2025-12-05 22:50:12 -08:00
326 lines
11 KiB
TypeScript
326 lines
11 KiB
TypeScript
// TODO: Testing Improvements
|
|
// - Add integration tests for API endpoints
|
|
// - Add end-to-end tests with Playwright/Cypress
|
|
// - Add performance testing
|
|
// - Add accessibility testing with axe-core
|
|
// - Add visual regression testing
|
|
// - Add load testing for API endpoints
|
|
// - Implement test data factories
|
|
|
|
import { describe, expect, it } from 'vitest';
|
|
import { accounts, transactions } from '../data/store';
|
|
import {
|
|
DELETE as deleteTransaction,
|
|
PUT as updateTransaction,
|
|
} from '../pages/api/transactions/[id]/index';
|
|
import { POST as createTransaction } from '../pages/api/transactions/index';
|
|
import type { Transaction } from '../types';
|
|
import { createMockAPIContext } from './setup';
|
|
|
|
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 ctx = createMockAPIContext() as APIContext;
|
|
ctx.request = new Request('http://localhost:4321/api/transactions', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(newTransaction),
|
|
});
|
|
|
|
const response = await createTransaction(ctx);
|
|
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 ctx = createMockAPIContext() as APIContext;
|
|
ctx.request = new Request('http://localhost:4321/api/transactions', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(invalidTransaction),
|
|
});
|
|
|
|
const response = await createTransaction(ctx);
|
|
const error = await response.json();
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(error).toHaveProperty('error', 'Missing required fields');
|
|
});
|
|
|
|
it('should reject transaction with invalid account', async () => {
|
|
const invalidTransaction = {
|
|
accountId: '999',
|
|
date: '2025-04-24',
|
|
description: 'Invalid Account Test',
|
|
amount: 100,
|
|
};
|
|
|
|
const ctx = createMockAPIContext() as APIContext;
|
|
ctx.request = new Request('http://localhost:4321/api/transactions', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(invalidTransaction),
|
|
});
|
|
|
|
const response = await createTransaction(ctx);
|
|
const error = await response.json();
|
|
|
|
expect(response.status).toBe(404);
|
|
expect(error).toHaveProperty('error', 'Account not found');
|
|
});
|
|
|
|
it('should reject invalid request body', async () => {
|
|
const ctx = createMockAPIContext() as APIContext;
|
|
ctx.request = new Request('http://localhost:4321/api/transactions', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: 'invalid json',
|
|
});
|
|
|
|
const response = await createTransaction(ctx);
|
|
const error = await response.json();
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(error).toHaveProperty('error', 'Invalid request body');
|
|
});
|
|
});
|
|
|
|
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 ctx = createMockAPIContext({ params: { id: '1' } }) as APIContext;
|
|
ctx.request = new Request('http://localhost:4321/api/transactions/1', {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(updates),
|
|
});
|
|
|
|
const response = await updateTransaction(ctx);
|
|
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 with invalid request body', async () => {
|
|
const ctx = createMockAPIContext({ params: { id: '1' } }) as APIContext;
|
|
ctx.request = new Request('http://localhost:4321/api/transactions/1', {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: 'invalid json',
|
|
});
|
|
|
|
const response = await updateTransaction(ctx);
|
|
const error = await response.json();
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(error).toHaveProperty('error', 'Invalid request body');
|
|
});
|
|
|
|
it('should reject update for non-existent transaction', async () => {
|
|
const ctx = createMockAPIContext({ params: { id: '999' } }) as APIContext;
|
|
ctx.request = new Request('http://localhost:4321/api/transactions/999', {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ description: 'Test' }),
|
|
});
|
|
|
|
const response = await updateTransaction(ctx);
|
|
const error = await response.json();
|
|
|
|
expect(response.status).toBe(404);
|
|
expect(error).toHaveProperty('error', 'Transaction not found');
|
|
});
|
|
|
|
it('should reject update for non-existent account', async () => {
|
|
// First update the transaction to point to a non-existent account
|
|
transactions[0].accountId = '999';
|
|
|
|
const ctx = createMockAPIContext({ params: { id: '1' } }) as APIContext;
|
|
ctx.request = new Request('http://localhost:4321/api/transactions/1', {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ amount: -100 }),
|
|
});
|
|
|
|
const response = await updateTransaction(ctx);
|
|
const error = await response.json();
|
|
|
|
expect(response.status).toBe(404);
|
|
expect(error).toHaveProperty('error', 'Account not found');
|
|
|
|
// Reset account ID for other tests
|
|
transactions[0].accountId = '1';
|
|
});
|
|
|
|
it('should handle account balance updates correctly when switching accounts', async () => {
|
|
// Create initial state
|
|
const oldAccount = accounts[0];
|
|
const newAccount = accounts[1];
|
|
const initialOldBalance = oldAccount.balance;
|
|
const initialNewBalance = newAccount.balance;
|
|
const oldTransaction = transactions.find((t) => t.id === '1');
|
|
if (!oldTransaction) throw new Error('Test transaction not found');
|
|
|
|
const ctx = createMockAPIContext({ params: { id: '1' } }) as APIContext;
|
|
ctx.request = new Request('http://localhost:4321/api/transactions/1', {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
accountId: newAccount.id,
|
|
amount: -100,
|
|
}),
|
|
});
|
|
|
|
const response = await updateTransaction(ctx);
|
|
const result = await response.json();
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(result.accountId).toBe(newAccount.id);
|
|
|
|
// Old account should have the old amount removed
|
|
expect(oldAccount.balance).toBe(initialOldBalance + Math.abs(oldTransaction.amount));
|
|
|
|
// New account should have the new amount added
|
|
expect(newAccount.balance).toBe(initialNewBalance - 100);
|
|
});
|
|
|
|
it('should reject update without transaction ID', async () => {
|
|
const ctx = createMockAPIContext() as APIContext;
|
|
ctx.request = new Request('http://localhost:4321/api/transactions/undefined', {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ description: 'Test' }),
|
|
});
|
|
|
|
const response = await updateTransaction(ctx);
|
|
const error = await response.json();
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(error).toHaveProperty('error', 'Transaction ID is required');
|
|
});
|
|
|
|
it('should reject update when old account is missing', async () => {
|
|
// Store current accounts and clear the array
|
|
const savedAccounts = [...accounts];
|
|
accounts.length = 0;
|
|
|
|
const ctx = createMockAPIContext({ params: { id: '1' } }) as APIContext;
|
|
ctx.request = new Request('http://localhost:4321/api/transactions/1', {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ amount: -100 }),
|
|
});
|
|
|
|
const response = await updateTransaction(ctx);
|
|
const error = await response.json();
|
|
|
|
expect(response.status).toBe(404);
|
|
expect(error).toHaveProperty('error', 'Account not found');
|
|
|
|
// Restore accounts
|
|
accounts.push(...savedAccounts);
|
|
});
|
|
|
|
it("should reject update when new account doesn't exist", async () => {
|
|
const ctx = createMockAPIContext({ params: { id: '1' } }) as APIContext;
|
|
ctx.request = new Request('http://localhost:4321/api/transactions/1', {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
accountId: '999', // Non-existent account
|
|
amount: -100,
|
|
}),
|
|
});
|
|
|
|
const response = await updateTransaction(ctx);
|
|
const error = await response.json();
|
|
|
|
expect(response.status).toBe(404);
|
|
expect(error).toHaveProperty('error', 'Account 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(
|
|
createMockAPIContext({ params: { id: '1' } }) as any,
|
|
);
|
|
|
|
expect(response.status).toBe(204);
|
|
expect(transactions).toHaveLength(initialCount - 1);
|
|
expect(accounts[0].balance).toBe(initialBalance - transactionAmount);
|
|
});
|
|
|
|
it('should reject delete without transaction ID', async () => {
|
|
const response = await deleteTransaction(createMockAPIContext() as APIContext);
|
|
|
|
const error = await response.json();
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(error).toHaveProperty('error', 'Transaction ID is required');
|
|
});
|
|
|
|
it('should return 404 for non-existent transaction', async () => {
|
|
const response = await deleteTransaction(
|
|
createMockAPIContext({ params: { id: '999' } }) as any,
|
|
);
|
|
|
|
const error = await response.json();
|
|
|
|
expect(response.status).toBe(404);
|
|
expect(error).toHaveProperty('error', 'Transaction not found');
|
|
});
|
|
|
|
it('should handle deletion with non-existent account', async () => {
|
|
// Create a transaction then remove its account
|
|
const testTransaction: Transaction = {
|
|
id: 'test-delete',
|
|
accountId: 'test-account',
|
|
date: '2025-04-24',
|
|
description: 'Test Delete',
|
|
amount: 100,
|
|
};
|
|
transactions.push(testTransaction);
|
|
|
|
const response = await deleteTransaction(
|
|
createMockAPIContext({ params: { id: 'test-delete' } }) as any,
|
|
);
|
|
|
|
const error = await response.json();
|
|
|
|
expect(response.status).toBe(404);
|
|
expect(error).toHaveProperty('error', 'Account not found');
|
|
});
|
|
});
|
|
});
|