60 lines
1.9 KiB
TypeScript
60 lines
1.9 KiB
TypeScript
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<ShipmentEntity>;
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [ShipmentsService],
|
|
})
|
|
.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);
|
|
repository = module.get<Repository<ShipmentEntity>>(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);
|
|
});
|
|
});
|