|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { ClaimsController } from './claims.controller'; |
| 3 | +import { ClaimsService } from './claims.service'; |
| 4 | +import { PrismaService } from '../prisma/prisma.service'; |
| 5 | +import { CryptoService } from '../core/crypto/crypto.service'; |
| 6 | +import { LoggedGuard } from '../core/auth/logged.guard'; |
| 7 | +import { CreateClaimDto } from './dto/create-claim.dto'; |
| 8 | + |
| 9 | +class Claim { |
| 10 | + id: string; |
| 11 | + policyId: string; |
| 12 | + operatorId: string; |
| 13 | + status: string; |
| 14 | + type: string; |
| 15 | + phoneNumber: string; |
| 16 | + address: string; |
| 17 | + claimNumber: number; |
| 18 | + imageUrl: string; |
| 19 | +} |
| 20 | + |
| 21 | +const mockPrismaService = {}; |
| 22 | +const mockClaimsService = { |
| 23 | + createClaim: jest.fn(), |
| 24 | + findByPolicyId: jest.fn(), |
| 25 | +}; |
| 26 | + |
| 27 | +const mockCryptoService = { |
| 28 | + verifyToken: jest.fn().mockResolvedValue(true), |
| 29 | +}; |
| 30 | + |
| 31 | +describe('ClaimsController', () => { |
| 32 | + let controller: ClaimsController; |
| 33 | + let service: ClaimsService; |
| 34 | + |
| 35 | + beforeEach(async () => { |
| 36 | + const module: TestingModule = await Test.createTestingModule({ |
| 37 | + controllers: [ClaimsController], |
| 38 | + providers: [ |
| 39 | + { provide: ClaimsService, useValue: mockClaimsService }, |
| 40 | + { provide: PrismaService, useValue: mockPrismaService }, |
| 41 | + { provide: CryptoService, useValue: mockCryptoService }, |
| 42 | + LoggedGuard, |
| 43 | + ], |
| 44 | + }).compile(); |
| 45 | + |
| 46 | + controller = module.get<ClaimsController>(ClaimsController); |
| 47 | + service = module.get<ClaimsService>(ClaimsService); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should be defined', () => { |
| 51 | + expect(controller).toBeDefined(); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('createClaim', () => { |
| 55 | + it('should create a claim and return the result', async () => { |
| 56 | + const createClaimDto: CreateClaimDto = { |
| 57 | + type: 'crash', |
| 58 | + phoneNumber: '1234567890', |
| 59 | + address: '123 Main St', |
| 60 | + } as any; |
| 61 | + const file = { originalname: 'file.png' } as any; |
| 62 | + const policyId = '1'; |
| 63 | + const result: Claim = { |
| 64 | + id: '1', |
| 65 | + policyId, |
| 66 | + operatorId: 'randomId', |
| 67 | + status: 'pending', |
| 68 | + type: 'crash', |
| 69 | + phoneNumber: '1234567890', |
| 70 | + address: '123 Main St', |
| 71 | + claimNumber: 1, |
| 72 | + imageUrl: 'http://example.com/image.png', |
| 73 | + }; |
| 74 | + |
| 75 | + jest.spyOn(service, 'createClaim').mockResolvedValue(result); |
| 76 | + |
| 77 | + const response = await controller.createClaim( |
| 78 | + policyId, |
| 79 | + createClaimDto, |
| 80 | + file, |
| 81 | + ); |
| 82 | + |
| 83 | + expect(response).toBe(result); |
| 84 | + expect(service.createClaim).toHaveBeenCalledWith( |
| 85 | + policyId, |
| 86 | + createClaimDto, |
| 87 | + file, |
| 88 | + ); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('getClaimsByPolicyId', () => { |
| 93 | + it('should return claims for the given policy ID', async () => { |
| 94 | + const policyId = '1'; |
| 95 | + const claims: Claim[] = [ |
| 96 | + { |
| 97 | + id: '1', |
| 98 | + policyId, |
| 99 | + operatorId: 'randomId', |
| 100 | + status: 'pending', |
| 101 | + type: 'crash', |
| 102 | + phoneNumber: '1234567890', |
| 103 | + address: '123 Main St', |
| 104 | + claimNumber: 1, |
| 105 | + imageUrl: 'http://example.com/image.png', |
| 106 | + }, |
| 107 | + ]; |
| 108 | + |
| 109 | + jest.spyOn(service, 'findByPolicyId').mockResolvedValue(claims); |
| 110 | + |
| 111 | + const response = await controller.getClaimsByPolicyId(policyId); |
| 112 | + |
| 113 | + expect(response).toBe(claims); |
| 114 | + expect(service.findByPolicyId).toHaveBeenCalledWith(policyId); |
| 115 | + }); |
| 116 | + }); |
| 117 | +}); |
0 commit comments