refactor: extract mocking in permissions.service.spec.ts

Signed-off-by: Yannick Bungers <git@innay.de>
This commit is contained in:
Yannick Bungers 2023-04-23 22:25:58 +02:00 committed by Yannick Bungers
parent d73bbcaeff
commit ed3c688e8b

View file

@ -171,6 +171,23 @@ describe('PermissionsService', () => {
eventEmitter = module.get<EventEmitter2>(EventEmitter2); eventEmitter = module.get<EventEmitter2>(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(() => { beforeEach(() => {
mockNoteRepo(noteRepo); mockNoteRepo(noteRepo);
eventEmitterEmitSpy = mockedEventEmitter(eventEmitter); eventEmitterEmitSpy = mockedEventEmitter(eventEmitter);
@ -774,17 +791,13 @@ describe('PermissionsService', () => {
describe('checkPermissionOnNote', () => { describe('checkPermissionOnNote', () => {
describe('accepts', () => { describe('accepts', () => {
it('with mayRead', async () => { it('with mayRead', async () => {
jest.spyOn(service, 'mayRead').mockImplementationOnce(async () => { mockMayReadTrue();
return true;
});
expect( expect(
await service.checkPermissionOnNote(Permission.READ, user1, notes[0]), await service.checkPermissionOnNote(Permission.READ, user1, notes[0]),
).toBeTruthy(); ).toBeTruthy();
}); });
it('with mayWrite', async () => { it('with mayWrite', async () => {
jest.spyOn(service, 'mayWrite').mockImplementationOnce(async () => { mockMayWriteTrue();
return true;
});
expect( expect(
await service.checkPermissionOnNote( await service.checkPermissionOnNote(
Permission.WRITE, Permission.WRITE,
@ -794,9 +807,7 @@ describe('PermissionsService', () => {
).toBeTruthy(); ).toBeTruthy();
}); });
it('with isOwner', async () => { it('with isOwner', async () => {
jest.spyOn(service, 'isOwner').mockImplementationOnce(async () => { mockIsOwner(true);
return true;
});
expect( expect(
await service.checkPermissionOnNote( await service.checkPermissionOnNote(
Permission.OWNER, Permission.OWNER,
@ -808,15 +819,9 @@ describe('PermissionsService', () => {
}); });
describe('denies', () => { describe('denies', () => {
it('with no permission', async () => { it('with no permission', async () => {
jest.spyOn(service, 'mayRead').mockImplementationOnce(async () => { mockMayReadTrue();
return false; mockMayWriteTrue();
}); mockIsOwner(false);
jest.spyOn(service, 'mayWrite').mockImplementationOnce(async () => {
return false;
});
jest.spyOn(service, 'isOwner').mockImplementationOnce(async () => {
return false;
});
expect( expect(
await service.checkPermissionOnNote( await service.checkPermissionOnNote(
Permission.OWNER, Permission.OWNER,