From b314ddb77ec5543b719c780b8d2ff1a8f4af08d3 Mon Sep 17 00:00:00 2001 From: Amit Ranjan Date: Thu, 26 Sep 2024 21:48:05 +0530 Subject: [PATCH] fix: added unit test case for permission deleteone api --- .../permission.repository.spec.ts | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/api/src/user/repositories/permission.repository.spec.ts b/api/src/user/repositories/permission.repository.spec.ts index 112d32d5c..13b7fb18e 100644 --- a/api/src/user/repositories/permission.repository.spec.ts +++ b/api/src/user/repositories/permission.repository.spec.ts @@ -35,6 +35,7 @@ describe('PermissionRepository', () => { let permissionRepository: PermissionRepository; let permissionModel: Model; let permission: Permission; + let permissionToDelete: Permission; beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ @@ -59,6 +60,9 @@ describe('PermissionRepository', () => { permission = await permissionRepository.findOne({ action: Action.CREATE, }); + permissionToDelete = await permissionRepository.findOne({ + action: Action.UPDATE, + }); }); afterAll(async () => { @@ -112,4 +116,36 @@ describe('PermissionRepository', () => { expect(result).toEqualPayload(permissionsWithRolesAndModels); }); }); + + describe('deleteOne', () => { + it('should delete a permission by id', async () => { + jest.spyOn(permissionModel, 'deleteOne'); + const result = await permissionRepository.deleteOne( + permissionToDelete.id, + ); + + expect(permissionModel.deleteOne).toHaveBeenCalledWith({ + _id: permissionToDelete.id, + }); + + expect(result).toEqual({ + acknowledged: true, + deletedCount: 1, + }); + + const permissions = await permissionRepository.find({ + role: permissionToDelete.id, + }); + expect(permissions.length).toEqual(0); + }); + + it('should fail to delete a permission that does not exist', async () => { + expect( + await permissionRepository.deleteOne(permissionToDelete.id), + ).toEqual({ + acknowledged: true, + deletedCount: 0, + }); + }); + }); });