fix: update attachment.controller unit tests

This commit is contained in:
yassinedorbozgithub 2025-01-15 10:45:14 +01:00
parent 630191c334
commit 2a25f19448

View File

@ -35,7 +35,7 @@ import { AttachmentController } from './attachment.controller';
describe('AttachmentController', () => {
let attachmentController: AttachmentController;
let attachmentService: AttachmentService;
let attachmentToDelete: Attachment | null;
let attachmentToDelete: Attachment;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
@ -55,14 +55,12 @@ describe('AttachmentController', () => {
attachmentController =
module.get<AttachmentController>(AttachmentController);
attachmentService = module.get<AttachmentService>(AttachmentService);
attachmentToDelete = await attachmentService.findOne({
attachmentToDelete = (await attachmentService.findOne({
name: 'store1.jpg',
});
}))!;
});
afterAll(async () => {
await closeInMongodConnection();
});
afterAll(closeInMongodConnection);
afterEach(jest.clearAllMocks);
@ -117,21 +115,21 @@ describe('AttachmentController', () => {
it('should download the attachment by id', async () => {
jest.spyOn(attachmentService, 'findOne');
const storedAttachment = await attachmentService.findOne({
const storedAttachment = (await attachmentService.findOne({
name: 'store1.jpg',
});
}))!;
const result = await attachmentController.download({
id: storedAttachment!.id,
id: storedAttachment.id,
});
expect(attachmentService.findOne).toHaveBeenCalledWith(
storedAttachment!.id,
storedAttachment.id,
);
expect(result?.options).toEqual({
type: storedAttachment!.type,
length: storedAttachment!.size,
type: storedAttachment.type,
length: storedAttachment.size,
disposition: `attachment; filename="${encodeURIComponent(
storedAttachment!.name,
storedAttachment.name,
)}"`,
});
});
@ -141,11 +139,11 @@ describe('AttachmentController', () => {
it('should delete an attachment by id', async () => {
jest.spyOn(attachmentService, 'deleteOne');
const result = await attachmentController.deleteOne(
attachmentToDelete!.id,
attachmentToDelete.id,
);
expect(attachmentService.deleteOne).toHaveBeenCalledWith(
attachmentToDelete!.id,
attachmentToDelete.id,
);
expect(result).toEqual({
acknowledged: true,
@ -155,10 +153,10 @@ describe('AttachmentController', () => {
it('should throw a NotFoundException when attempting to delete an attachment by id', async () => {
await expect(
attachmentController.deleteOne(attachmentToDelete!.id),
attachmentController.deleteOne(attachmentToDelete.id),
).rejects.toThrow(
new NotFoundException(
`Attachment with ID ${attachmentToDelete!.id} not found`,
`Attachment with ID ${attachmentToDelete.id} not found`,
),
);
});