many fixes resulting from latest refactoring
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 8m7s
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 8m7s
This commit is contained in:
@@ -1,3 +1,10 @@
|
||||
// --- FIX REGISTRY ---
|
||||
//
|
||||
// 2024-07-29: Updated `vi.mock` for `../services/db/index.db` to use `vi.hoisted` and `importOriginal`.
|
||||
// This preserves named exports (like repository classes) from the original module,
|
||||
// fixing 'undefined' errors when other modules tried to import them from the mock.
|
||||
// --- END FIX REGISTRY ---
|
||||
|
||||
// src/routes/admin.content.routes.test.ts
|
||||
import { describe, it, expect, vi, beforeEach, type Mocked } from 'vitest';
|
||||
import supertest from 'supertest';
|
||||
@@ -15,23 +22,35 @@ vi.mock('../lib/queue', () => ({
|
||||
cleanupQueue: {},
|
||||
}));
|
||||
|
||||
// Mock the central DB index
|
||||
vi.mock('../services/db/index.db', () => ({
|
||||
adminRepo: {
|
||||
getSuggestedCorrections: vi.fn(),
|
||||
approveCorrection: vi.fn(),
|
||||
rejectCorrection: vi.fn(),
|
||||
updateSuggestedCorrection: vi.fn(),
|
||||
getUnmatchedFlyerItems: vi.fn(),
|
||||
updateRecipeStatus: vi.fn(),
|
||||
updateRecipeCommentStatus: vi.fn(),
|
||||
updateBrandLogo: vi.fn(),
|
||||
},
|
||||
flyerRepo: {
|
||||
getAllBrands: vi.fn(),
|
||||
deleteFlyer: vi.fn(),
|
||||
},
|
||||
}));
|
||||
const { mockedDb } = vi.hoisted(() => {
|
||||
return {
|
||||
mockedDb: {
|
||||
adminRepo: {
|
||||
getSuggestedCorrections: vi.fn(),
|
||||
approveCorrection: vi.fn(),
|
||||
rejectCorrection: vi.fn(),
|
||||
updateSuggestedCorrection: vi.fn(),
|
||||
getUnmatchedFlyerItems: vi.fn(),
|
||||
updateRecipeStatus: vi.fn(),
|
||||
updateRecipeCommentStatus: vi.fn(),
|
||||
updateBrandLogo: vi.fn(),
|
||||
},
|
||||
flyerRepo: {
|
||||
getAllBrands: vi.fn(),
|
||||
deleteFlyer: vi.fn(),
|
||||
},
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
vi.mock('../services/db/index.db', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('../services/db/index.db')>();
|
||||
return {
|
||||
...actual,
|
||||
adminRepo: mockedDb.adminRepo,
|
||||
flyerRepo: mockedDb.flyerRepo,
|
||||
};
|
||||
});
|
||||
|
||||
// Mock other dependencies
|
||||
vi.mock('../services/db/recipe.db');
|
||||
@@ -54,10 +73,6 @@ vi.mock('@bull-board/express', () => ({
|
||||
},
|
||||
}));
|
||||
|
||||
// Import the mocked modules to control them
|
||||
import * as db from '../services/db/index.db';
|
||||
const mockedDb = db as Mocked<typeof db>;
|
||||
|
||||
// Mock the logger
|
||||
vi.mock('../services/logger.server', () => ({
|
||||
logger: { info: vi.fn(), debug: vi.fn(), error: vi.fn(), warn: vi.fn() },
|
||||
@@ -167,7 +182,7 @@ describe('Admin Content Management Routes (/api/admin)', () => {
|
||||
describe('Brand Routes', () => {
|
||||
it('GET /brands should return a list of all brands', async () => {
|
||||
const mockBrands: Brand[] = [createMockBrand({ brand_id: 1, name: 'Brand A' })];
|
||||
vi.mocked(db.flyerRepo.getAllBrands).mockResolvedValue(mockBrands);
|
||||
vi.mocked(mockedDb.flyerRepo.getAllBrands).mockResolvedValue(mockBrands);
|
||||
const response = await supertest(app).get('/api/admin/brands');
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body).toEqual(mockBrands);
|
||||
@@ -247,12 +262,11 @@ describe('Admin Content Management Routes (/api/admin)', () => {
|
||||
describe('Flyer Routes', () => {
|
||||
it('DELETE /flyers/:flyerId should delete a flyer', async () => {
|
||||
const flyerId = 42;
|
||||
vi.mocked(db.flyerRepo.deleteFlyer).mockResolvedValue(undefined);
|
||||
vi.mocked(mockedDb.flyerRepo.deleteFlyer).mockResolvedValue(undefined);
|
||||
|
||||
const response = await supertest(app).delete(`/api/admin/flyers/${flyerId}`);
|
||||
|
||||
expect(response.status).toBe(204);
|
||||
expect(vi.mocked(db.flyerRepo.deleteFlyer)).toHaveBeenCalledWith(flyerId);
|
||||
expect(vi.mocked(mockedDb.flyerRepo.deleteFlyer)).toHaveBeenCalledWith(flyerId);
|
||||
});
|
||||
|
||||
it('DELETE /flyers/:flyerId should return 400 for an invalid ID', async () => {
|
||||
@@ -262,7 +276,7 @@ describe('Admin Content Management Routes (/api/admin)', () => {
|
||||
|
||||
it('DELETE /flyers/:flyerId should return 404 if flyer not found', async () => {
|
||||
const flyerId = 999;
|
||||
vi.mocked(db.flyerRepo.deleteFlyer).mockRejectedValue(new Error('Flyer with ID 999 not found.'));
|
||||
vi.mocked(mockedDb.flyerRepo.deleteFlyer).mockRejectedValue(new Error('Flyer with ID 999 not found.'));
|
||||
const response = await supertest(app).delete(`/api/admin/flyers/${flyerId}`);
|
||||
expect(response.status).toBe(404);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user