From ed3c688e8b410c473f99eaccbfa99d39c6087477 Mon Sep 17 00:00:00 2001 From: Yannick Bungers Date: Sun, 23 Apr 2023 22:25:58 +0200 Subject: [PATCH] refactor: extract mocking in permissions.service.spec.ts Signed-off-by: Yannick Bungers --- .../permissions/permissions.service.spec.ts | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/backend/src/permissions/permissions.service.spec.ts b/backend/src/permissions/permissions.service.spec.ts index 41e08a7eb..fc233e3ce 100644 --- a/backend/src/permissions/permissions.service.spec.ts +++ b/backend/src/permissions/permissions.service.spec.ts @@ -171,6 +171,23 @@ describe('PermissionsService', () => { eventEmitter = module.get(EventEmitter2); }); + function mockMayReadTrue(): void { + jest.spyOn(service, 'mayRead').mockImplementationOnce(async () => { + return true; + }); + } + + function mockMayWriteTrue(): void { + jest.spyOn(service, 'mayWrite').mockImplementationOnce(async () => { + return true; + }); + } + + function mockIsOwner(isOwner: boolean): void { + jest.spyOn(service, 'isOwner').mockImplementationOnce(async () => { + return isOwner; + }); + } beforeEach(() => { mockNoteRepo(noteRepo); eventEmitterEmitSpy = mockedEventEmitter(eventEmitter); @@ -774,17 +791,13 @@ describe('PermissionsService', () => { describe('checkPermissionOnNote', () => { describe('accepts', () => { it('with mayRead', async () => { - jest.spyOn(service, 'mayRead').mockImplementationOnce(async () => { - return true; - }); + mockMayReadTrue(); expect( await service.checkPermissionOnNote(Permission.READ, user1, notes[0]), ).toBeTruthy(); }); it('with mayWrite', async () => { - jest.spyOn(service, 'mayWrite').mockImplementationOnce(async () => { - return true; - }); + mockMayWriteTrue(); expect( await service.checkPermissionOnNote( Permission.WRITE, @@ -794,9 +807,7 @@ describe('PermissionsService', () => { ).toBeTruthy(); }); it('with isOwner', async () => { - jest.spyOn(service, 'isOwner').mockImplementationOnce(async () => { - return true; - }); + mockIsOwner(true); expect( await service.checkPermissionOnNote( Permission.OWNER, @@ -808,15 +819,9 @@ describe('PermissionsService', () => { }); describe('denies', () => { it('with no permission', async () => { - jest.spyOn(service, 'mayRead').mockImplementationOnce(async () => { - return false; - }); - jest.spyOn(service, 'mayWrite').mockImplementationOnce(async () => { - return false; - }); - jest.spyOn(service, 'isOwner').mockImplementationOnce(async () => { - return false; - }); + mockMayReadTrue(); + mockMayWriteTrue(); + mockIsOwner(false); expect( await service.checkPermissionOnNote( Permission.OWNER,