From f1ac705204fecbced5a38922a571eb84b0c44273 Mon Sep 17 00:00:00 2001 From: Thom Werring Date: Tue, 26 Mar 2024 21:45:07 +0100 Subject: [PATCH] Added final tests --- src/shipments/shipments.service.ts | 1 - tests/shipments/shipments.service.spec.ts | 43 ++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/shipments/shipments.service.ts b/src/shipments/shipments.service.ts index e56535c..970b668 100644 --- a/src/shipments/shipments.service.ts +++ b/src/shipments/shipments.service.ts @@ -10,7 +10,6 @@ export class ShipmentsService { private readonly shipmentRepository: Repository, ) {} - // @todo add pagination getAll() { return this.shipmentRepository.find({ relations: ['liveTracking', 'containers'], diff --git a/tests/shipments/shipments.service.spec.ts b/tests/shipments/shipments.service.spec.ts index 8492c4d..cbd835e 100644 --- a/tests/shipments/shipments.service.spec.ts +++ b/tests/shipments/shipments.service.spec.ts @@ -1,18 +1,59 @@ import { Test, TestingModule } from '@nestjs/testing'; import { ShipmentsService } from '@/shipments/shipments.service'; +import { getRepositoryToken } from '@nestjs/typeorm'; +import { ShipmentEntity } from '@/entities/shipment.entity'; +import { Repository } from 'typeorm'; +import { mockShipment } from '@tests/data/tracking.mock'; describe('ShipmentsService', () => { + const shipmentEntityToken = getRepositoryToken(ShipmentEntity); let service: ShipmentsService; + let repository: Repository; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ShipmentsService], - }).compile(); + }) + .useMocker((token) => { + switch (token) { + case shipmentEntityToken: + return { + find: jest.fn().mockResolvedValue([]), + save: jest + .fn() + .mockImplementation((shipmentEntity: ShipmentEntity) => + Promise.resolve(shipmentEntity), + ), + }; + } + }) + .compile(); service = module.get(ShipmentsService); + repository = module.get>(shipmentEntityToken); }); it('should be defined', () => { expect(service).toBeDefined(); }); + + it('should have a getAll method', () => { + expect(service.getAll).toBeDefined(); + }); + + it('should fetch data with relations liveTracking and containers', async () => { + await expect(service.getAll()).resolves.toEqual([]); + expect(repository.find).toHaveBeenCalledWith({ + relations: ['liveTracking', 'containers'], + }); + }); + + it('should have an update method', () => { + expect(service.update).toBeDefined(); + }); + + it('should fetch data with relations liveTracking and containers', async () => { + await expect(service.update(mockShipment)).resolves.toEqual(mockShipment); + expect(repository.save).toHaveBeenCalledWith(mockShipment); + }); });