|
| 1 | +import { Test } from "@nestjs/testing"; |
| 2 | +import { TrackingController } from "./tracking.controller"; |
| 3 | +import { TrackingService } from "./tracking.service"; |
| 4 | +import { BadRequestException } from "@nestjs/common"; |
| 5 | +import { mockResponse, mockTrackingService } from "../mocks/TrackingMock"; |
| 6 | +import { VerificationRequestStatus } from "../verification-request/dto/types"; |
| 7 | +import { AbilitiesGuard } from "../auth/ability/abilities.guard"; |
| 8 | + |
| 9 | +describe("TrackingController (Unit)", () => { |
| 10 | + let controller: TrackingController; |
| 11 | + let trackingService: any; |
| 12 | + |
| 13 | + beforeEach(async () => { |
| 14 | + const testingModule = await Test.createTestingModule({ |
| 15 | + controllers: [TrackingController], |
| 16 | + providers: [{ provide: TrackingService, useValue: mockTrackingService }], |
| 17 | + }) |
| 18 | + .overrideGuard(AbilitiesGuard) |
| 19 | + .useValue({}) |
| 20 | + .compile(); |
| 21 | + |
| 22 | + controller = testingModule.get(TrackingController); |
| 23 | + trackingService = testingModule.get(TrackingService); |
| 24 | + }); |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + jest.clearAllMocks(); |
| 28 | + }); |
| 29 | + |
| 30 | + describe("getTracking", () => { |
| 31 | + const validId = "507f1f77bcf86cd799439011"; |
| 32 | + const invalidId = "123-id-invalido"; |
| 33 | + |
| 34 | + it("should return tracking status correctly (happy path)", async () => { |
| 35 | + trackingService.getTrackingStatus.mockResolvedValue(mockResponse); |
| 36 | + |
| 37 | + const response = await controller.getTracking(validId); |
| 38 | + |
| 39 | + expect(response).toEqual(mockResponse); |
| 40 | + expect(response.currentStatus).toBe(VerificationRequestStatus.IN_TRIAGE); |
| 41 | + expect(trackingService.getTrackingStatus).toHaveBeenCalledWith(validId); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should throw BadRequestException if ID format is invalid", async () => { |
| 45 | + await expect(controller.getTracking(invalidId)).rejects.toThrow( |
| 46 | + BadRequestException |
| 47 | + ); |
| 48 | + |
| 49 | + expect(trackingService.getTrackingStatus).not.toHaveBeenCalled(); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should propagate service errors", async () => { |
| 53 | + trackingService.getTrackingStatus.mockRejectedValue(new Error("Database connection error")); |
| 54 | + |
| 55 | + await expect(controller.getTracking(validId)).rejects.toThrow("Database connection error"); |
| 56 | + }); |
| 57 | + }); |
| 58 | +}); |
0 commit comments