diff --git a/src/modules/database/src/adapters/secondaries/prisma-repository.abstract.ts b/src/modules/database/src/adapters/secondaries/prisma-repository.abstract.ts index b8f170d..0f655c1 100644 --- a/src/modules/database/src/adapters/secondaries/prisma-repository.abstract.ts +++ b/src/modules/database/src/adapters/secondaries/prisma-repository.abstract.ts @@ -1,4 +1,4 @@ -import { ConsoleLogger, Injectable } from '@nestjs/common'; +import { Injectable } from '@nestjs/common'; import { PrismaClientKnownRequestError } from '@prisma/client/runtime'; import { DatabaseException } from '../../exceptions/DatabaseException'; import { ICollection } from '../../interfaces/collection.interface'; @@ -11,7 +11,6 @@ import { PrismaService } from './prisma-service'; @Injectable() export abstract class PrismaRepository implements IRepository { protected _model: string; - protected _logger: ConsoleLogger = new ConsoleLogger(PrismaRepository.name); constructor(protected readonly _prisma: PrismaService) {} @@ -38,7 +37,7 @@ export abstract class PrismaRepository implements IRepository { }); } - async findOneByUuid(uuid: string, include?: any): Promise { + async findOneByUuid(uuid: string): Promise { try { const entity = await this._prisma[this._model].findUnique({ where: { uuid }, @@ -98,7 +97,7 @@ export abstract class PrismaRepository implements IRepository { } } - async update(uuid: string, entity: Partial, include?: any): Promise { + async update(uuid: string, entity: Partial): Promise { try { const updatedEntity = await this._prisma[this._model].update({ where: { uuid }, diff --git a/src/modules/database/tests/unit/prisma-repository.spec.ts b/src/modules/database/tests/unit/prisma-repository.spec.ts index ae4e97c..39b9d0a 100644 --- a/src/modules/database/tests/unit/prisma-repository.spec.ts +++ b/src/modules/database/tests/unit/prisma-repository.spec.ts @@ -5,7 +5,6 @@ import { PrismaRepository } from '../../src/adapters/secondaries/prisma-reposito import { DatabaseException } from '../../src/exceptions/DatabaseException'; class FakeEntity { - id?: number; uuid?: string; name: string; } @@ -16,7 +15,6 @@ const entityName = 'name-'; const createRandomEntity = (): FakeEntity => { const entity: FakeEntity = { - id: entityId, uuid: `${entityUuid}${entityId}`, name: `${entityName}${entityId}`, }; @@ -32,7 +30,6 @@ const fakeEntityToCreate: FakeEntity = { const fakeEntityCreated: FakeEntity = { ...fakeEntityToCreate, - id: 1, uuid: 'some-uuid', }; @@ -51,7 +48,17 @@ class FakePrismaService extends PrismaService { } const mockPrismaService = { + $transaction: jest.fn().mockImplementation(async (data: any) => { + const entities = await data[0]; + if (entities.length == 1) { + return Promise.resolve([[fakeEntityCreated], 1]); + } + + return Promise.resolve([fakeEntities, fakeEntities.length]); + }), fake: { + create: jest.fn().mockResolvedValue(fakeEntityCreated), + findMany: jest.fn().mockImplementation((params?: any) => { if (params?.where?.limit == 1) { return Promise.resolve([fakeEntityCreated]); @@ -59,16 +66,11 @@ const mockPrismaService = { return Promise.resolve(fakeEntities); }), - - create: jest.fn().mockResolvedValue(fakeEntityCreated), + count: jest.fn().mockResolvedValue(fakeEntities.length), findUnique: jest.fn().mockImplementation(async (params?: any) => { let entity; - if (params?.where?.id) { - entity = fakeEntities.find((entity) => entity.id === params?.where?.id); - } - if (params?.where?.uuid) { entity = fakeEntities.find( (entity) => entity.uuid === params?.where?.uuid, @@ -145,20 +147,28 @@ describe('PrismaRepository', () => { describe('findAll', () => { it('should return an array of entities', async () => { jest.spyOn(prisma.fake, 'findMany'); + jest.spyOn(prisma.fake, 'count'); + jest.spyOn(prisma, '$transaction'); const entities = await fakeRepository.findAll(); - expect(entities).toBe(fakeEntities); + expect(entities).toStrictEqual({ + data: fakeEntities, + total: fakeEntities.length, + }); }); it('should return an array containing only one entity', async () => { - jest.spyOn(prisma.fake, 'findMany'); - - const entities = await fakeRepository.findAll({ limit: 1 }); + const entities = await fakeRepository.findAll(1, 10, { limit: 1 }); expect(prisma.fake.findMany).toHaveBeenCalledWith({ + skip: 0, + take: 10, where: { limit: 1 }, }); - expect(entities).toEqual([fakeEntityCreated]); + expect(entities).toEqual({ + data: [fakeEntityCreated], + total: 1, + }); }); }); diff --git a/src/modules/users/tests/unit/delete-user.usecase.spec.ts b/src/modules/users/tests/unit/delete-user.usecase.spec.ts index b88fd9f..a1cd771 100644 --- a/src/modules/users/tests/unit/delete-user.usecase.spec.ts +++ b/src/modules/users/tests/unit/delete-user.usecase.spec.ts @@ -3,7 +3,7 @@ import { UsersRepository } from '../../adapters/secondaries/users.repository'; import { DeleteUserCommand } from '../../commands/delete-user.command'; import { DeleteUserUseCase } from '../../domain/usecases/delete-user.usecase'; -const usersMock = [ +const mockUsers = [ { uuid: 'bb281075-1b98-4456-89d6-c643d3044a91', firstName: 'John', @@ -26,9 +26,9 @@ const usersMock = [ const mockUsersRepository = { delete: jest.fn().mockImplementation((uuid: string) => { - usersMock.forEach((user, index) => { + mockUsers.forEach((user, index) => { if (user.uuid === uuid) { - usersMock.splice(index, 1); + mockUsers.splice(index, 1); return Promise.resolve(); } }); @@ -58,11 +58,11 @@ describe('DeleteUserUseCase', () => { describe('execute', () => { it('should delete an User', async () => { - const savedUuid = usersMock[0].uuid; + const savedUuid = mockUsers[0].uuid; const deleteUserCommand = new DeleteUserCommand(savedUuid); await deleteUserUseCase.execute(deleteUserCommand); - const deletedUser = usersMock.find((user) => user.uuid === savedUuid); + const deletedUser = mockUsers.find((user) => user.uuid === savedUuid); expect(deletedUser).toBeUndefined(); }); }); diff --git a/src/modules/users/tests/unit/find-all-users.usecase.spec.ts b/src/modules/users/tests/unit/find-all-users.usecase.spec.ts new file mode 100644 index 0000000..16a9c22 --- /dev/null +++ b/src/modules/users/tests/unit/find-all-users.usecase.spec.ts @@ -0,0 +1,62 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { UsersRepository } from '../../adapters/secondaries/users.repository'; +import { FindAllUsersUseCase } from '../../domain/usecases/find-all-users.usecase'; +import { FindAllUsersQuery } from '../../queries/find-all-users.query'; + +const mockUsers = [ + { + uuid: 'bb281075-1b98-4456-89d6-c643d3044a91', + firstName: 'John', + lastName: 'Doe', + email: 'john.doe@email.com', + }, + { + uuid: 'bb281075-1b98-4456-89d6-c643d3044a92', + firstName: 'Jane', + lastName: 'Doe', + email: 'jane.doe@email.com', + }, + { + uuid: 'bb281075-1b98-4456-89d6-c643d3044a93', + firstName: 'Jimmy', + lastName: 'Doe', + email: 'jimmy.doe@email.com', + }, +]; + +const mockUsersRepository = { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + findAll: jest.fn().mockImplementation((query?: FindAllUsersQuery) => { + return Promise.resolve(mockUsers); + }), +}; + +describe('FindAllUsersUseCase', () => { + let findAllUsersUseCase: FindAllUsersUseCase; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + { + provide: UsersRepository, + useValue: mockUsersRepository, + }, + FindAllUsersUseCase, + ], + }).compile(); + + findAllUsersUseCase = module.get(FindAllUsersUseCase); + }); + + it('should be defined', () => { + expect(findAllUsersUseCase).toBeDefined(); + }); + + describe('execute', () => { + it('should return an array filled with users', async () => { + const users = await findAllUsersUseCase.execute(new FindAllUsersQuery()); + + expect(users).toBe(mockUsers); + }); + }); +});