fix: update unit tests

This commit is contained in:
yassinedorbozgithub 2024-12-04 08:47:57 +01:00
parent 32f3dc8271
commit 6cbedcbc16
20 changed files with 92 additions and 52 deletions

View File

@ -68,7 +68,10 @@ describe('BlockRepository', () => {
jest.spyOn(blockModel, 'findById');
const result = await blockRepository.findOneAndPopulate(hasNextBlocks.id);
expect(blockModel.findById).toHaveBeenCalledWith(hasNextBlocks.id);
expect(blockModel.findById).toHaveBeenCalledWith(
hasNextBlocks.id,
undefined,
);
expect(result).toEqualPayload({
...blockFixtures.find(({ name }) => name === hasNextBlocks.name),
category,
@ -92,7 +95,7 @@ describe('BlockRepository', () => {
blockFixture.name === 'hasNextBlocks' ? [hasPreviousBlocks] : [],
}));
expect(blockModel.find).toHaveBeenCalledWith({});
expect(blockModel.find).toHaveBeenCalledWith({}, undefined);
expect(result).toEqualPayload(blocksWithCategory);
});
@ -109,7 +112,7 @@ describe('BlockRepository', () => {
blockFixture.name === 'hasNextBlocks' ? [hasPreviousBlocks] : [],
}));
expect(blockModel.find).toHaveBeenCalledWith({});
expect(blockModel.find).toHaveBeenCalledWith({}, undefined);
expect(result).toEqualPayload(blocksWithCategory);
});
});

View File

@ -21,8 +21,8 @@ import {
rootMongooseTestModule,
} from '@/utils/test/test';
import { LabelModel, Label } from '../schemas/label.schema';
import { SubscriberModel, Subscriber } from '../schemas/subscriber.schema';
import { Label, LabelModel } from '../schemas/label.schema';
import { Subscriber, SubscriberModel } from '../schemas/subscriber.schema';
import { LabelRepository } from './label.repository';
import { SubscriberRepository } from './subscriber.repository';
@ -62,7 +62,7 @@ describe('LabelRepository', () => {
const label = await labelRepository.findOne({ name: 'TEST_TITLE_2' });
const result = await labelRepository.findOneAndPopulate(label.id);
expect(labelModel.findById).toHaveBeenCalledWith(label.id);
expect(labelModel.findById).toHaveBeenCalledWith(label.id, undefined);
expect(result).toEqualPayload({
...labelFixtures.find(({ name }) => name === label.name),
users,
@ -79,7 +79,7 @@ describe('LabelRepository', () => {
users,
}));
expect(labelModel.find).toHaveBeenCalledWith({});
expect(labelModel.find).toHaveBeenCalledWith({}, undefined);
expect(result).toEqualPayload(labelsWithUsers);
});
});
@ -94,7 +94,7 @@ describe('LabelRepository', () => {
users,
}));
expect(labelModel.find).toHaveBeenCalledWith({});
expect(labelModel.find).toHaveBeenCalledWith({}, undefined);
expect(result).toEqualPayload(labelsWithUsers.sort(sortRowsBy));
});
});

View File

@ -23,7 +23,7 @@ import {
rootMongooseTestModule,
} from '@/utils/test/test';
import { MessageModel, Message } from '../schemas/message.schema';
import { Message, MessageModel } from '../schemas/message.schema';
import { SubscriberModel } from '../schemas/subscriber.schema';
import { AnyMessage } from '../schemas/types/message';
@ -70,7 +70,7 @@ describe('MessageRepository', () => {
const user = await userRepository.findOne(message['sentBy']);
const result = await messageRepository.findOneAndPopulate(message.id);
expect(messageModel.findById).toHaveBeenCalledWith(message.id);
expect(messageModel.findById).toHaveBeenCalledWith(message.id, undefined);
expect(result).toEqualPayload({
...messageFixtures.find(({ mid }) => mid === message.mid),
sender,
@ -95,7 +95,7 @@ describe('MessageRepository', () => {
sentBy: allUsers.find(({ id }) => id === message['sentBy']).id,
}));
expect(messageModel.find).toHaveBeenCalledWith({});
expect(messageModel.find).toHaveBeenCalledWith({}, undefined);
expect(result).toEqualPayload(messages);
});
});

