|
| 1 | +import type { Request, Response } from "express"; |
| 2 | + |
| 3 | +import getByOrganismeIdController from "../../controllers/agrement"; // ton contrôleur |
| 4 | +import getByOrganismeIdService from "../../services/agrements/Agrements"; |
| 5 | +import AppError from "../../utils/error"; |
| 6 | + |
| 7 | +// On mocke les dépendances |
| 8 | +jest.mock("../../services/agrements/Agrements"); |
| 9 | +jest.mock("../../utils/logger", () => () => ({ |
| 10 | + d: jest.fn(), |
| 11 | + i: jest.fn(), |
| 12 | + w: jest.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +describe("🧪 Controller: agrement.getByOrganismeId", () => { |
| 16 | + const mockReq = {} as Request; |
| 17 | + const mockRes = {} as Response; |
| 18 | + const mockNext = jest.fn(); |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + jest.clearAllMocks(); |
| 22 | + mockRes.json = jest.fn().mockReturnValue(mockRes); |
| 23 | + }); |
| 24 | + |
| 25 | + it("Renvoie le résultat du service avec un ID valide", async () => { |
| 26 | + (getByOrganismeIdService as jest.Mock).mockResolvedValue({ |
| 27 | + agrement: { id: 1, organismeId: 42 }, |
| 28 | + }); |
| 29 | + |
| 30 | + mockReq.params = { id: "42" }; |
| 31 | + |
| 32 | + await getByOrganismeIdController.getByOrganismeId( |
| 33 | + mockReq as any, |
| 34 | + mockRes, |
| 35 | + mockNext, |
| 36 | + ); |
| 37 | + |
| 38 | + expect(getByOrganismeIdService).toHaveBeenCalledWith({ organismeId: 42 }); |
| 39 | + expect(mockRes.json).toHaveBeenCalledWith({ |
| 40 | + agrement: { id: 1, organismeId: 42 }, |
| 41 | + }); |
| 42 | + expect(mockNext).not.toHaveBeenCalled(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("Appelle next avec une AppError si l'id est invalide", async () => { |
| 46 | + mockReq.params = { id: "abc" }; |
| 47 | + |
| 48 | + await getByOrganismeIdController.getByOrganismeId( |
| 49 | + mockReq as any, |
| 50 | + mockRes, |
| 51 | + mockNext, |
| 52 | + ); |
| 53 | + |
| 54 | + expect(getByOrganismeIdService).not.toHaveBeenCalled(); |
| 55 | + expect(mockNext).toHaveBeenCalledTimes(1); |
| 56 | + |
| 57 | + const error = mockNext.mock.calls[0][0]; |
| 58 | + expect(error).toBeInstanceOf(AppError); |
| 59 | + expect(error.message).toBe("Paramètre incorrect"); |
| 60 | + expect(error.statusCode).toBe(400); |
| 61 | + }); |
| 62 | + |
| 63 | + it("Appelle next si le service lève une erreur", async () => { |
| 64 | + (getByOrganismeIdService as jest.Mock).mockRejectedValue( |
| 65 | + new Error("DB fail"), |
| 66 | + ); |
| 67 | + mockReq.params = { id: "10" }; |
| 68 | + |
| 69 | + await getByOrganismeIdController.getByOrganismeId( |
| 70 | + mockReq as any, |
| 71 | + mockRes, |
| 72 | + mockNext, |
| 73 | + ); |
| 74 | + |
| 75 | + expect(getByOrganismeIdService).toHaveBeenCalledWith({ organismeId: 10 }); |
| 76 | + expect(mockNext).toHaveBeenCalledTimes(1); |
| 77 | + |
| 78 | + const error = mockNext.mock.calls[0][0]; |
| 79 | + expect(error).toBeInstanceOf(Error); |
| 80 | + expect(error.message).toBe("DB fail"); |
| 81 | + }); |
| 82 | +}); |
0 commit comments