1
1
import { addHeaders } from '../../../../auth/authentication/mock' ;
2
2
3
- import { mockBlogFindByUrl , mockFindInfoWithTextById , BLOG_ID , BLOG_URL } from './mock' ;
3
+ import {
4
+ mockBlogCacheFetchById ,
5
+ mockBlogCacheFetchByUrl ,
6
+ mockBlogCacheSave ,
7
+ mockPublishedBlogFindByUrl ,
8
+ mockPublishedBlogFindById ,
9
+ BLOG_ID ,
10
+ BLOG_URL ,
11
+ BLOG_2_URL ,
12
+ BLOG_2_ID ,
13
+ } from './mock' ;
4
14
5
15
import supertest from 'supertest' ;
6
16
import app from '../../../../../src/app' ;
7
17
import { Types } from 'mongoose' ;
8
18
9
19
describe ( 'BlogDetail by URL route' , ( ) => {
10
20
beforeEach ( ( ) => {
11
- mockBlogFindByUrl . mockClear ( ) ;
21
+ mockBlogCacheFetchByUrl . mockClear ( ) ;
22
+ mockBlogCacheSave . mockClear ( ) ;
23
+ mockPublishedBlogFindByUrl . mockClear ( ) ;
12
24
} ) ;
13
25
14
26
const request = supertest ( app ) ;
@@ -19,7 +31,7 @@ describe('BlogDetail by URL route', () => {
19
31
expect ( response . status ) . toBe ( 400 ) ;
20
32
expect ( response . body . message ) . toMatch ( / e n d p o i n t / ) ;
21
33
expect ( response . body . message ) . toMatch ( / r e q u i r e d / ) ;
22
- expect ( mockBlogFindByUrl ) . not . toBeCalled ( ) ;
34
+ expect ( mockPublishedBlogFindByUrl ) . not . toBeCalled ( ) ;
23
35
} ) ;
24
36
25
37
it ( 'Should send error when url endpoint is more that 200 chars' , async ( ) => {
@@ -28,31 +40,60 @@ describe('BlogDetail by URL route', () => {
28
40
expect ( response . status ) . toBe ( 400 ) ;
29
41
expect ( response . body . message ) . toMatch ( / l e n g t h m u s t / ) ;
30
42
expect ( response . body . message ) . toMatch ( / 2 0 0 / ) ;
31
- expect ( mockBlogFindByUrl ) . not . toBeCalled ( ) ;
43
+ expect ( mockPublishedBlogFindByUrl ) . not . toBeCalled ( ) ;
32
44
} ) ;
33
45
34
46
it ( 'Should send error when blog do not exists for url' , async ( ) => {
35
47
const response = await addHeaders ( request . get ( endpoint ) . query ( { endpoint : 'xyz' } ) ) ;
36
- expect ( response . status ) . toBe ( 400 ) ;
37
- expect ( response . body . message ) . toMatch ( / d o n o t e x i s t s / ) ;
38
- expect ( mockBlogFindByUrl ) . toBeCalledTimes ( 1 ) ;
39
- expect ( mockBlogFindByUrl ) . toBeCalledWith ( 'xyz' ) ;
48
+ expect ( response . status ) . toBe ( 404 ) ;
49
+ expect ( response . body . message ) . toMatch ( / n o t f o u n d / ) ;
50
+ expect ( mockBlogCacheFetchByUrl ) . toBeCalledTimes ( 1 ) ;
51
+ expect ( mockBlogCacheFetchByUrl ) . toBeCalledWith ( 'xyz' ) ;
52
+ expect ( mockBlogCacheSave ) . not . toBeCalled ( ) ;
53
+ expect ( mockPublishedBlogFindByUrl ) . toBeCalledTimes ( 1 ) ;
54
+ expect ( mockPublishedBlogFindByUrl ) . toBeCalledWith ( 'xyz' ) ;
40
55
} ) ;
41
56
42
- it ( 'Should send data when blog exists for url' , async ( ) => {
57
+ it ( 'Should send cache data when blog exists for url in cache ' , async ( ) => {
43
58
const response = await addHeaders ( request . get ( endpoint ) . query ( { endpoint : BLOG_URL } ) ) ;
44
59
expect ( response . status ) . toBe ( 200 ) ;
45
60
expect ( response . body . message ) . toMatch ( / s u c c e s s / ) ;
46
61
expect ( response . body . data ) . toBeDefined ( ) ;
47
62
expect ( response . body . data ) . toHaveProperty ( '_id' ) ;
48
- expect ( mockBlogFindByUrl ) . toBeCalledTimes ( 1 ) ;
49
- expect ( mockBlogFindByUrl ) . toBeCalledWith ( BLOG_URL ) ;
63
+
64
+ expect ( mockBlogCacheFetchByUrl ) . toBeCalledTimes ( 1 ) ;
65
+ expect ( mockBlogCacheFetchByUrl ) . toBeCalledWith ( BLOG_URL ) ;
66
+ expect ( mockBlogCacheFetchByUrl ) . toReturnTimes ( 1 ) ;
67
+
68
+ expect ( mockPublishedBlogFindByUrl ) . not . toBeCalled ( ) ;
69
+ expect ( mockBlogCacheSave ) . not . toBeCalled ( ) ;
70
+ } ) ;
71
+
72
+ it ( 'Should send database data when blog dont exists for url in cache' , async ( ) => {
73
+ const response = await addHeaders ( request . get ( endpoint ) . query ( { endpoint : BLOG_2_URL } ) ) ;
74
+ expect ( response . status ) . toBe ( 200 ) ;
75
+ expect ( response . body . message ) . toMatch ( / s u c c e s s / ) ;
76
+ expect ( response . body . data ) . toBeDefined ( ) ;
77
+ expect ( response . body . data ) . toHaveProperty ( '_id' ) ;
78
+
79
+ expect ( mockBlogCacheFetchByUrl ) . toBeCalledTimes ( 1 ) ;
80
+ expect ( mockBlogCacheFetchByUrl ) . toBeCalledWith ( BLOG_2_URL ) ;
81
+ expect ( mockBlogCacheFetchByUrl ) . toReturnTimes ( 1 ) ;
82
+
83
+ expect ( mockPublishedBlogFindByUrl ) . toBeCalledTimes ( 1 ) ;
84
+ expect ( mockPublishedBlogFindByUrl ) . toBeCalledWith ( BLOG_2_URL ) ;
85
+ expect ( mockPublishedBlogFindByUrl ) . toReturnTimes ( 1 ) ;
86
+
87
+ expect ( mockBlogCacheSave ) . toBeCalledTimes ( 1 ) ;
88
+ expect ( mockBlogCacheSave ) . toReturnTimes ( 1 ) ;
50
89
} ) ;
51
90
} ) ;
52
91
53
92
describe ( 'BlogDetail by id route' , ( ) => {
54
93
beforeEach ( ( ) => {
55
- mockFindInfoWithTextById . mockClear ( ) ;
94
+ mockBlogCacheFetchById . mockClear ( ) ;
95
+ mockBlogCacheSave . mockClear ( ) ;
96
+ mockPublishedBlogFindById . mockClear ( ) ;
56
97
} ) ;
57
98
58
99
const request = supertest ( app ) ;
@@ -62,22 +103,47 @@ describe('BlogDetail by id route', () => {
62
103
const response = await addHeaders ( request . get ( endpoint + 'abc' ) ) ;
63
104
expect ( response . status ) . toBe ( 400 ) ;
64
105
expect ( response . body . message ) . toMatch ( / i n v a l i d / ) ;
65
- expect ( mockFindInfoWithTextById ) . not . toBeCalled ( ) ;
106
+ expect ( mockPublishedBlogFindById ) . not . toBeCalled ( ) ;
66
107
} ) ;
67
108
68
109
it ( 'Should send error when blog do not exists for id' , async ( ) => {
69
110
const response = await addHeaders ( request . get ( endpoint + new Types . ObjectId ( ) . toHexString ( ) ) ) ;
70
- expect ( response . status ) . toBe ( 400 ) ;
71
- expect ( response . body . message ) . toMatch ( / d o n o t e x i s t s / ) ;
72
- expect ( mockFindInfoWithTextById ) . toBeCalledTimes ( 1 ) ;
111
+ expect ( response . status ) . toBe ( 404 ) ;
112
+ expect ( response . body . message ) . toMatch ( / n o t f o u n d / ) ;
113
+ expect ( mockPublishedBlogFindById ) . toBeCalledTimes ( 1 ) ;
73
114
} ) ;
74
115
75
- it ( 'Should send data when blog exists for id' , async ( ) => {
116
+ it ( 'Should send cache data when blog exists for id in cache ' , async ( ) => {
76
117
const response = await addHeaders ( request . get ( endpoint + BLOG_ID . toHexString ( ) ) ) ;
77
118
expect ( response . status ) . toBe ( 200 ) ;
78
119
expect ( response . body . message ) . toMatch ( / s u c c e s s / ) ;
79
120
expect ( response . body . data ) . toBeDefined ( ) ;
80
121
expect ( response . body . data ) . toHaveProperty ( '_id' ) ;
81
- expect ( mockFindInfoWithTextById ) . toBeCalledTimes ( 1 ) ;
122
+
123
+ expect ( mockBlogCacheFetchById ) . toBeCalledTimes ( 1 ) ;
124
+ expect ( mockBlogCacheFetchById ) . toBeCalledWith ( BLOG_ID ) ;
125
+ expect ( mockBlogCacheFetchById ) . toReturnTimes ( 1 ) ;
126
+
127
+ expect ( mockPublishedBlogFindById ) . not . toBeCalled ( ) ;
128
+ expect ( mockBlogCacheSave ) . not . toBeCalled ( ) ;
129
+ } ) ;
130
+
131
+ it ( 'Should send database data when blog dont exists for url in cache' , async ( ) => {
132
+ const response = await addHeaders ( request . get ( endpoint + BLOG_2_ID . toHexString ( ) ) ) ;
133
+ expect ( response . status ) . toBe ( 200 ) ;
134
+ expect ( response . body . message ) . toMatch ( / s u c c e s s / ) ;
135
+ expect ( response . body . data ) . toBeDefined ( ) ;
136
+ expect ( response . body . data ) . toHaveProperty ( '_id' ) ;
137
+
138
+ expect ( mockBlogCacheFetchById ) . toBeCalledTimes ( 1 ) ;
139
+ expect ( mockBlogCacheFetchById ) . toBeCalledWith ( BLOG_2_ID ) ;
140
+ expect ( mockBlogCacheFetchById ) . toReturnTimes ( 1 ) ;
141
+
142
+ expect ( mockPublishedBlogFindById ) . toBeCalledTimes ( 1 ) ;
143
+ expect ( mockPublishedBlogFindById ) . toBeCalledWith ( BLOG_2_ID ) ;
144
+ expect ( mockPublishedBlogFindById ) . toReturnTimes ( 1 ) ;
145
+
146
+ expect ( mockBlogCacheSave ) . toBeCalledTimes ( 1 ) ;
147
+ expect ( mockBlogCacheSave ) . toReturnTimes ( 1 ) ;
82
148
} ) ;
83
149
} ) ;
0 commit comments