1
+ import { addHeaders } from '../../../../auth/authentication/mock' ;
2
+
3
+ import {
4
+ mockBlogFindByUrl , mockFindInfoWithTextById , BLOG_ID , BLOG_URL
5
+ } from './mock' ;
6
+
7
+ import supertest from 'supertest' ;
8
+ import app from '../../../../../src/app' ;
9
+ import { Types } from 'mongoose' ;
10
+
11
+ describe ( 'BlogDetail by URL route' , ( ) => {
12
+
13
+ beforeEach ( ( ) => {
14
+ mockBlogFindByUrl . mockClear ( ) ;
15
+ } ) ;
16
+
17
+ const request = supertest ( app ) ;
18
+ const endpoint = '/v1/blog/url' ;
19
+
20
+ it ( 'Should send error when endpoint query is not passed' , async ( ) => {
21
+ const response = await addHeaders ( request . get ( endpoint ) ) ;
22
+ expect ( response . status ) . toBe ( 400 ) ;
23
+ expect ( response . body . message ) . toMatch ( / e n d p o i n t / ) ;
24
+ expect ( response . body . message ) . toMatch ( / r e q u i r e d / ) ;
25
+ expect ( mockBlogFindByUrl ) . not . toBeCalled ( ) ;
26
+ } ) ;
27
+
28
+ it ( 'Should send error when url endpoint is more that 200 chars' , async ( ) => {
29
+ const param = new Array ( 201 ) . fill ( 'a' ) . join ( '' ) ;
30
+ const response = await addHeaders ( request . get ( endpoint )
31
+ . query ( { endpoint : param } )
32
+ ) ;
33
+ expect ( response . status ) . toBe ( 400 ) ;
34
+ expect ( response . body . message ) . toMatch ( / l e n g t h m u s t / ) ;
35
+ expect ( response . body . message ) . toMatch ( / 2 0 0 / ) ;
36
+ expect ( mockBlogFindByUrl ) . not . toBeCalled ( ) ;
37
+ } ) ;
38
+
39
+ it ( 'Should send error when blog do not exists' , async ( ) => {
40
+ const response = await addHeaders ( request . get ( endpoint )
41
+ . query ( { endpoint : 'xyz' } )
42
+ ) ;
43
+ expect ( response . status ) . toBe ( 400 ) ;
44
+ expect ( response . body . message ) . toMatch ( / d o n o t e x i s t s / ) ;
45
+ expect ( mockBlogFindByUrl ) . toBeCalledTimes ( 1 ) ;
46
+ expect ( mockBlogFindByUrl ) . toBeCalledWith ( 'xyz' ) ;
47
+ } ) ;
48
+
49
+ it ( 'Should send data when blog exists' , async ( ) => {
50
+ const response = await addHeaders ( request . get ( endpoint )
51
+ . query ( { endpoint : BLOG_URL } )
52
+ ) ;
53
+ expect ( response . status ) . toBe ( 200 ) ;
54
+ expect ( response . body . message ) . toMatch ( / s u c c e s s / ) ;
55
+ expect ( response . body . data ) . toBeDefined ( ) ;
56
+ expect ( response . body . data ) . toHaveProperty ( '_id' ) ;
57
+ expect ( mockBlogFindByUrl ) . toBeCalledTimes ( 1 ) ;
58
+ expect ( mockBlogFindByUrl ) . toBeCalledWith ( BLOG_URL ) ;
59
+ } ) ;
60
+ } ) ;
61
+
62
+ describe ( 'BlogDetail by id route' , ( ) => {
63
+
64
+ beforeEach ( ( ) => {
65
+ mockFindInfoWithTextById . mockClear ( ) ;
66
+ } ) ;
67
+
68
+ const request = supertest ( app ) ;
69
+ const endpoint = '/v1/blog/id/' ;
70
+
71
+ it ( 'Should send error when invalid id is passed' , async ( ) => {
72
+ const response = await addHeaders ( request . get ( endpoint + 'abc' ) ) ;
73
+ expect ( response . status ) . toBe ( 400 ) ;
74
+ expect ( response . body . message ) . toMatch ( / i n v a l i d / ) ;
75
+ expect ( mockFindInfoWithTextById ) . not . toBeCalled ( ) ;
76
+ } ) ;
77
+
78
+ it ( 'Should send error when blog do not exists' , async ( ) => {
79
+ const response = await addHeaders (
80
+ request . get ( endpoint + new Types . ObjectId ( ) . toHexString ( ) )
81
+ ) ;
82
+ expect ( response . status ) . toBe ( 400 ) ;
83
+ expect ( response . body . message ) . toMatch ( / d o n o t e x i s t s / ) ;
84
+ expect ( mockFindInfoWithTextById ) . toBeCalledTimes ( 1 ) ;
85
+ } ) ;
86
+
87
+ it ( 'Should send data when blog exists' , async ( ) => {
88
+ const response = await addHeaders (
89
+ request . get ( endpoint + BLOG_ID . toHexString ( ) )
90
+ ) ;
91
+ expect ( response . status ) . toBe ( 200 ) ;
92
+ expect ( response . body . message ) . toMatch ( / s u c c e s s / ) ;
93
+ expect ( response . body . data ) . toBeDefined ( ) ;
94
+ expect ( response . body . data ) . toHaveProperty ( '_id' ) ;
95
+ expect ( mockFindInfoWithTextById ) . toBeCalledTimes ( 1 ) ;
96
+ } ) ;
97
+ } ) ;
0 commit comments