Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/backend/controllers/library.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const addAlbum: RequestHandler = async (req: Request<object, object, NewA
console.error('Error: Failed to get artist_id from name');
console.error(e);
next(e);
return;
}
}
if (!artist_id) {
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/controllers/library.addAlbum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { jest } from '@jest/globals';

jest.mock('../../../apps/backend/services/library.service', () => ({
artistIdFromName: jest.fn(),
}));

import { Request, Response, NextFunction } from 'express';
import { addAlbum } from '../../../apps/backend/controllers/library.controller';
import * as libraryService from '../../../apps/backend/services/library.service';

function buildReqResNext(body: Record<string, unknown>) {
const req = { body } as unknown as Request;
const res = {
status: jest.fn().mockReturnThis(),
send: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis(),
} as unknown as Response;
const next = jest.fn() as unknown as NextFunction;
return { req, res, next };
}

describe('addAlbum', () => {
it('should not send a response after calling next(e) when artistIdFromName throws', async () => {
const error = new Error('db connection lost');
(libraryService.artistIdFromName as jest.Mock).mockRejectedValue(error);

const { req, res, next } = buildReqResNext({
album_title: 'Test Album',
label: 'Test Label',
genre_id: 1,
format_id: 1,
artist_name: 'Test Artist',
});

await addAlbum(req, res, next);

expect(next).toHaveBeenCalledWith(error);
expect(res.send as jest.Mock).not.toHaveBeenCalled();
});
});
Loading