Some checks failed
Deploy to Test Environment / deploy-to-test (push) Has been cancelled
- Added tests for invalid request bodies in price and recipe routes. - Improved type safety in request handlers using Zod schemas. - Introduced a consistent validation pattern for empty request bodies. - Enhanced error messages for invalid query parameters in stats and user routes. - Implemented middleware to inject mock logging for tests. - Created a custom type for validated requests to streamline type inference.
110 lines
4.3 KiB
TypeScript
110 lines
4.3 KiB
TypeScript
// src/routes/personalization.routes.test.ts
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
import supertest from 'supertest';
|
|
import express, { Request, Response, NextFunction } from 'express';
|
|
import personalizationRouter from './personalization.routes';
|
|
import { createMockMasterGroceryItem, createMockDietaryRestriction, createMockAppliance } from '../tests/utils/mockFactories';
|
|
import { errorHandler } from '../middleware/errorHandler';
|
|
|
|
// 1. Mock the Service Layer directly.
|
|
vi.mock('../services/db/index.db', () => ({
|
|
personalizationRepo: {
|
|
getAllMasterItems: vi.fn(),
|
|
getDietaryRestrictions: vi.fn(),
|
|
getAppliances: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
// Mock the logger to keep test output clean
|
|
vi.mock('../services/logger.server', () => ({
|
|
logger: {
|
|
error: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
// Import the mocked db module to control its functions in tests
|
|
import * as db from '../services/db/index.db';
|
|
|
|
// Create the Express app
|
|
const app = express();
|
|
app.use(express.json());
|
|
|
|
// Add a middleware to inject a mock req.log object for tests
|
|
app.use((req, res, next) => {
|
|
req.log = { info: vi.fn(), debug: vi.fn(), error: vi.fn(), warn: vi.fn() } as any;
|
|
next();
|
|
});
|
|
|
|
// Mount the router under its designated base path
|
|
app.use('/api/personalization', personalizationRouter);
|
|
app.use(errorHandler);
|
|
|
|
describe('Personalization Routes (/api/personalization)', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe('GET /master-items', () => {
|
|
it('should return a list of master items', async () => {
|
|
const mockItems = [createMockMasterGroceryItem({ master_grocery_item_id: 1, name: 'Milk' })];
|
|
vi.mocked(db.personalizationRepo.getAllMasterItems).mockResolvedValue(mockItems);
|
|
|
|
const response = await supertest(app).get('/api/personalization/master-items');
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.body).toEqual(mockItems);
|
|
});
|
|
|
|
it('should return 500 if the database call fails', async () => {
|
|
vi.mocked(db.personalizationRepo.getAllMasterItems).mockRejectedValue(new Error('DB Error'));
|
|
const response = await supertest(app).get('/api/personalization/master-items');
|
|
expect(response.status).toBe(500);
|
|
expect(response.body.message).toBe('DB Error');
|
|
});
|
|
|
|
it('should return 500 if the database call fails for dietary restrictions', async () => {
|
|
vi.mocked(db.personalizationRepo.getDietaryRestrictions).mockRejectedValue(new Error('DB Error'));
|
|
const response = await supertest(app).get('/api/personalization/dietary-restrictions');
|
|
expect(response.status).toBe(500);
|
|
expect(response.body.message).toBe('DB Error');
|
|
});
|
|
});
|
|
|
|
describe('GET /dietary-restrictions', () => {
|
|
it('should return a list of all dietary restrictions', async () => {
|
|
const mockRestrictions = [createMockDietaryRestriction({ name: 'Gluten-Free' })];
|
|
vi.mocked(db.personalizationRepo.getDietaryRestrictions).mockResolvedValue(mockRestrictions);
|
|
|
|
const response = await supertest(app).get('/api/personalization/dietary-restrictions');
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.body).toEqual(mockRestrictions);
|
|
});
|
|
|
|
it('should return 500 if the database call fails', async () => {
|
|
vi.mocked(db.personalizationRepo.getDietaryRestrictions).mockRejectedValue(new Error('DB Error'));
|
|
const response = await supertest(app).get('/api/personalization/dietary-restrictions');
|
|
expect(response.status).toBe(500);
|
|
expect(response.body.message).toBe('DB Error');
|
|
});
|
|
});
|
|
|
|
describe('GET /appliances', () => {
|
|
it('should return a list of all appliances', async () => {
|
|
const mockAppliances = [createMockAppliance({ name: 'Air Fryer' })];
|
|
vi.mocked(db.personalizationRepo.getAppliances).mockResolvedValue(mockAppliances);
|
|
|
|
const response = await supertest(app).get('/api/personalization/appliances');
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.body).toEqual(mockAppliances);
|
|
});
|
|
|
|
it('should return 500 if the database call fails', async () => {
|
|
vi.mocked(db.personalizationRepo.getAppliances).mockRejectedValue(new Error('DB Error'));
|
|
const response = await supertest(app).get('/api/personalization/appliances');
|
|
expect(response.status).toBe(500);
|
|
expect(response.body.message).toBe('DB Error');
|
|
});
|
|
});
|
|
}); |