Skip to content

Commit ba0ac20

Browse files
committed
test: add tests for comment controller
1 parent 836d6aa commit ba0ac20

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import CommentController from './comment.controller'
2+
import * as CommentRepository from '../repositories/comment.repository'
3+
import {generateCommentsData, generateCommentPayload, generateCommentData} from 'test/utils/generate'
4+
5+
afterEach(() => {
6+
jest.resetAllMocks()
7+
})
8+
9+
describe("CommentController", () => {
10+
describe("getComments", () => {
11+
test("should return empty array", async () => {
12+
const spy = jest.spyOn(CommentRepository, 'getComments').mockResolvedValueOnce([])
13+
const controller = new CommentController();
14+
const comments = await controller.getComments();
15+
expect(comments).toEqual([])
16+
expect(spy).toHaveBeenCalledWith()
17+
expect(spy).toHaveBeenCalledTimes(1)
18+
})
19+
20+
test("should return comments list", async () => {
21+
const commentsData = generateCommentsData(2)
22+
const spy = jest.spyOn(CommentRepository, 'getComments').mockResolvedValueOnce(commentsData)
23+
const controller = new CommentController();
24+
const comments = await controller.getComments();
25+
expect(comments).toEqual(commentsData)
26+
expect(spy).toHaveBeenCalledWith()
27+
expect(spy).toHaveBeenCalledTimes(1)
28+
})
29+
})
30+
31+
describe("createComment", () => {
32+
test("should add comment to the database", async () => {
33+
const payload = generateCommentPayload()
34+
const commentData = generateCommentData(payload)
35+
const spy = jest.spyOn(CommentRepository, 'createComment').mockResolvedValueOnce(commentData)
36+
const controller = new CommentController();
37+
const comment = await controller.createComment(payload);
38+
expect(comment).toMatchObject(payload)
39+
expect(comment).toEqual(commentData)
40+
expect(spy).toHaveBeenCalledWith(payload)
41+
expect(spy).toHaveBeenCalledTimes(1)
42+
})
43+
})
44+
45+
describe("getComment", () => {
46+
test("should return comment from the database", async () => {
47+
const id = 1
48+
const commentData = generateCommentData({id})
49+
const spy = jest.spyOn(CommentRepository, 'getComment').mockResolvedValueOnce(commentData)
50+
const controller = new CommentController();
51+
const comment = await controller.getComment(id.toString());
52+
expect(comment).toEqual(commentData)
53+
expect(comment?.id).toBe(id)
54+
expect(spy).toHaveBeenCalledWith(id)
55+
expect(spy).toHaveBeenCalledTimes(1)
56+
})
57+
58+
test("should return null if comment not found", async () => {
59+
const id = 1
60+
const spy = jest.spyOn(CommentRepository, 'getComment').mockResolvedValueOnce(null)
61+
const controller = new CommentController();
62+
const comment = await controller.getComment(id.toString());
63+
expect(comment).toBeNull()
64+
expect(spy).toHaveBeenCalledWith(id)
65+
expect(spy).toHaveBeenCalledTimes(1)
66+
})
67+
})
68+
})

test/utils/generate.ts

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import faker from 'faker'
2-
import { User } from '../../src/models';
2+
import { User, Post } from '../../src/models';
33

44
export function generateUserData(overide = {}) {
55
return {
@@ -59,4 +59,35 @@ export function generatePostPayload() {
5959
content: faker.lorem.paragraph(),
6060
userId: faker.random.number(),
6161
}
62+
}
63+
64+
export function generateCommentData(overide = {}) {
65+
return {
66+
id: faker.random.number(),
67+
content: faker.lorem.paragraph(),
68+
userId: faker.random.number(),
69+
user: new User(),
70+
postId: faker.random.number(),
71+
post: new Post(),
72+
createdAt: new Date(),
73+
updatedAt: new Date(),
74+
...overide
75+
}
76+
}
77+
78+
export function generateCommentsData(n: number = 1, overide = {}) {
79+
return Array.from({
80+
length: n
81+
}, (_, i) => {
82+
return generateCommentData(overide)
83+
});
84+
}
85+
86+
87+
export function generateCommentPayload() {
88+
return {
89+
content: faker.lorem.paragraph(),
90+
userId: faker.random.number(),
91+
postId: faker.random.number(),
92+
}
6293
}

0 commit comments

Comments
 (0)