2023-05-15 20:30:53 +03:00
|
|
|
import { BadRequestException } from '@nestjs/common';
|
2024-03-21 12:59:49 +01:00
|
|
|
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';
|
2024-10-02 10:54:35 -04:00
|
|
|
import { newTestService } from 'test/utils';
|
2024-04-16 10:44:45 -04:00
|
|
|
import { Mocked } from 'vitest';
|
2023-05-15 20:30:53 +03:00
|
|
|
|
|
|
|
|
describe(PartnerService.name, () => {
|
|
|
|
|
let sut: PartnerService;
|
2024-04-16 10:44:45 -04:00
|
|
|
let partnerMock: Mocked<IPartnerRepository>;
|
2023-05-15 20:30:53 +03:00
|
|
|
|
2024-03-05 23:23:06 +01:00
|
|
|
beforeEach(() => {
|
2024-10-02 10:54:35 -04:00
|
|
|
({ sut, partnerMock } = newTestService(PartnerService));
|
2023-05-15 20:30:53 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should work', () => {
|
|
|
|
|
expect(sut).toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
|
2024-07-08 16:41:39 -04:00
|
|
|
describe('search', () => {
|
2023-05-15 20:30:53 +03:00
|
|
|
it("should return a list of partners with whom I've shared my library", async () => {
|
|
|
|
|
partnerMock.getAll.mockResolvedValue([partnerStub.adminToUser1, partnerStub.user1ToAdmin1]);
|
2024-07-08 16:41:39 -04:00
|
|
|
await expect(sut.search(authStub.user1, { direction: PartnerDirection.SharedBy })).resolves.toBeDefined();
|
2023-12-09 23:34:12 -05:00
|
|
|
expect(partnerMock.getAll).toHaveBeenCalledWith(authStub.user1.user.id);
|
2023-05-15 20:30:53 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return a list of partners who have shared their libraries with me', async () => {
|
|
|
|
|
partnerMock.getAll.mockResolvedValue([partnerStub.adminToUser1, partnerStub.user1ToAdmin1]);
|
2024-07-08 16:41:39 -04:00
|
|
|
await expect(sut.search(authStub.user1, { direction: PartnerDirection.SharedWith })).resolves.toBeDefined();
|
2023-12-09 23:34:12 -05:00
|
|
|
expect(partnerMock.getAll).toHaveBeenCalledWith(authStub.user1.user.id);
|
2023-05-15 20:30:53 +03:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('create', () => {
|
|
|
|
|
it('should create a new partner', async () => {
|
|
|
|
|
partnerMock.get.mockResolvedValue(null);
|
|
|
|
|
partnerMock.create.mockResolvedValue(partnerStub.adminToUser1);
|
|
|
|
|
|
2024-05-26 18:15:52 -04:00
|
|
|
await expect(sut.create(authStub.admin, authStub.user1.user.id)).resolves.toBeDefined();
|
2023-05-15 20:30:53 +03:00
|
|
|
|
|
|
|
|
expect(partnerMock.create).toHaveBeenCalledWith({
|
2023-12-09 23:34:12 -05:00
|
|
|
sharedById: authStub.admin.user.id,
|
|
|
|
|
sharedWithId: authStub.user1.user.id,
|
2023-05-15 20:30:53 +03:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should throw an error when the partner already exists', async () => {
|
|
|
|
|
partnerMock.get.mockResolvedValue(partnerStub.adminToUser1);
|
|
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
await expect(sut.create(authStub.admin, authStub.user1.user.id)).rejects.toBeInstanceOf(BadRequestException);
|
2023-05-15 20:30:53 +03:00
|
|
|
|
|
|
|
|
expect(partnerMock.create).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('remove', () => {
|
|
|
|
|
it('should remove a partner', async () => {
|
|
|
|
|
partnerMock.get.mockResolvedValue(partnerStub.adminToUser1);
|
|
|
|
|
|
2023-12-09 23:34:12 -05:00
|
|
|
await sut.remove(authStub.admin, authStub.user1.user.id);
|
2023-05-15 20:30:53 +03: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-09 23:34:12 -05:00
|
|
|
await expect(sut.remove(authStub.admin, authStub.user1.user.id)).rejects.toBeInstanceOf(BadRequestException);
|
2023-05-15 20:30:53 +03:00
|
|
|
|
|
|
|
|
expect(partnerMock.remove).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|