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
+ } )
0 commit comments