fix: CMS module issues

This commit is contained in:
yassinedorbozgithub
2025-01-07 11:37:03 +01:00
parent 2910de0058
commit 0101655e33
25 changed files with 235 additions and 207 deletions

View File

@@ -39,7 +39,7 @@ describe('ContentTypeController', () => {
let contentTypeController: ContentTypeController;
let contentTypeService: ContentTypeService;
let contentService: ContentService;
let contentType: ContentType;
let contentType: ContentType | null;
let blockService: BlockService;
beforeAll(async () => {
@@ -76,12 +76,10 @@ describe('ContentTypeController', () => {
);
contentTypeService = module.get<ContentTypeService>(ContentTypeService);
contentService = module.get<ContentService>(ContentService);
contentType = await contentTypeService.findOne({ name: 'Product' });
contentType = await contentTypeService.findOne({ name: 'Product' })!;
});
afterAll(async () => {
await closeInMongodConnection();
});
afterAll(closeInMongodConnection);
afterEach(jest.clearAllMocks);
@@ -138,10 +136,10 @@ describe('ContentTypeController', () => {
describe('findOne', () => {
it('should find a content type by id', async () => {
jest.spyOn(contentTypeService, 'findOne');
const result = await contentTypeController.findOne(contentType.id);
expect(contentTypeService.findOne).toHaveBeenCalledWith(contentType.id);
const result = await contentTypeController.findOne(contentType!.id);
expect(contentTypeService.findOne).toHaveBeenCalledWith(contentType!.id);
expect(result).toEqualPayload(
contentTypeFixtures.find(({ name }) => name === 'Product'),
contentTypeFixtures.find(({ name }) => name === 'Product')!,
);
});
@@ -160,10 +158,10 @@ describe('ContentTypeController', () => {
jest.spyOn(contentTypeService, 'updateOne');
const result = await contentTypeController.updateOne(
updatedContent,
contentType.id,
contentType!.id,
);
expect(contentTypeService.updateOne).toHaveBeenCalledWith(
contentType.id,
contentType!.id,
updatedContent,
);
expect(result).toEqualPayload({
@@ -190,17 +188,19 @@ describe('ContentTypeController', () => {
const contentType = await contentTypeService.findOne({
name: 'Restaurant',
});
const result = await contentTypeController.deleteOne(contentType.id);
const result = await contentTypeController.deleteOne(contentType!.id);
expect(contentTypeService.deleteCascadeOne).toHaveBeenCalledWith(
contentType.id,
contentType!.id,
);
expect(result).toEqual({ acknowledged: true, deletedCount: 1 });
await expect(
contentTypeController.findOne(contentType.id),
contentTypeController.findOne(contentType!.id),
).rejects.toThrow(NotFoundException);
expect(await contentService.find({ entity: contentType.id })).toEqual([]);
expect(await contentService.find({ entity: contentType!.id })).toEqual(
[],
);
});
it('should throw NotFoundException if the content type is not found', async () => {

View File

@@ -15,8 +15,8 @@ import { Test, TestingModule } from '@nestjs/testing';
import { AttachmentRepository } from '@/attachment/repositories/attachment.repository';
import {
AttachmentModel,
Attachment,
AttachmentModel,
} from '@/attachment/schemas/attachment.schema';
import { AttachmentService } from '@/attachment/services/attachment.service';
import { LoggerService } from '@/logger/logger.service';
@@ -48,9 +48,9 @@ describe('ContentController', () => {
let contentService: ContentService;
let contentTypeService: ContentTypeService;
let attachmentService: AttachmentService;
let contentType: ContentType;
let content: Content;
let attachment: Attachment;
let contentType: ContentType | null;
let content: Content | null;
let attachment: Attachment | null;
let updatedContent;
let pageQuery: PageQueryDto<Content>;
@@ -94,21 +94,19 @@ describe('ContentController', () => {
});
});
afterAll(async () => {
await closeInMongodConnection();
});
afterAll(closeInMongodConnection);
afterEach(jest.clearAllMocks);
describe('findOne', () => {
it('should find content by ID', async () => {
const contentType = await contentTypeService.findOne(content.entity);
const contentType = await contentTypeService.findOne(content!.entity);
jest.spyOn(contentService, 'findOne');
const result = await contentController.findOne(content.id, []);
expect(contentService.findOne).toHaveBeenCalledWith(content.id);
const result = await contentController.findOne(content!.id, []);
expect(contentService.findOne).toHaveBeenCalledWith(content!.id);
expect(result).toEqualPayload({
...contentFixtures.find(({ title }) => title === 'Jean'),
entity: contentType.id,
entity: contentType!.id,
});
});
@@ -121,8 +119,8 @@ describe('ContentController', () => {
});
it('should find content by ID and populate its corresponding content type', async () => {
const result = await contentController.findOne(content.id, ['entity']);
const contentType = await contentTypeService.findOne(content.entity);
const result = await contentController.findOne(content!.id, ['entity']);
const contentType = await contentTypeService.findOne(content!.entity);
expect(result).toEqualPayload({
...contentFixtures.find(({ title }) => title === 'Jean'),
@@ -137,7 +135,7 @@ describe('ContentController', () => {
expect(result).toEqualPayload([
{
...contentFixtures.find(({ title }) => title === 'Jean'),
entity: contentType.id,
entity: contentType!.id,
},
]);
});
@@ -160,12 +158,14 @@ describe('ContentController', () => {
describe('findByType', () => {
it('should find contents by content type', async () => {
const result = await contentController.findByType(
contentType.id,
contentType!.id,
pageQuery,
);
const contents = contentFixtures.filter(({ entity }) => entity === '0');
contents.reduce((acc, curr) => {
curr['entity'] = contentType.id;
if (contentType?.id) {
curr['entity'] = contentType.id;
}
return acc;
}, []);
expect(result).toEqualPayload([contents[0]]);
@@ -174,15 +174,15 @@ describe('ContentController', () => {
describe('update', () => {
it('should update and return the updated content', async () => {
const contentType = await contentTypeService.findOne(content.entity);
const contentType = await contentTypeService.findOne(content!.entity);
updatedContent = {
...contentFixtures.find(({ title }) => title === 'Jean'),
entity: contentType.id,
entity: contentType!.id,
title: 'modified Jean',
};
const result = await contentController.updateOne(
updatedContent,
content.id,
content!.id,
);
expect(result).toEqualPayload(updatedContent, [
@@ -201,7 +201,7 @@ describe('ContentController', () => {
describe('deleteOne', () => {
it('should delete an existing Content', async () => {
const content = await contentService.findOne({ title: 'Adaptateur' });
const result = await contentService.deleteOne(content.id);
const result = await contentService.deleteOne(content!.id);
expect(result).toEqual({ acknowledged: true, deletedCount: 1 });
});
});
@@ -219,7 +219,7 @@ describe('ContentController', () => {
},
},
},
entity: contentType.id,
entity: contentType!.id,
status: true,
};
jest.spyOn(contentService, 'create');
@@ -263,18 +263,18 @@ should not appear,store 3,true,image.jpg`;
});
const result = await contentController.import({
idFileToImport: attachment.id,
idTargetContentType: contentType.id,
idFileToImport: attachment!.id,
idTargetContentType: contentType!.id,
});
expect(contentService.createMany).toHaveBeenCalledWith([
{ ...mockCsvContentDto, entity: contentType.id },
{ ...mockCsvContentDto, entity: contentType!.id },
]);
expect(result).toEqualPayload(
[
{
...mockCsvContentDto,
entity: contentType.id,
entity: contentType!.id,
},
],
[...IGNORED_TEST_FIELDS, 'rag'],
@@ -284,7 +284,7 @@ should not appear,store 3,true,image.jpg`;
it('should throw NotFoundException if content type is not found', async () => {
await expect(
contentController.import({
idFileToImport: attachment.id,
idFileToImport: attachment!.id,
idTargetContentType: NOT_FOUND_ID,
}),
).rejects.toThrow(new NotFoundException('Content type is not found'));
@@ -298,7 +298,7 @@ should not appear,store 3,true,image.jpg`;
await expect(
contentController.import({
idFileToImport: NOT_FOUND_ID,
idTargetContentType: contentType.id.toString(),
idTargetContentType: contentType!.id.toString(),
}),
).rejects.toThrow(new NotFoundException('File does not exist'));
});
@@ -307,8 +307,8 @@ should not appear,store 3,true,image.jpg`;
jest.spyOn(fs, 'existsSync').mockReturnValue(false);
await expect(
contentController.import({
idFileToImport: attachment.id,
idTargetContentType: contentType.id,
idFileToImport: attachment!.id,
idTargetContentType: contentType!.id,
}),
).rejects.toThrow(new NotFoundException('File does not exist'));
});

View File

@@ -131,7 +131,7 @@ export class ContentController extends BaseController<
? path.join(config.parameters.uploadDir, file.location)
: undefined;
if (!file || !fs.existsSync(filePath)) {
if (!file || !filePath || !fs.existsSync(filePath)) {
this.logger.warn(`Failed to find file type with id ${fileToImport}.`);
throw new NotFoundException(`File does not exist`);
}
@@ -159,12 +159,12 @@ export class ContentController extends BaseController<
(acc, { title, status, ...rest }) => [
...acc,
{
title,
status,
title: String(title),
status: Boolean(status),
entity: targetContentType,
dynamicFields: Object.keys(rest)
.filter((key) =>
contentType.fields.map((field) => field.name).includes(key),
contentType.fields?.map((field) => field.name).includes(key),
)
.reduce((filtered, key) => ({ ...filtered, [key]: rest[key] }), {}),
},

View File

@@ -60,9 +60,7 @@ describe('MenuController', () => {
menuService = module.get<MenuService>(MenuService);
});
afterAll(async () => {
await closeInMongodConnection();
});
afterAll(closeInMongodConnection);
afterEach(jest.clearAllMocks);
describe('create', () => {
@@ -81,7 +79,7 @@ describe('MenuController', () => {
const websiteMenu = await menuService.findOne({
title: websiteMenuFixture.title,
});
const search = await menuController.findOne(websiteMenu.id);
const search = await menuController.findOne(websiteMenu!.id);
expect(search).toEqualPayload(websiteMenuFixture);
});
});
@@ -98,12 +96,12 @@ describe('MenuController', () => {
const offersEntry = await menuService.findOne({
title: offerMenuFixture.title,
});
await menuController.delete(offersEntry.id);
await menuController.delete(offersEntry!.id);
const offersChildren = await menuService.find({
title: {
$in: [
offersEntry.title,
offersEntry!.title,
...offersMenuFixtures.map((menu) => menu.title),
],
},

View File

@@ -94,7 +94,9 @@ export class MenuController extends BaseController<
this.validate({
dto: body,
allowedIds: {
parent: (await this.menuService.findOne(body.parent))?.id,
parent: body?.parent
? (await this.menuService.findOne(body.parent))?.id
: undefined,
},
});
return await this.menuService.create(body);