App.tsx refactor + even more unit tests
All checks were successful
Deploy to Web Server flyer-crawler.projectium.com / deploy (push) Successful in 8m28s

This commit is contained in:
2025-12-08 12:18:46 -08:00
parent e022a4a2cc
commit 0eda796fad
34 changed files with 1999 additions and 611 deletions

View File

@@ -110,6 +110,12 @@ describe('Admin Content Management Routes (/api/admin)', () => {
expect(response.body).toEqual(mockCorrections);
});
it('should return 500 if the database call fails', async () => {
mockedDb.getSuggestedCorrections.mockRejectedValue(new Error('DB Error'));
const response = await supertest(app).get('/api/admin/corrections');
expect(response.status).toBe(500);
});
it('POST /corrections/:id/approve should approve a correction', async () => {
const correctionId = 123;
mockedDb.approveCorrection.mockResolvedValue(undefined);
@@ -119,6 +125,12 @@ describe('Admin Content Management Routes (/api/admin)', () => {
expect(mockedDb.approveCorrection).toHaveBeenCalledWith(correctionId);
});
it('POST /corrections/:id/approve should return 400 for an invalid ID', async () => {
const response = await supertest(app).post('/api/admin/corrections/abc/approve');
expect(response.status).toBe(400);
expect(response.body.message).toBe('Invalid correction ID provided.');
});
it('POST /corrections/:id/reject should reject a correction', async () => {
const correctionId = 789;
mockedDb.rejectCorrection.mockResolvedValue(undefined);
@@ -127,6 +139,12 @@ describe('Admin Content Management Routes (/api/admin)', () => {
expect(response.body).toEqual({ message: 'Correction rejected successfully.' });
});
it('PUT /corrections/:id should return 400 if suggested_value is missing', async () => {
const response = await supertest(app).put('/api/admin/corrections/101').send({});
expect(response.status).toBe(400);
expect(response.body.message).toBe('A new suggested_value is required.');
});
it('PUT /corrections/:id should update a correction', async () => {
const correctionId = 101;
const requestBody = { suggested_value: 'A new corrected value' };
@@ -136,6 +154,12 @@ describe('Admin Content Management Routes (/api/admin)', () => {
expect(response.status).toBe(200);
expect(response.body).toEqual(mockUpdatedCorrection);
});
it('PUT /corrections/:id should return 404 if correction not found', async () => {
mockedDb.updateSuggestedCorrection.mockRejectedValue(new Error('Correction with ID 999 not found'));
const response = await supertest(app).put('/api/admin/corrections/999').send({ suggested_value: 'new value' });
expect(response.status).toBe(404);
});
});
describe('Brand Routes', () => {
@@ -157,6 +181,12 @@ describe('Admin Content Management Routes (/api/admin)', () => {
expect(response.body.message).toBe('Brand logo updated successfully.');
expect(mockedDb.updateBrandLogo).toHaveBeenCalledWith(brandId, expect.stringContaining('/assets/'));
});
it('POST /brands/:id/logo should return 400 if no file is uploaded', async () => {
const response = await supertest(app).post('/api/admin/brands/55/logo');
expect(response.status).toBe(400);
expect(response.body.message).toBe('Logo image file is required.');
});
});
describe('Recipe and Comment Routes', () => {
@@ -170,6 +200,12 @@ describe('Admin Content Management Routes (/api/admin)', () => {
expect(response.body).toEqual(mockUpdatedRecipe);
});
it('PUT /recipes/:id/status should return 400 for an invalid status', async () => {
const response = await supertest(app).put('/api/admin/recipes/201').send({ status: 'invalid-status' });
expect(response.status).toBe(400);
expect(response.body.message).toContain('A valid status');
});
it('PUT /comments/:id/status should update a comment status', async () => {
const commentId = 301;
const requestBody = { status: 'hidden' as const };
@@ -179,6 +215,12 @@ describe('Admin Content Management Routes (/api/admin)', () => {
expect(response.status).toBe(200);
expect(response.body).toEqual(mockUpdatedComment);
});
it('PUT /comments/:id/status should return 400 for an invalid status', async () => {
const response = await supertest(app).put('/api/admin/comments/301').send({ status: 'invalid-status' });
expect(response.status).toBe(400);
expect(response.body.message).toContain('A valid status');
});
});
describe('Unmatched Items Route', () => {