fix: unit tests

This commit is contained in:
Mohamed Marrouchi
2024-09-21 19:43:04 +01:00
parent 7bc5270551
commit 93bb9ee04f
21 changed files with 87 additions and 72 deletions

View File

@@ -70,7 +70,7 @@ describe('ModelService', () => {
jest.spyOn(modelRepository, 'findAndPopulate');
const models = await modelRepository.findAll();
const permissions = await permissionRepository.findAll();
const result = await modelService.findAndPopulate({}, ['permissions']);
const result = await modelService.findAndPopulate({});
const modelsWithPermissions = models.reduce((acc, currModel) => {
acc.push({
...currModel,
@@ -80,9 +80,10 @@ describe('ModelService', () => {
});
return acc;
}, []);
expect(modelRepository.findAndPopulate).toHaveBeenCalledWith({}, [
'permissions',
]);
expect(modelRepository.findAndPopulate).toHaveBeenCalledWith(
{},
undefined,
);
expect(result).toEqualPayload(modelsWithPermissions);
});
});

View File

@@ -99,10 +99,8 @@ describe('UserService', () => {
describe('findOneAndPopulate', () => {
it('should find one user and populate its role', async () => {
jest.spyOn(userRepository, 'findOneAndPopulate');
const result = await userService.findOneAndPopulate(user.id, ['roles']);
expect(userRepository.findOneAndPopulate).toHaveBeenCalledWith(user.id, [
'roles',
]);
const result = await userService.findOneAndPopulate(user.id);
expect(userRepository.findOneAndPopulate).toHaveBeenCalledWith(user.id);
expect(result).toEqualPayload(
{
...userFixtures.find(({ username }) => username === 'admin'),
@@ -118,9 +116,7 @@ describe('UserService', () => {
const pageQuery = getPageQuery<User>({ sort: ['_id', 'asc'] });
jest.spyOn(userRepository, 'findPageAndPopulate');
const allUsers = await userRepository.findAll();
const result = await userService.findPageAndPopulate({}, pageQuery, [
'roles',
]);
const result = await userService.findPageAndPopulate({}, pageQuery);
const usersWithRoles = allUsers.reduce((acc, currUser) => {
acc.push({
...currUser,
@@ -132,7 +128,6 @@ describe('UserService', () => {
expect(userRepository.findPageAndPopulate).toHaveBeenCalledWith(
{},
pageQuery,
['roles'],
);
expect(result).toEqualPayload(usersWithRoles);
});