mirror of
https://github.com/acedanger/finance.git
synced 2025-12-06 07:00:13 -08:00
refactor: Reorganize imports and improve code consistency across components and API routes
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
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 type { APIContext } from 'astro';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { listAccounts } from '../pages/api/accounts';
|
||||
import { getAccount } from '../pages/api/accounts/[id]';
|
||||
import { listTransactions } from '../pages/api/accounts/[id]/transactions';
|
||||
import { createMockAPIContext } from './setup';
|
||||
|
||||
describe('Accounts API', () => {
|
||||
describe('GET /api/accounts', () => {
|
||||
it('should return all accounts', async () => {
|
||||
const response = await listAccounts(createMockAPIContext() as any);
|
||||
const response = await listAccounts(createMockAPIContext());
|
||||
const accounts = await response.json();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
@@ -19,7 +20,9 @@ describe('Accounts API', () => {
|
||||
|
||||
describe('GET /api/accounts/:id', () => {
|
||||
it('should return a specific account', async () => {
|
||||
const response = await getAccount(createMockAPIContext({ params: { id: '1' } }) as any);
|
||||
const response = await getAccount(
|
||||
createMockAPIContext({ params: { id: '1' } }) as APIContext,
|
||||
);
|
||||
const account = await response.json();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
@@ -28,7 +31,9 @@ describe('Accounts API', () => {
|
||||
});
|
||||
|
||||
it('should return 404 for non-existent account', async () => {
|
||||
const response = await getAccount(createMockAPIContext({ params: { id: '999' } }) as any);
|
||||
const response = await getAccount(
|
||||
createMockAPIContext({ params: { id: '999' } }) as APIContext,
|
||||
);
|
||||
const error = await response.json();
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
@@ -38,7 +43,9 @@ describe('Accounts API', () => {
|
||||
|
||||
describe('GET /api/accounts/:id/transactions', () => {
|
||||
it('should return transactions for an account', async () => {
|
||||
const response = await listTransactions(createMockAPIContext({ params: { id: '1' } }) as any);
|
||||
const response = await listTransactions(
|
||||
createMockAPIContext({ params: { id: '1' } }) as APIContext,
|
||||
);
|
||||
const transactions = await response.json();
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
@@ -48,7 +55,7 @@ describe('Accounts API', () => {
|
||||
|
||||
it('should return empty array for account with no transactions', async () => {
|
||||
const response = await listTransactions(
|
||||
createMockAPIContext({ params: { id: '999' } }) as any
|
||||
createMockAPIContext({ params: { id: '999' } }) as APIContext,
|
||||
);
|
||||
const transactions = await response.json();
|
||||
|
||||
|
||||
@@ -1,29 +1,20 @@
|
||||
import type { APIContext } from 'astro';
|
||||
import { beforeEach } from 'vitest';
|
||||
import { accounts, transactions } from '../data/store';
|
||||
import type { APIContext } from 'astro';
|
||||
|
||||
// Create a mock APIContext factory
|
||||
export function createMockAPIContext<T extends Record<string, string> = Record<string, string>>({
|
||||
params = {} as T,
|
||||
} = {}): Partial<APIContext> {
|
||||
export function createMockAPIContext(options: Partial<APIContext> = {}): APIContext {
|
||||
return {
|
||||
params,
|
||||
props: {},
|
||||
request: new Request('http://localhost:4321'),
|
||||
site: new URL('http://localhost:4321'),
|
||||
generator: 'test',
|
||||
url: new URL('http://localhost:4321'),
|
||||
clientAddress: '127.0.0.1',
|
||||
cookies: new Headers() as any, // Cast Headers to cookies as we don't need cookie functionality in tests
|
||||
cookies: new Headers(),
|
||||
redirect: () => new Response(),
|
||||
locals: {},
|
||||
preferredLocale: undefined,
|
||||
preferredLocaleList: [],
|
||||
currentLocale: undefined,
|
||||
routePattern: '/api/[...path]',
|
||||
originPathname: '/api',
|
||||
getActionResult: () => undefined,
|
||||
isPrerendered: false,
|
||||
site: new URL('http://localhost:4321'),
|
||||
generator: 'test',
|
||||
params: {},
|
||||
...options,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -43,7 +34,7 @@ beforeEach(() => {
|
||||
name: 'Test Savings',
|
||||
last4: '5678',
|
||||
balance: 5000.0,
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// Reset transactions to initial state
|
||||
@@ -62,6 +53,6 @@ beforeEach(() => {
|
||||
date: '2025-04-24',
|
||||
description: 'Test Transaction 2',
|
||||
amount: 100.0,
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
@@ -7,13 +7,12 @@
|
||||
// - Add load testing for API endpoints
|
||||
// - Implement test data factories
|
||||
|
||||
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 type { APIContext } from 'astro';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { accounts, transactions } from '../data/store';
|
||||
import { createTransaction } from '../pages/api/transactions';
|
||||
import { updateTransaction } from '../pages/api/transactions/[id]';
|
||||
import { DELETE as deleteTransaction } from '../pages/api/transactions/[id]/index';
|
||||
import type { Transaction } from '../types';
|
||||
import { createMockAPIContext } from './setup';
|
||||
|
||||
@@ -28,7 +27,7 @@ describe('Transactions API', () => {
|
||||
amount: -25.0,
|
||||
};
|
||||
|
||||
const ctx = createMockAPIContext() as any;
|
||||
const ctx = createMockAPIContext() as APIContext;
|
||||
ctx.request = new Request('http://localhost:4321/api/transactions', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
@@ -50,7 +49,7 @@ describe('Transactions API', () => {
|
||||
// Missing required fields
|
||||
};
|
||||
|
||||
const ctx = createMockAPIContext() as any;
|
||||
const ctx = createMockAPIContext() as APIContext;
|
||||
ctx.request = new Request('http://localhost:4321/api/transactions', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
@@ -72,7 +71,7 @@ describe('Transactions API', () => {
|
||||
amount: 100,
|
||||
};
|
||||
|
||||
const ctx = createMockAPIContext() as any;
|
||||
const ctx = createMockAPIContext() as APIContext;
|
||||
ctx.request = new Request('http://localhost:4321/api/transactions', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
@@ -87,7 +86,7 @@ describe('Transactions API', () => {
|
||||
});
|
||||
|
||||
it('should reject invalid request body', async () => {
|
||||
const ctx = createMockAPIContext() as any;
|
||||
const ctx = createMockAPIContext() as APIContext;
|
||||
ctx.request = new Request('http://localhost:4321/api/transactions', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
@@ -111,7 +110,7 @@ describe('Transactions API', () => {
|
||||
amount: -75.0,
|
||||
};
|
||||
|
||||
const ctx = createMockAPIContext({ params: { id: '1' } }) as any;
|
||||
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' },
|
||||
@@ -128,7 +127,7 @@ describe('Transactions API', () => {
|
||||
});
|
||||
|
||||
it('should reject update with invalid request body', async () => {
|
||||
const ctx = createMockAPIContext({ params: { id: '1' } }) as any;
|
||||
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' },
|
||||
@@ -143,7 +142,7 @@ describe('Transactions API', () => {
|
||||
});
|
||||
|
||||
it('should reject update for non-existent transaction', async () => {
|
||||
const ctx = createMockAPIContext({ params: { id: '999' } }) as any;
|
||||
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' },
|
||||
@@ -161,7 +160,7 @@ describe('Transactions API', () => {
|
||||
// First update the transaction to point to a non-existent account
|
||||
transactions[0].accountId = '999';
|
||||
|
||||
const ctx = createMockAPIContext({ params: { id: '1' } }) as any;
|
||||
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' },
|
||||
@@ -187,7 +186,7 @@ describe('Transactions API', () => {
|
||||
const oldTransaction = transactions.find((t) => t.id === '1');
|
||||
if (!oldTransaction) throw new Error('Test transaction not found');
|
||||
|
||||
const ctx = createMockAPIContext({ params: { id: '1' } }) as any;
|
||||
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' },
|
||||
@@ -211,7 +210,7 @@ describe('Transactions API', () => {
|
||||
});
|
||||
|
||||
it('should reject update without transaction ID', async () => {
|
||||
const ctx = createMockAPIContext() as any;
|
||||
const ctx = createMockAPIContext() as APIContext;
|
||||
ctx.request = new Request('http://localhost:4321/api/transactions/undefined', {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
@@ -230,7 +229,7 @@ describe('Transactions API', () => {
|
||||
const savedAccounts = [...accounts];
|
||||
accounts.length = 0;
|
||||
|
||||
const ctx = createMockAPIContext({ params: { id: '1' } }) as any;
|
||||
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' },
|
||||
@@ -248,7 +247,7 @@ describe('Transactions API', () => {
|
||||
});
|
||||
|
||||
it("should reject update when new account doesn't exist", async () => {
|
||||
const ctx = createMockAPIContext({ params: { id: '1' } }) as any;
|
||||
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' },
|
||||
@@ -273,7 +272,7 @@ describe('Transactions API', () => {
|
||||
const initialCount = transactions.length;
|
||||
|
||||
const response = await deleteTransaction(
|
||||
createMockAPIContext({ params: { id: '1' } }) as any
|
||||
createMockAPIContext({ params: { id: '1' } }) as APIContext,
|
||||
);
|
||||
|
||||
expect(response.status).toBe(204);
|
||||
@@ -282,7 +281,7 @@ describe('Transactions API', () => {
|
||||
});
|
||||
|
||||
it('should reject delete without transaction ID', async () => {
|
||||
const response = await deleteTransaction(createMockAPIContext() as any);
|
||||
const response = await deleteTransaction(createMockAPIContext() as APIContext);
|
||||
|
||||
const error = await response.json();
|
||||
|
||||
@@ -292,7 +291,7 @@ describe('Transactions API', () => {
|
||||
|
||||
it('should return 404 for non-existent transaction', async () => {
|
||||
const response = await deleteTransaction(
|
||||
createMockAPIContext({ params: { id: '999' } }) as any
|
||||
createMockAPIContext({ params: { id: '999' } }) as APIContext,
|
||||
);
|
||||
|
||||
const error = await response.json();
|
||||
@@ -313,7 +312,7 @@ describe('Transactions API', () => {
|
||||
transactions.push(testTransaction);
|
||||
|
||||
const response = await deleteTransaction(
|
||||
createMockAPIContext({ params: { id: 'test-delete' } }) as any
|
||||
createMockAPIContext({ params: { id: 'test-delete' } }) as APIContext,
|
||||
);
|
||||
|
||||
const error = await response.json();
|
||||
|
||||
Reference in New Issue
Block a user