testing routes
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 3m15s
Some checks failed
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Failing after 3m15s
This commit is contained in:
73
src/routes/public.routes.test.tx
Normal file
73
src/routes/public.routes.test.tx
Normal file
@@ -0,0 +1,73 @@
|
||||
// src/routes/public.routes.test.ts
|
||||
import { describe, it, expect, vi, beforeEach, type Mocked } from 'vitest';
|
||||
import supertest from 'supertest';
|
||||
import express from 'express';
|
||||
import publicRouter from './public'; // Import the router we want to test
|
||||
import * as db from '../services/db';
|
||||
|
||||
// Mock the entire db service
|
||||
vi.mock('../services/db');
|
||||
const mockedDb = db as Mocked<typeof db>;
|
||||
|
||||
// Mock the logger to keep test output clean
|
||||
vi.mock('../services/logger.server', () => ({
|
||||
logger: {
|
||||
info: vi.fn(),
|
||||
debug: vi.fn(),
|
||||
error: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
// Create a minimal Express app to host our router
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
// Mount the router under a base path, similar to how it's done in the main server
|
||||
app.use('/api', publicRouter);
|
||||
|
||||
describe('Public Routes (/api)', () => {
|
||||
beforeEach(() => {
|
||||
// Clear all mock history before each test
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('GET /health/ping', () => {
|
||||
it('should respond with 200 and "pong"', async () => {
|
||||
const response = await supertest(app).get('/api/health/ping');
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.text).toBe('pong');
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /flyers', () => {
|
||||
it('should return a list of flyers on success', async () => {
|
||||
// Arrange: Mock the database response
|
||||
const mockFlyers = [
|
||||
{ flyer_id: 1, store_name: 'Store A' },
|
||||
{ flyer_id: 2, store_name: 'Store B' },
|
||||
];
|
||||
mockedDb.getFlyers.mockResolvedValue(mockFlyers as any);
|
||||
|
||||
// Act: Make the request
|
||||
const response = await supertest(app).get('/api/flyers');
|
||||
|
||||
// Assert
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body).toEqual(mockFlyers);
|
||||
expect(mockedDb.getFlyers).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should handle database errors gracefully', async () => {
|
||||
// Arrange: Mock a database failure
|
||||
mockedDb.getFlyers.mockRejectedValue(new Error('DB Connection Failed'));
|
||||
|
||||
// Act
|
||||
const response = await supertest(app).get('/api/flyers');
|
||||
|
||||
// Assert: The global error handler should catch it and respond with 500
|
||||
expect(response.status).toBe(500);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user