import { Injectable } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { PrismaService } from '../../src/adapters/secondaries/prisma-service'; import { TerritoryRepository } from '../../src/domain/territory-repository'; import { Territory } from '../../../territory/domain/entities/territory'; import { Point } from '../../../territory/domain/entities/point'; import { DatabaseException } from '../../src/exceptions/database.exception'; const mockTerritories: Array = [ { uuid: 'bb281075-1b98-4456-89d6-c643d3044a91', name: 'Nancy', shape: 'nancy-shape', }, { uuid: 'bb281075-1b98-4456-89d6-c643d3044a92', name: 'Meurthe-et-Moselle', shape: 'nancy-meurthe-et-moselle', }, ]; const territoryCreated: Territory = { uuid: 'bb281075-1b98-4456-89d6-c643d3044a91', name: 'Nancy', shape: 'nancy-shape', }; const territoryUpdated: Territory = { uuid: 'bb281075-1b98-4456-89d6-c643d3044a91', name: 'New Nancy', shape: 'new-nancy-shape', }; @Injectable() class TerritoriesRepository extends TerritoryRepository { protected _model = 'territory'; } const mockPrismaService = { // eslint-disable-next-line @typescript-eslint/no-unused-vars $queryRawUnsafe: jest.fn().mockImplementation((query?: string) => { return Promise.resolve(mockTerritories); }), $executeRawUnsafe: jest .fn() .mockResolvedValueOnce(1) .mockResolvedValueOnce(0) .mockResolvedValueOnce(1) .mockResolvedValueOnce(0), territory: { findFirst: jest .fn() .mockResolvedValueOnce(territoryCreated) .mockResolvedValueOnce(territoryUpdated), }, }; describe('TerritoryRepository', () => { let repository: TerritoriesRepository; beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ TerritoriesRepository, { provide: PrismaService, useValue: mockPrismaService, }, ], }).compile(); repository = module.get(TerritoriesRepository); }); it('should be defined', () => { expect(repository).toBeDefined(); }); describe('findForPoint', () => { it('should return an array of entities', async () => { const entities = await repository.findForPoint(new Point(6.1, 48.2)); expect(entities).toStrictEqual({ data: mockTerritories, total: mockTerritories.length, }); }); }); describe('findForPoints', () => { it('should return an array of entities', async () => { const entities = await repository.findForPoints([ new Point(6.1, 48.2), new Point(6.2, 48.3), ]); expect(entities).toStrictEqual({ data: mockTerritories, total: mockTerritories.length, }); }); }); describe('createTerritory', () => { it('should create a new territory', async () => { const territory = await repository.createTerritory({ name: territoryCreated.name, shape: territoryCreated.shape, }); expect(territory.uuid).toBe(territoryCreated.uuid); }); it('should throw a DatabaseException for client error', async () => { await expect( repository.createTerritory({ name: territoryCreated.name, shape: territoryCreated.shape, }), ).rejects.toBeInstanceOf(DatabaseException); }); }); describe('updateTerritory', () => { it('should update a territory', async () => { const updatedTerritory = await repository.updateTerritory( 'bb281075-1b98-4456-89d6-c643d3044a91', { name: 'newNancy', shape: 'new-nancy-shape', }, ); expect(updatedTerritory.name).toBe(territoryUpdated.name); }); it('should throw a DatabaseException for client error', async () => { await expect( repository.updateTerritory('bb281075-1b98-4456-89d6-c643d3044a91', { name: 'newNancy', shape: 'new-nancy-shape', }), ).rejects.toBeInstanceOf(DatabaseException); }); }); });