View File

@ -19,7 +19,7 @@ import {
import { AttachmentService } from '@/attachment/services/attachment.service';
import { LoggerService } from '@/logger/logger.service';
import { UserRepository } from '@/user/repositories/user.repository';
import { UserModel, User } from '@/user/schemas/user.schema';
import { User, UserModel } from '@/user/schemas/user.schema';
import {
installSubscriberFixtures,
subscriberFixtures,
@ -31,11 +31,11 @@ import {
rootMongooseTestModule,
} from '@/utils/test/test';
import { LabelModel, Label } from '../schemas/label.schema';
import { Label, LabelModel } from '../schemas/label.schema';
import {
SubscriberModel,
Subscriber,
SubscriberFull,
SubscriberModel,
} from '../schemas/subscriber.schema';
import { LabelRepository } from './label.repository';
@ -119,7 +119,10 @@ describe('SubscriberRepository', () => {
assignedTo: allUsers.find(({ id }) => subscriber.assignedTo === id),
};
expect(subscriberModel.findById).toHaveBeenCalledWith(subscriber.id);
expect(subscriberModel.findById).toHaveBeenCalledWith(
subscriber.id,
undefined,
);
expect(result).toEqualPayload(subscriberWithLabels);
});
});
@ -133,7 +136,7 @@ describe('SubscriberRepository', () => {
pageQuery,
);
expect(subscriberModel.find).toHaveBeenCalledWith({});
expect(subscriberModel.find).toHaveBeenCalledWith({}, undefined);
expect(result).toEqualPayload(
subscribersWithPopulatedFields.sort(sortRowsBy),
);
@ -145,7 +148,7 @@ describe('SubscriberRepository', () => {
jest.spyOn(subscriberModel, 'find');
const result = await subscriberRepository.findAllAndPopulate();
expect(subscriberModel.find).toHaveBeenCalledWith({});
expect(subscriberModel.find).toHaveBeenCalledWith({}, undefined);
expect(result).toEqualPayload(
subscribersWithPopulatedFields.sort(sortRowsBy),
);

View File

@ -170,7 +170,10 @@ describe('BlockService', () => {
jest.spyOn(blockRepository, 'findOneAndPopulate');
const result = await blockService.findOneAndPopulate(block.id);
expect(blockRepository.findOneAndPopulate).toHaveBeenCalledWith(block.id);
expect(blockRepository.findOneAndPopulate).toHaveBeenCalledWith(
block.id,
undefined,
);
expect(result).toEqualPayload({
...blockFixtures.find(({ name }) => name === 'hasNextBlocks'),
category,
@ -196,6 +199,7 @@ describe('BlockService', () => {
expect(blockRepository.findAndPopulate).toHaveBeenCalledWith(
{},
undefined,
undefined,
);
expect(result).toEqualPayload(blocksWithCategory);
});

View File

@ -113,6 +113,7 @@ describe('MessageService', () => {
expect(messageRepository.findOneAndPopulate).toHaveBeenCalledWith(
message.id,
undefined,
);
expect(result).toEqualPayload({
...messageFixtures.find(({ mid }) => mid === message.mid),

View File

@ -26,8 +26,8 @@ import {
import { Content, ContentModel } from '../schemas/content.schema';
import {
installContentFixtures,
contentFixtures,
installContentFixtures,
} from './../../utils/test/fixtures/content';
import { ContentRepository } from './content.repository';
@ -63,7 +63,7 @@ describe('ContentRepository', () => {
const content = await contentModel.findOne({ title: 'Jean' });
const contentType = await contentTypeModel.findById(content.entity);
const result = await contentRepository.findOneAndPopulate(content.id);
expect(findSpy).toHaveBeenCalledWith(content.id);
expect(findSpy).toHaveBeenCalledWith(content.id, undefined);
expect(result).toEqualPayload({
...contentFixtures.find(({ title }) => title === 'Jean'),
entity: contentTypeFixtures.find(

View File

@ -84,7 +84,7 @@ describe('ContentService', () => {
const content = await contentService.findOne({ title: 'Jean' });
const contentType = await contentTypeService.findOne(content.entity);
const result = await contentService.findOneAndPopulate(content.id);
expect(findSpy).toHaveBeenCalledWith(content.id);
expect(findSpy).toHaveBeenCalledWith(content.id, undefined);
expect(result).toEqualPayload({
...contentFixtures.find(({ title }) => title === 'Jean'),
entity: contentType,

View File

@ -23,7 +23,7 @@ import {
rootMongooseTestModule,
} from '@/utils/test/test';
import { InvitationModel, Invitation } from '../schemas/invitation.schema';
import { Invitation, InvitationModel } from '../schemas/invitation.schema';
import { PermissionModel } from '../schemas/permission.schema';
import { RoleModel } from '../schemas/role.schema';
@ -81,7 +81,10 @@ describe('InvitationRepository', () => {
const result = await invitationRepository.findOneAndPopulate(
invitation.id,
);
expect(invitationModel.findById).toHaveBeenCalledWith(invitation.id);
expect(invitationModel.findById).toHaveBeenCalledWith(
invitation.id,
undefined,
);
expect(result).toEqualPayload({
...toTestAgainst,
roles,
@ -110,7 +113,7 @@ describe('InvitationRepository', () => {
return acc;
}, []);
expect(invitationModel.find).toHaveBeenCalledWith({});
expect(invitationModel.find).toHaveBeenCalledWith({}, undefined);
expect(result).toEqualPayload(invitationsWithRoles);
});
});

View File

@ -56,7 +56,7 @@ describe('ModelRepository', () => {
it('should find a model and populate its permissions', async () => {
jest.spyOn(modelModel, 'findById');
const result = await modelRepository.findOneAndPopulate(model.id);
expect(modelModel.findById).toHaveBeenCalledWith(model.id);
expect(modelModel.findById).toHaveBeenCalledWith(model.id, undefined);
expect(result).toEqualPayload({
...modelFixtures.find(({ name }) => name === 'ContentType'),
permissions,
@ -79,7 +79,7 @@ describe('ModelRepository', () => {
});
return acc;
}, []);
expect(modelModel.find).toHaveBeenCalledWith({});
expect(modelModel.find).toHaveBeenCalledWith({}, undefined);
expect(result).toEqualPayload(modelsWithPermissions);
});
});

View File

@ -24,7 +24,7 @@ import { ModelRepository } from '../repositories/model.repository';
import { PermissionRepository } from '../repositories/permission.repository';
import { RoleRepository } from '../repositories/role.repository';
import { ModelModel } from '../schemas/model.schema';
import { PermissionModel, Permission } from '../schemas/permission.schema';
import { Permission, PermissionModel } from '../schemas/permission.schema';
import { RoleModel } from '../schemas/role.schema';
import { Action } from '../types/action.type';
@ -77,8 +77,12 @@ describe('PermissionRepository', () => {
const model = await modelRepository.findOne(permission.model);
const result = await permissionRepository.findOneAndPopulate(
permission.id,
undefined,
);
expect(permissionModel.findById).toHaveBeenCalledWith(
permission.id,
undefined,
);
expect(permissionModel.findById).toHaveBeenCalledWith(permission.id);
expect(result).toEqualPayload({
...permissionFixtures.find(({ action }) => action === 'create'),
role,
@ -111,7 +115,7 @@ describe('PermissionRepository', () => {
},
[],
);
expect(permissionModel.find).toHaveBeenCalledWith({});
expect(permissionModel.find).toHaveBeenCalledWith({}, undefined);
expect(result).toEqualPayload(permissionsWithRolesAndModels);
});
});

View File

@ -74,7 +74,7 @@ describe('RoleRepository', () => {
jest.spyOn(roleModel, 'findById');
const permissions = await permissionRepository.find({ role: role.id });
const result = await roleRepository.findOneAndPopulate(role.id);
expect(roleModel.findById).toHaveBeenCalledWith(role.id);
expect(roleModel.findById).toHaveBeenCalledWith(role.id, undefined);
expect(result).toEqualPayload({
...roleFixtures.find(({ name }) => name === 'admin'),
users,
@ -106,7 +106,7 @@ describe('RoleRepository', () => {
return acc;
}, []);
expect(roleModel.find).toHaveBeenCalledWith({});
expect(roleModel.find).toHaveBeenCalledWith({}, undefined);
expect(result).toEqualPayload(rolesWithPermissionsAndUsers);
});
});

View File

@ -27,8 +27,8 @@ import { PermissionRepository } from '../repositories/permission.repository';
import { RoleRepository } from '../repositories/role.repository';
import { UserRepository } from '../repositories/user.repository';
import { PermissionModel } from '../schemas/permission.schema';
import { RoleModel, Role } from '../schemas/role.schema';
import { UserModel, User } from '../schemas/user.schema';
import { Role, RoleModel } from '../schemas/role.schema';
import { User, UserModel } from '../schemas/user.schema';
describe('UserRepository', () => {
let roleRepository: RoleRepository;
@ -91,7 +91,7 @@ describe('UserRepository', () => {
it('should find one user and populate its role', async () => {
jest.spyOn(userModel, 'findById');
const result = await userRepository.findOneAndPopulate(user.id);
expect(userModel.findById).toHaveBeenCalledWith(user.id);
expect(userModel.findById).toHaveBeenCalledWith(user.id, undefined);
expect(result).toEqualPayload(
{
...userFixtures.find(({ username }) => username === 'admin'),
@ -118,7 +118,7 @@ describe('UserRepository', () => {
return acc;
}, []);
expect(userModel.find).toHaveBeenCalledWith({});
expect(userModel.find).toHaveBeenCalledWith({}, undefined);
expect(result).toEqualPayload(usersWithRoles);
});
});

View File

@ -77,7 +77,11 @@ describe('AuthService', () => {
'admin@admin.admin',
'adminadmin',
);
expect(userRepository.findOne).toHaveBeenCalledWith(searchCriteria, {});
expect(userRepository.findOne).toHaveBeenCalledWith(
searchCriteria,
{},
undefined,
);
expect(result.id).toBe(user.id);
});
it('should not validate user if the provided password is incorrect', async () => {
@ -85,7 +89,11 @@ describe('AuthService', () => {
'admin@admin.admin',
'randomPassword',
);
expect(userRepository.findOne).toHaveBeenCalledWith(searchCriteria, {});
expect(userRepository.findOne).toHaveBeenCalledWith(
searchCriteria,
{},
undefined,
);
expect(result).toBeNull();
});
@ -99,6 +107,7 @@ describe('AuthService', () => {
email: 'admin2@admin.admin',
},
{},
undefined,
);
expect(result).toBeNull();
});

View File

@ -19,8 +19,8 @@ import {
import { ModelRepository } from '../repositories/model.repository';
import { PermissionRepository } from '../repositories/permission.repository';
import { ModelModel, Model } from '../schemas/model.schema';
import { PermissionModel, Permission } from '../schemas/permission.schema';
import { Model, ModelModel } from '../schemas/model.schema';
import { Permission, PermissionModel } from '../schemas/permission.schema';
import { ModelService } from './model.service';
@ -83,6 +83,7 @@ describe('ModelService', () => {
expect(modelRepository.findAndPopulate).toHaveBeenCalledWith(
{},
undefined,
undefined,
);
expect(result).toEqualPayload(modelsWithPermissions);
});

View File

@ -26,9 +26,9 @@ import { PermissionRepository } from '../repositories/permission.repository';
import { RoleRepository } from '../repositories/role.repository';
import { ModelModel } from '../schemas/model.schema';
import {
PermissionModel,
Permission,
PermissionFull,
PermissionModel,
} from '../schemas/permission.schema';
import { RoleModel } from '../schemas/role.schema';
import { Action } from '../types/action.type';
@ -89,6 +89,7 @@ describe('PermissionService', () => {
const result = await permissionService.findOneAndPopulate(permission.id);
expect(permissionRepository.findOneAndPopulate).toHaveBeenLastCalledWith(
permission.id,
undefined,
);
expect(result).toEqualPayload({
...permissionFixtures.find(({ action }) => action === 'create'),

View File

@ -20,9 +20,9 @@ import {
import { PermissionRepository } from '../repositories/permission.repository';
import { RoleRepository } from '../repositories/role.repository';
import { UserRepository } from '../repositories/user.repository';
import { PermissionModel, Permission } from '../schemas/permission.schema';
import { RoleModel, Role } from '../schemas/role.schema';
import { UserModel, User } from '../schemas/user.schema';
import { Permission, PermissionModel } from '../schemas/permission.schema';
import { Role, RoleModel } from '../schemas/role.schema';
import { User, UserModel } from '../schemas/user.schema';
import { roleFixtures } from './../../utils/test/fixtures/role';
import { RoleService } from './role.service';
@ -72,8 +72,11 @@ describe('RoleService', () => {
describe('findOneAndPopulate', () => {
it('should find one role and populate its permissions and users', async () => {
jest.spyOn(roleRepository, 'findOneAndPopulate');
const result = await roleService.findOneAndPopulate(role.id);
expect(roleRepository.findOneAndPopulate).toHaveBeenCalledWith(role.id);
const result = await roleService.findOneAndPopulate(role.id, undefined);
expect(roleRepository.findOneAndPopulate).toHaveBeenCalledWith(
role.id,
undefined,
);
expect(result).toEqualPayload({
...roleFixtures.find(({ name }) => name == 'admin'),
users,

View File

@ -28,8 +28,8 @@ import { PermissionRepository } from '../repositories/permission.repository';
import { RoleRepository } from '../repositories/role.repository';
import { UserRepository } from '../repositories/user.repository';
import { PermissionModel } from '../schemas/permission.schema';
import { RoleModel, Role } from '../schemas/role.schema';
import { UserModel, User } from '../schemas/user.schema';
import { Role, RoleModel } from '../schemas/role.schema';
import { User, UserModel } from '../schemas/user.schema';
import { PermissionService } from './permission.service';
import { RoleService } from './role.service';
@ -100,7 +100,10 @@ describe('UserService', () => {
it('should find one user and populate its role', async () => {
jest.spyOn(userRepository, 'findOneAndPopulate');
const result = await userService.findOneAndPopulate(user.id);
expect(userRepository.findOneAndPopulate).toHaveBeenCalledWith(user.id);
expect(userRepository.findOneAndPopulate).toHaveBeenCalledWith(
user.id,
undefined,
);
expect(result).toEqualPayload(
{
...userFixtures.find(({ username }) => username === 'admin'),

View File

@ -72,7 +72,7 @@ describe('BaseRepository', () => {
jest.spyOn(dummyModel, 'findById');
const result = await dummyRepository.findOne(createdId);
expect(dummyModel.findById).toHaveBeenCalledWith(createdId);
expect(dummyModel.findById).toHaveBeenCalledWith(createdId, undefined);
expect(result).toEqualPayload({
dummy: 'dummy test 5',
});
@ -82,9 +82,12 @@ describe('BaseRepository', () => {
jest.spyOn(dummyModel, 'findOne');
const result = await dummyRepository.findOne({ dummy: 'dummy test 5' });
expect(dummyModel.findOne).toHaveBeenCalledWith({
dummy: 'dummy test 5',
});
expect(dummyModel.findOne).toHaveBeenCalledWith(
{
dummy: 'dummy test 5',
},
undefined,
);
expect(result).toEqualPayload({
dummy: 'dummy test 5',
});

View File

@ -58,6 +58,7 @@ describe('BaseService', () => {
expect(dummyRepository.findOne).toHaveBeenCalledWith(
createdId,
undefined,
undefined,
);
expect(result).toEqualPayload(createdPayload);
});
@ -69,6 +70,7 @@ describe('BaseService', () => {
expect(dummyRepository.findOne).toHaveBeenCalledWith(
createdPayload,
undefined,
undefined,
);
expect(result).toEqualPayload(createdPayload);
});