-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTopics.test.ts
More file actions
161 lines (139 loc) · 3.93 KB
/
Topics.test.ts
File metadata and controls
161 lines (139 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { Request, Response } from "express";
import {
createTopic,
updateTopic,
deleteTopic,
} from "../src/controllers/topic.controller";
import * as topicServices from "../src/services/topic.service";
import { ApiError } from "../src/utils/apiError";
import { Topic } from "../src/generated/prisma/browser";
beforeEach(() => {
jest.clearAllMocks();
});
const mockResponse = (): Response => {
const res = {} as Response;
res.status = jest.fn().mockReturnThis();
res.json = jest.fn().mockReturnThis();
return res;
};
const baseMockTopic: Topic = {
id: 1,
title: "Original Title",
description: "Original Description",
createdById: "1",
createdAt: new Date("2024-01-01"),
updatedById: "1",
updatedAt: new Date("2025-07-19"),
};
describe("Create topic", () => {
it("should repond with 201 and create topic", async () => {
const req = {
body: {
title: "demo",
description: "demo test to check is everything is working or not",
adminId: "1",
},
} as Request;
const res = mockResponse();
const mockTopic: Topic = {
id: 1,
title: "demo",
description: "demo test to check is everything is working or not",
createdById: req.body.adminId,
createdAt: new Date(),
updatedById: req.body.adminId,
updatedAt: new Date(),
};
const spy = jest
.spyOn(topicServices, "createTopics")
.mockResolvedValue(mockTopic);
await createTopic(req, res);
expect(spy).toHaveBeenCalledWith(
req.body.title,
req.body.description,
req.body.adminId,
);
expect(res.status).toHaveBeenCalledWith(201);
expect(res.json).toHaveBeenCalledWith(mockTopic);
});
it("should throw 400 for missing fields", async () => {
const req = {
body: {},
} as Request;
const res = mockResponse();
await expect(createTopic(req, res)).rejects.toThrow(
new ApiError("missing required fields", 400),
);
});
});
describe("update Topic", () => {
it("should respond with 200 and should update topic", async () => {
const req = {
params: {
topicId: 1,
},
body: {
adminId: "2",
updateData: {
title: "updated title",
},
},
} as unknown as Request;
const res = mockResponse();
const mockUpdated: Topic = {
...baseMockTopic,
title: "updated title",
updatedAt: new Date(),
updatedById: req.body.adminId,
};
const spy = jest
.spyOn(topicServices, "updateTopic")
.mockResolvedValue(mockUpdated);
await updateTopic(req, res);
expect(spy).toHaveBeenCalledWith(
req.params.topicId,
req.body.updateData,
req.body.adminId,
);
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith(mockUpdated);
});
it("should throw 400 for missing required field", async () => {
const req = {
params: {
topicId: 1,
},
body: {
adminId: "2",
},
} as unknown as Request;
const res = mockResponse();
await expect(updateTopic(req, res)).rejects.toThrow(
new ApiError("missing required fields", 400),
);
});
});
describe("delete topic", () => {
it("should respond with 200 and delete topic", async () => {
const req = {
params: {
topicId: 1,
},
} as unknown as Request;
const res = mockResponse();
const spy = jest
.spyOn(topicServices, "deleteTopic")
.mockResolvedValue(baseMockTopic);
await deleteTopic(req, res);
expect(spy).toHaveBeenCalledWith(req.params.topicId);
expect(res.status).toHaveBeenCalledWith(200);
expect(res.json).toHaveBeenCalledWith({ status: "SUCCESS" });
});
it("should throw 400 for missing required field", async () => {
const req = { params: {} } as Request;
const res = mockResponse();
await expect(deleteTopic(req, res)).rejects.toThrow(
new ApiError("missing required fields", 400),
);
});
});