|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | + |
| 3 | +import type { PrismaClient } from "@calcom/prisma"; |
| 4 | + |
| 5 | +import { WrongAssignmentReportRepository } from "./WrongAssignmentReportRepository"; |
| 6 | + |
| 7 | +describe("WrongAssignmentReportRepository", () => { |
| 8 | + let repository: WrongAssignmentReportRepository; |
| 9 | + |
| 10 | + const mockPrisma = { |
| 11 | + wrongAssignmentReport: { |
| 12 | + findUnique: vi.fn(), |
| 13 | + create: vi.fn(), |
| 14 | + }, |
| 15 | + } as unknown as PrismaClient; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + vi.clearAllMocks(); |
| 19 | + repository = new WrongAssignmentReportRepository(mockPrisma); |
| 20 | + }); |
| 21 | + |
| 22 | + describe("existsByBookingUid", () => { |
| 23 | + it("should return true when a report exists for the booking", async () => { |
| 24 | + mockPrisma.wrongAssignmentReport.findUnique.mockResolvedValue({ id: "report-123" }); |
| 25 | + |
| 26 | + const result = await repository.existsByBookingUid("test-booking-uid"); |
| 27 | + |
| 28 | + expect(result).toBe(true); |
| 29 | + expect(mockPrisma.wrongAssignmentReport.findUnique).toHaveBeenCalledWith({ |
| 30 | + where: { bookingUid: "test-booking-uid" }, |
| 31 | + select: { id: true }, |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should return false when no report exists for the booking", async () => { |
| 36 | + mockPrisma.wrongAssignmentReport.findUnique.mockResolvedValue(null); |
| 37 | + |
| 38 | + const result = await repository.existsByBookingUid("non-existent-booking-uid"); |
| 39 | + |
| 40 | + expect(result).toBe(false); |
| 41 | + expect(mockPrisma.wrongAssignmentReport.findUnique).toHaveBeenCalledWith({ |
| 42 | + where: { bookingUid: "non-existent-booking-uid" }, |
| 43 | + select: { id: true }, |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should handle empty string bookingUid", async () => { |
| 48 | + mockPrisma.wrongAssignmentReport.findUnique.mockResolvedValue(null); |
| 49 | + |
| 50 | + const result = await repository.existsByBookingUid(""); |
| 51 | + |
| 52 | + expect(result).toBe(false); |
| 53 | + expect(mockPrisma.wrongAssignmentReport.findUnique).toHaveBeenCalledWith({ |
| 54 | + where: { bookingUid: "" }, |
| 55 | + select: { id: true }, |
| 56 | + }); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe("createReport", () => { |
| 61 | + it("should create a report with all fields", async () => { |
| 62 | + const input = { |
| 63 | + bookingUid: "test-booking-uid", |
| 64 | + reportedById: 1, |
| 65 | + correctAssignee: "correct@example.com", |
| 66 | + additionalNotes: "This booking was assigned incorrectly", |
| 67 | + teamId: 5, |
| 68 | + routingFormId: "routing-form-123", |
| 69 | + }; |
| 70 | + |
| 71 | + const mockCreatedReport = { id: "report-uuid-123" }; |
| 72 | + mockPrisma.wrongAssignmentReport.create.mockResolvedValue(mockCreatedReport); |
| 73 | + |
| 74 | + const result = await repository.createReport(input); |
| 75 | + |
| 76 | + expect(result).toEqual({ id: "report-uuid-123" }); |
| 77 | + expect(mockPrisma.wrongAssignmentReport.create).toHaveBeenCalledWith({ |
| 78 | + data: input, |
| 79 | + select: { id: true }, |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should create a report with null optional fields", async () => { |
| 84 | + const input = { |
| 85 | + bookingUid: "test-booking-uid-2", |
| 86 | + reportedById: 2, |
| 87 | + correctAssignee: null, |
| 88 | + additionalNotes: "Wrong person", |
| 89 | + teamId: null, |
| 90 | + routingFormId: null, |
| 91 | + }; |
| 92 | + |
| 93 | + const mockCreatedReport = { id: "report-uuid-456" }; |
| 94 | + mockPrisma.wrongAssignmentReport.create.mockResolvedValue(mockCreatedReport); |
| 95 | + |
| 96 | + const result = await repository.createReport(input); |
| 97 | + |
| 98 | + expect(result).toEqual({ id: "report-uuid-456" }); |
| 99 | + expect(mockPrisma.wrongAssignmentReport.create).toHaveBeenCalledWith({ |
| 100 | + data: input, |
| 101 | + select: { id: true }, |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should create a report with empty additionalNotes", async () => { |
| 106 | + const input = { |
| 107 | + bookingUid: "test-booking-uid-3", |
| 108 | + reportedById: 3, |
| 109 | + correctAssignee: "someone@example.com", |
| 110 | + additionalNotes: "", |
| 111 | + teamId: 10, |
| 112 | + routingFormId: "routing-form-456", |
| 113 | + }; |
| 114 | + |
| 115 | + const mockCreatedReport = { id: "report-uuid-789" }; |
| 116 | + mockPrisma.wrongAssignmentReport.create.mockResolvedValue(mockCreatedReport); |
| 117 | + |
| 118 | + const result = await repository.createReport(input); |
| 119 | + |
| 120 | + expect(result).toEqual({ id: "report-uuid-789" }); |
| 121 | + expect(mockPrisma.wrongAssignmentReport.create).toHaveBeenCalledWith({ |
| 122 | + data: input, |
| 123 | + select: { id: true }, |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + it("should propagate database errors", async () => { |
| 128 | + const input = { |
| 129 | + bookingUid: "test-booking-uid", |
| 130 | + reportedById: 1, |
| 131 | + correctAssignee: null, |
| 132 | + additionalNotes: "Notes", |
| 133 | + teamId: null, |
| 134 | + routingFormId: null, |
| 135 | + }; |
| 136 | + |
| 137 | + mockPrisma.wrongAssignmentReport.create.mockRejectedValue(new Error("Database connection failed")); |
| 138 | + |
| 139 | + await expect(repository.createReport(input)).rejects.toThrow("Database connection failed"); |
| 140 | + }); |
| 141 | + |
| 142 | + it("should propagate foreign key constraint errors", async () => { |
| 143 | + const input = { |
| 144 | + bookingUid: "non-existent-booking", |
| 145 | + reportedById: 999999, |
| 146 | + correctAssignee: null, |
| 147 | + additionalNotes: "Notes", |
| 148 | + teamId: null, |
| 149 | + routingFormId: null, |
| 150 | + }; |
| 151 | + |
| 152 | + mockPrisma.wrongAssignmentReport.create.mockRejectedValue( |
| 153 | + new Error("Foreign key constraint failed on the field: `bookingUid`") |
| 154 | + ); |
| 155 | + |
| 156 | + await expect(repository.createReport(input)).rejects.toThrow("Foreign key constraint failed"); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments