Skip to content

Commit 6e36ca6

Browse files
committed
Move controllers to module structure and add controller-level tests
1 parent b44c4f4 commit 6e36ca6

5 files changed

Lines changed: 372 additions & 44 deletions

File tree

src/.DS_Store

0 Bytes
Binary file not shown.

src/modules/nft/__tests__/controllers/NFTController.int.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Request, Response } from "express";
2-
// Integration test for NFTController
32
import NFTController from "../../presentation/controllers/NFTController";
43

54
describe("NFTController Integration", () => {
@@ -12,7 +11,7 @@ describe("NFTController Integration", () => {
1211
const res = {
1312
status: jest.fn().mockReturnThis(),
1413
json: jest.fn(),
15-
} as Partial<Response>;
14+
} as unknown as Response;
1615
await NFTController.createNFT(req as Request, res as Response);
1716
expect(res.status).toHaveBeenCalledWith(400);
1817
expect(res.json).toHaveBeenCalledWith(
Lines changed: 125 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,132 @@
11
// Integration test for OrganizationController
2-
import { NextFunction, Request, Response } from "express";
32
import OrganizationController from "../../presentation/controllers/OrganizationController";
3+
import request from "supertest";
4+
import express, { Express } from "express";
45

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+
});
8119
});
9120

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+
});
26131
});
27132
});
Lines changed: 150 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,155 @@
11
// Integration test for UserController
2+
import request from "supertest";
3+
import express, { Express } from "express";
24
import UserController from "../../presentation/controllers/UserController";
35

4-
describe("UserController Integration", () => {
5-
it("should have a createUser method", () => {
6-
expect(typeof UserController.prototype.createUser).toBe("function");
6+
// Mock the UserService
7+
jest.mock("../../../../services/UserService");
8+
import { UserService } from "../../../../services/UserService";
9+
10+
// Mock DTOs
11+
jest.mock("../../../../modules/user/dto/CreateUserDto", () => {
12+
return {
13+
CreateUserDto: function () {
14+
return {};
15+
},
16+
};
17+
});
18+
jest.mock("../../../../modules/user/dto/UpdateUserDto", () => {
19+
return {
20+
UpdateUserDto: function () {
21+
return {};
22+
},
23+
};
24+
});
25+
26+
function setupApp(): Express {
27+
const app = express();
28+
app.use(express.json());
29+
const controller = new UserController();
30+
app.post("/users", controller.createUser.bind(controller));
31+
app.get("/users/:id", controller.getUserById.bind(controller));
32+
app.get("/users", controller.getUserByEmail.bind(controller));
33+
app.put("/users/:id", controller.updateUser.bind(controller));
34+
return app;
35+
}
36+
37+
describe("UserController", () => {
38+
let app: Express;
39+
40+
beforeEach(() => {
41+
app = setupApp();
42+
jest.clearAllMocks();
43+
});
44+
45+
describe("POST /users", () => {
46+
it("should create a user and return 201", async () => {
47+
const mockUser = { id: "1", email: "test@example.com" };
48+
(UserService as jest.Mock).mockImplementation(() => ({
49+
createUser: jest.fn().mockResolvedValue(mockUser),
50+
}));
51+
const res = await request(app)
52+
.post("/users")
53+
.send({ email: "test@example.com" });
54+
expect(res.status).toBe(201);
55+
expect(res.body).toEqual(mockUser);
56+
});
57+
58+
it("should handle errors and return 400", async () => {
59+
(UserService as jest.Mock).mockImplementation(() => ({
60+
createUser: jest.fn().mockRejectedValue(new Error("fail")),
61+
}));
62+
const res = await request(app)
63+
.post("/users")
64+
.send({ email: "test@example.com" });
65+
expect(res.status).toBe(400);
66+
expect(res.body.error).toBe("fail");
67+
});
68+
});
69+
70+
describe("GET /users/:id", () => {
71+
it("should return a user by id", async () => {
72+
const mockUser = { id: "1", email: "test@example.com" };
73+
(UserService as jest.Mock).mockImplementation(() => ({
74+
getUserById: jest.fn().mockResolvedValue(mockUser),
75+
}));
76+
const res = await request(app).get("/users/1");
77+
expect(res.status).toBe(200);
78+
expect(res.body).toEqual(mockUser);
79+
});
80+
81+
it("should return 404 if user not found", async () => {
82+
(UserService as jest.Mock).mockImplementation(() => ({
83+
getUserById: jest.fn().mockResolvedValue(null),
84+
}));
85+
const res = await request(app).get("/users/1");
86+
expect(res.status).toBe(404);
87+
expect(res.body.error).toBe("User not found");
88+
});
89+
90+
it("should handle errors and return 400", async () => {
91+
(UserService as jest.Mock).mockImplementation(() => ({
92+
getUserById: jest.fn().mockRejectedValue(new Error("fail")),
93+
}));
94+
const res = await request(app).get("/users/1");
95+
expect(res.status).toBe(400);
96+
expect(res.body.error).toBe("fail");
97+
});
98+
});
99+
100+
describe("GET /users?email=", () => {
101+
it("should return a user by email", async () => {
102+
const mockUser = { id: "1", email: "test@example.com" };
103+
(UserService as jest.Mock).mockImplementation(() => ({
104+
getUserByEmail: jest.fn().mockResolvedValue(mockUser),
105+
}));
106+
const res = await request(app).get("/users?email=test@example.com");
107+
expect(res.status).toBe(200);
108+
expect(res.body).toEqual(mockUser);
109+
});
110+
111+
it("should return 400 if email is missing", async () => {
112+
const res = await request(app).get("/users");
113+
expect(res.status).toBe(400);
114+
expect(res.body.error).toBe("Email is required");
115+
});
116+
117+
it("should return 404 if user not found", async () => {
118+
(UserService as jest.Mock).mockImplementation(() => ({
119+
getUserByEmail: jest.fn().mockResolvedValue(null),
120+
}));
121+
const res = await request(app).get("/users?email=test@example.com");
122+
expect(res.status).toBe(404);
123+
expect(res.body.error).toBe("User not found");
124+
});
125+
126+
it("should handle errors and return 400", async () => {
127+
(UserService as jest.Mock).mockImplementation(() => ({
128+
getUserByEmail: jest.fn().mockRejectedValue(new Error("fail")),
129+
}));
130+
const res = await request(app).get("/users?email=test@example.com");
131+
expect(res.status).toBe(400);
132+
expect(res.body.error).toBe("fail");
133+
});
134+
});
135+
136+
describe("PUT /users/:id", () => {
137+
it("should update a user and return 200", async () => {
138+
(UserService as jest.Mock).mockImplementation(() => ({
139+
updateUser: jest.fn().mockResolvedValue(undefined),
140+
}));
141+
const res = await request(app).put("/users/1").send({ name: "Updated" });
142+
expect(res.status).toBe(200);
143+
expect(res.body.message).toBe("User updated successfully");
144+
});
145+
146+
it("should handle errors and return 400", async () => {
147+
(UserService as jest.Mock).mockImplementation(() => ({
148+
updateUser: jest.fn().mockRejectedValue(new Error("fail")),
149+
}));
150+
const res = await request(app).put("/users/1").send({ name: "Updated" });
151+
expect(res.status).toBe(400);
152+
expect(res.body.error).toBe("fail");
153+
});
7154
});
8155
});

0 commit comments

Comments
 (0)