2023-05-15 19:30:53 +02:00
|
|
|
import { BadRequestException } from '@nestjs/common';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { IAccessRepository } from 'src/interfaces/access.interface';
|
|
|
|
import { IPartnerRepository, PartnerDirection } from 'src/interfaces/partner.interface';
|
2024-03-21 00:07:30 +01:00
|
|
|
import { PartnerService } from 'src/services/partner.service';
|
2024-03-20 19:32:04 +01:00
|
|
|
import { authStub } from 'test/fixtures/auth.stub';
|
|
|
|
import { partnerStub } from 'test/fixtures/partner.stub';
|
|
|
|
import { newPartnerRepositoryMock } from 'test/repositories/partner.repository.mock';
|
2024-04-16 16:44:45 +02:00
|
|
|
import { Mocked } from 'vitest';
|
2023-05-15 19:30:53 +02:00
|
|
|
|
|
|
|
describe(PartnerService.name, () => {
|
|
|
|
let sut: PartnerService;
|
2024-04-16 16:44:45 +02:00
|
|
|
let partnerMock: Mocked<IPartnerRepository>;
|
|
|
|
let accessMock: Mocked<IAccessRepository>;
|
2023-05-15 19:30:53 +02:00
|
|
|
|
2024-03-05 23:23:06 +01:00
|
|
|
beforeEach(() => {
|
2023-05-15 19:30:53 +02:00
|
|
|
partnerMock = newPartnerRepositoryMock();
|
2023-11-11 22:06:19 +01:00
|
|
|
sut = new PartnerService(partnerMock, accessMock);
|
2023-05-15 19:30:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should work', () => {
|
|
|
|
expect(sut).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getAll', () => {
|
|
|
|
it("should return a list of partners with whom I've shared my library", async () => {
|
|
|
|
partnerMock.getAll.mockResolvedValue([partnerStub.adminToUser1, partnerStub.user1ToAdmin1]);
|
2024-05-27 00:15:52 +02:00
|
|
|
await expect(sut.getAll(authStub.user1, PartnerDirection.SharedBy)).resolves.toBeDefined();
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(partnerMock.getAll).toHaveBeenCalledWith(authStub.user1.user.id);
|
2023-05-15 19:30:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return a list of partners who have shared their libraries with me', async () => {
|
|
|
|
partnerMock.getAll.mockResolvedValue([partnerStub.adminToUser1, partnerStub.user1ToAdmin1]);
|
2024-05-27 00:15:52 +02:00
|
|
|
await expect(sut.getAll(authStub.user1, PartnerDirection.SharedWith)).resolves.toBeDefined();
|
2023-12-10 05:34:12 +01:00
|
|
|
expect(partnerMock.getAll).toHaveBeenCalledWith(authStub.user1.user.id);
|
2023-05-15 19:30:53 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('create', () => {
|
|
|
|
it('should create a new partner', async () => {
|
|
|
|
partnerMock.get.mockResolvedValue(null);
|
|
|
|
partnerMock.create.mockResolvedValue(partnerStub.adminToUser1);
|
|
|
|
|
2024-05-27 00:15:52 +02:00
|
|
|
await expect(sut.create(authStub.admin, authStub.user1.user.id)).resolves.toBeDefined();
|
2023-05-15 19:30:53 +02:00
|
|
|
|
|
|
|
expect(partnerMock.create).toHaveBeenCalledWith({
|
2023-12-10 05:34:12 +01:00
|
|
|
sharedById: authStub.admin.user.id,
|
|
|
|
sharedWithId: authStub.user1.user.id,
|
2023-05-15 19:30:53 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error when the partner already exists', async () => {
|
|
|
|
partnerMock.get.mockResolvedValue(partnerStub.adminToUser1);
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
await expect(sut.create(authStub.admin, authStub.user1.user.id)).rejects.toBeInstanceOf(BadRequestException);
|
2023-05-15 19:30:53 +02:00
|
|
|
|
|
|
|
expect(partnerMock.create).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('remove', () => {
|
|
|
|
it('should remove a partner', async () => {
|
|
|
|
partnerMock.get.mockResolvedValue(partnerStub.adminToUser1);
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
await sut.remove(authStub.admin, authStub.user1.user.id);
|
2023-05-15 19:30:53 +02:00
|
|
|
|
|
|
|
expect(partnerMock.remove).toHaveBeenCalledWith(partnerStub.adminToUser1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error when the partner does not exist', async () => {
|
|
|
|
partnerMock.get.mockResolvedValue(null);
|
|
|
|
|
2023-12-10 05:34:12 +01:00
|
|
|
await expect(sut.remove(authStub.admin, authStub.user1.user.id)).rejects.toBeInstanceOf(BadRequestException);
|
2023-05-15 19:30:53 +02:00
|
|
|
|
|
|
|
expect(partnerMock.remove).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|