Added final tests

This commit is contained in:
Thom Werring 2024-03-26 21:45:07 +01:00
parent 73db243099
commit f1ac705204
2 changed files with 42 additions and 2 deletions

View File

@ -10,7 +10,6 @@ export class ShipmentsService {
private readonly shipmentRepository: Repository<ShipmentEntity>, private readonly shipmentRepository: Repository<ShipmentEntity>,
) {} ) {}
// @todo add pagination
getAll() { getAll() {
return this.shipmentRepository.find({ return this.shipmentRepository.find({
relations: ['liveTracking', 'containers'], relations: ['liveTracking', 'containers'],

View File

@ -1,18 +1,59 @@
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { ShipmentsService } from '@/shipments/shipments.service'; 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', () => { describe('ShipmentsService', () => {
const shipmentEntityToken = getRepositoryToken(ShipmentEntity);
let service: ShipmentsService; let service: ShipmentsService;
let repository: Repository<ShipmentEntity>;
beforeEach(async () => { beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({ const module: TestingModule = await Test.createTestingModule({
providers: [ShipmentsService], 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>(ShipmentsService); service = module.get<ShipmentsService>(ShipmentsService);
repository = module.get<Repository<ShipmentEntity>>(shipmentEntityToken);
}); });
it('should be defined', () => { it('should be defined', () => {
expect(service).toBeDefined(); 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);
});
}); });