|
1 | 1 | // Integration test for OrganizationController |
2 | | -import { NextFunction, Request, Response } from "express"; |
3 | 2 | import OrganizationController from "../../presentation/controllers/OrganizationController"; |
| 3 | +import request from "supertest"; |
| 4 | +import express, { Express } from "express"; |
4 | 5 |
|
5 | | -describe("OrganizationController Integration", () => { |
6 | | - it("should have a createOrganization method", () => { |
7 | | - expect(typeof OrganizationController.createOrganization).toBe("function"); |
| 6 | +// Mock the OrganizationService |
| 7 | +jest.mock("../../../../services/OrganizationService"); |
| 8 | +import { OrganizationService } from "../../../../services/OrganizationService"; |
| 9 | + |
| 10 | +// Mock asyncHandler to just return the function (for test simplicity) |
| 11 | +jest.mock("../../../../utils/asyncHandler", () => ({ |
| 12 | + asyncHandler: <T extends (...args: unknown[]) => unknown>(fn: T) => fn, |
| 13 | +})); |
| 14 | + |
| 15 | +function setupApp(): Express { |
| 16 | + const app = express(); |
| 17 | + app.use(express.json()); |
| 18 | + // OrganizationController is a singleton instance |
| 19 | + app.post("/organizations", OrganizationController.createOrganization); |
| 20 | + app.get("/organizations/:id", OrganizationController.getOrganizationById); |
| 21 | + app.get( |
| 22 | + "/organizations/email/:email", |
| 23 | + OrganizationController.getOrganizationByEmail |
| 24 | + ); |
| 25 | + app.put("/organizations/:id", OrganizationController.updateOrganization); |
| 26 | + app.delete("/organizations/:id", OrganizationController.deleteOrganization); |
| 27 | + app.get("/organizations", OrganizationController.getAllOrganizations); |
| 28 | + return app; |
| 29 | +} |
| 30 | + |
| 31 | +describe("OrganizationController", () => { |
| 32 | + let app: Express; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + app = setupApp(); |
| 36 | + jest.clearAllMocks(); |
| 37 | + }); |
| 38 | + |
| 39 | + describe("POST /organizations", () => { |
| 40 | + it("should create an organization and return 201", async () => { |
| 41 | + const mockOrg = { id: "1", name: "Org" }; |
| 42 | + (OrganizationService as jest.Mock).mockImplementation(() => ({ |
| 43 | + createOrganization: jest.fn().mockResolvedValue(mockOrg), |
| 44 | + })); |
| 45 | + const res = await request(app).post("/organizations").send({ |
| 46 | + name: "Org", |
| 47 | + email: "org@mail.com", |
| 48 | + password: "pass", |
| 49 | + category: "cat", |
| 50 | + wallet: "wallet", |
| 51 | + }); |
| 52 | + expect(res.status).toBe(201); |
| 53 | + expect(res.body).toEqual(mockOrg); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe("GET /organizations/:id", () => { |
| 58 | + it("should return an organization by id", async () => { |
| 59 | + const mockOrg = { id: "1", name: "Org" }; |
| 60 | + (OrganizationService as jest.Mock).mockImplementation(() => ({ |
| 61 | + getOrganizationById: jest.fn().mockResolvedValue(mockOrg), |
| 62 | + })); |
| 63 | + const res = await request(app).get("/organizations/1"); |
| 64 | + expect(res.status).toBe(200); |
| 65 | + expect(res.body).toEqual(mockOrg); |
| 66 | + }); |
| 67 | + it("should return 404 if not found", async () => { |
| 68 | + (OrganizationService as jest.Mock).mockImplementation(() => ({ |
| 69 | + getOrganizationById: jest.fn().mockResolvedValue(null), |
| 70 | + })); |
| 71 | + const res = await request(app).get("/organizations/1"); |
| 72 | + expect(res.status).toBe(404); |
| 73 | + expect(res.body.error).toBe("Organization not found"); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe("GET /organizations/email/:email", () => { |
| 78 | + it("should return an organization by email", async () => { |
| 79 | + const mockOrg = { id: "1", name: "Org" }; |
| 80 | + (OrganizationService as jest.Mock).mockImplementation(() => ({ |
| 81 | + getOrganizationByEmail: jest.fn().mockResolvedValue(mockOrg), |
| 82 | + })); |
| 83 | + const res = await request(app).get("/organizations/email/test@mail.com"); |
| 84 | + expect(res.status).toBe(200); |
| 85 | + expect(res.body).toEqual(mockOrg); |
| 86 | + }); |
| 87 | + it("should return 404 if not found", async () => { |
| 88 | + (OrganizationService as jest.Mock).mockImplementation(() => ({ |
| 89 | + getOrganizationByEmail: jest.fn().mockResolvedValue(null), |
| 90 | + })); |
| 91 | + const res = await request(app).get("/organizations/email/test@mail.com"); |
| 92 | + expect(res.status).toBe(404); |
| 93 | + expect(res.body.error).toBe("Organization not found"); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe("PUT /organizations/:id", () => { |
| 98 | + it("should update an organization and return 200", async () => { |
| 99 | + const mockOrg = { id: "1", name: "Updated Org" }; |
| 100 | + (OrganizationService as jest.Mock).mockImplementation(() => ({ |
| 101 | + updateOrganization: jest.fn().mockResolvedValue(mockOrg), |
| 102 | + })); |
| 103 | + const res = await request(app) |
| 104 | + .put("/organizations/1") |
| 105 | + .send({ name: "Updated Org" }); |
| 106 | + expect(res.status).toBe(200); |
| 107 | + expect(res.body).toEqual(mockOrg); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe("DELETE /organizations/:id", () => { |
| 112 | + it("should delete an organization and return 204", async () => { |
| 113 | + (OrganizationService as jest.Mock).mockImplementation(() => ({ |
| 114 | + deleteOrganization: jest.fn().mockResolvedValue(undefined), |
| 115 | + })); |
| 116 | + const res = await request(app).delete("/organizations/1"); |
| 117 | + expect(res.status).toBe(204); |
| 118 | + }); |
8 | 119 | }); |
9 | 120 |
|
10 | | - test("should return error if required fields are missing on createOrganization", async () => { |
11 | | - const req = { body: {} } as Partial<Request>; |
12 | | - const res = { |
13 | | - status: jest.fn().mockReturnThis(), |
14 | | - json: jest.fn(), |
15 | | - } as Partial<Response>; |
16 | | - const next = jest.fn() as NextFunction; |
17 | | - OrganizationController.createOrganization( |
18 | | - req as Request, |
19 | | - res as Response, |
20 | | - next |
21 | | - ); |
22 | | - expect(res.status).toHaveBeenCalledWith(400); |
23 | | - expect(res.json).toHaveBeenCalledWith( |
24 | | - expect.objectContaining({ error: expect.any(String) }) |
25 | | - ); |
| 121 | + describe("GET /organizations", () => { |
| 122 | + it("should return all organizations", async () => { |
| 123 | + const mockOrgs = [{ id: "1" }, { id: "2" }]; |
| 124 | + (OrganizationService as jest.Mock).mockImplementation(() => ({ |
| 125 | + getAllOrganizations: jest.fn().mockResolvedValue(mockOrgs), |
| 126 | + })); |
| 127 | + const res = await request(app).get("/organizations"); |
| 128 | + expect(res.status).toBe(200); |
| 129 | + expect(res.body).toEqual(mockOrgs); |
| 130 | + }); |
26 | 131 | }); |
27 | 132 | }); |
0 commit comments