|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { getRepositoryToken } from '@nestjs/typeorm'; |
| 3 | +import { Repository } from 'typeorm'; |
| 4 | +import { AssetTransfersService } from './asset-transfers.service'; |
| 5 | +import { AssetTransfer } from './entities/asset-transfer.entity'; |
| 6 | +import { InventoryItem } from '../../inventory-items/entities/inventory-item.entity'; |
| 7 | +import { InitiateTransferDto } from './dto/initiate-transfer.dto'; |
| 8 | + |
| 9 | +describe('AssetTransfersService', () => { |
| 10 | + let service: AssetTransfersService; |
| 11 | + let transferRepo: Repository<AssetTransfer>; |
| 12 | + let inventoryRepo: Repository<InventoryItem>; |
| 13 | + |
| 14 | + const mockTransferRepo = { |
| 15 | + create: jest.fn(), |
| 16 | + save: jest.fn(), |
| 17 | + }; |
| 18 | + |
| 19 | + const mockInventoryRepo = { |
| 20 | + findOne: jest.fn(), |
| 21 | + save: jest.fn(), |
| 22 | + }; |
| 23 | + |
| 24 | + beforeEach(async () => { |
| 25 | + const module: TestingModule = await Test.createTestingModule({ |
| 26 | + providers: [ |
| 27 | + AssetTransfersService, |
| 28 | + { provide: getRepositoryToken(AssetTransfer), useValue: mockTransferRepo }, |
| 29 | + { provide: getRepositoryToken(InventoryItem), useValue: mockInventoryRepo }, |
| 30 | + ], |
| 31 | + }).compile(); |
| 32 | + |
| 33 | + service = module.get<AssetTransfersService>(AssetTransfersService); |
| 34 | + transferRepo = module.get<Repository<AssetTransfer>>(getRepositoryToken(AssetTransfer)); |
| 35 | + inventoryRepo = module.get<Repository<InventoryItem>>(getRepositoryToken(InventoryItem)); |
| 36 | + jest.clearAllMocks(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should be defined', () => { |
| 40 | + expect(service).toBeDefined(); |
| 41 | + expect(transferRepo).toBeDefined(); |
| 42 | + expect(inventoryRepo).toBeDefined(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('updates asset currentDepartmentId and logs transfer with previous fromDepartmentId', async () => { |
| 46 | + const assetId = 'a-uuid'; |
| 47 | + const dto: InitiateTransferDto = { |
| 48 | + assetId, |
| 49 | + toDepartmentId: 20, |
| 50 | + initiatedBy: 'john.doe', |
| 51 | + reason: 'Relocation', |
| 52 | + } as InitiateTransferDto; |
| 53 | + |
| 54 | + const asset: Partial<InventoryItem> = { |
| 55 | + id: assetId, |
| 56 | + currentDepartmentId: 10, |
| 57 | + }; |
| 58 | + |
| 59 | + const createdTransfer: Partial<AssetTransfer> = { |
| 60 | + id: 1, |
| 61 | + assetId, |
| 62 | + fromDepartmentId: 10, |
| 63 | + toDepartmentId: 20, |
| 64 | + transferDate: new Date(), |
| 65 | + initiatedBy: dto.initiatedBy, |
| 66 | + reason: dto.reason, |
| 67 | + }; |
| 68 | + |
| 69 | + mockInventoryRepo.findOne.mockResolvedValue(asset); |
| 70 | + mockTransferRepo.create.mockImplementation((data) => ({ id: 1, ...data })); |
| 71 | + mockTransferRepo.save.mockImplementation((data) => data); |
| 72 | + mockInventoryRepo.save.mockImplementation((data) => data); |
| 73 | + |
| 74 | + const result = await service.initiateTransfer(dto); |
| 75 | + |
| 76 | + expect(mockInventoryRepo.findOne).toHaveBeenCalledWith({ where: { id: assetId } }); |
| 77 | + expect(mockInventoryRepo.save).toHaveBeenCalledWith({ ...asset, currentDepartmentId: 20 }); |
| 78 | + expect(mockTransferRepo.create).toHaveBeenCalledWith( |
| 79 | + expect.objectContaining({ |
| 80 | + assetId, |
| 81 | + fromDepartmentId: 10, |
| 82 | + toDepartmentId: 20, |
| 83 | + initiatedBy: 'john.doe', |
| 84 | + reason: 'Relocation', |
| 85 | + }), |
| 86 | + ); |
| 87 | + expect(result).toEqual(expect.objectContaining({ id: 1, assetId })); |
| 88 | + }); |
| 89 | + |
| 90 | + it('prefers provided fromDepartmentId when passed in dto', async () => { |
| 91 | + const assetId = 'a-uuid'; |
| 92 | + const dto: InitiateTransferDto = { |
| 93 | + assetId, |
| 94 | + fromDepartmentId: 5, |
| 95 | + toDepartmentId: 7, |
| 96 | + initiatedBy: 'ops', |
| 97 | + } as InitiateTransferDto; |
| 98 | + |
| 99 | + mockInventoryRepo.findOne.mockResolvedValue({ id: assetId, currentDepartmentId: 10 }); |
| 100 | + mockTransferRepo.create.mockImplementation((data) => ({ id: 2, ...data })); |
| 101 | + mockTransferRepo.save.mockImplementation((data) => data); |
| 102 | + mockInventoryRepo.save.mockImplementation((data) => data); |
| 103 | + |
| 104 | + const result = await service.initiateTransfer(dto); |
| 105 | + |
| 106 | + expect(mockTransferRepo.create).toHaveBeenCalledWith( |
| 107 | + expect.objectContaining({ fromDepartmentId: 5, toDepartmentId: 7 }), |
| 108 | + ); |
| 109 | + expect(result).toEqual(expect.objectContaining({ id: 2 })); |
| 110 | + }); |
| 111 | + |
| 112 | + it('throws when asset not found', async () => { |
| 113 | + const dto: InitiateTransferDto = { |
| 114 | + assetId: 'missing', |
| 115 | + toDepartmentId: 1, |
| 116 | + initiatedBy: 'ops', |
| 117 | + } as InitiateTransferDto; |
| 118 | + mockInventoryRepo.findOne.mockResolvedValue(undefined); |
| 119 | + await expect(service.initiateTransfer(dto)).rejects.toThrow('Asset missing not found'); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | + |
0 commit comments