mirror of
https://github.com/acedanger/finance.git
synced 2025-12-05 22:50:12 -08:00
refactor: Reorganize imports and improve code consistency across components and API routes
This commit is contained in:
@@ -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