11import { addHeaders } from '../../../../auth/authentication/mock' ;
22
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' ;
414
515import supertest from 'supertest' ;
616import app from '../../../../../src/app' ;
717import { Types } from 'mongoose' ;
818
919describe ( 'BlogDetail by URL route' , ( ) => {
1020 beforeEach ( ( ) => {
11- mockBlogFindByUrl . mockClear ( ) ;
21+ mockBlogCacheFetchByUrl . mockClear ( ) ;
22+ mockBlogCacheSave . mockClear ( ) ;
23+ mockPublishedBlogFindByUrl . mockClear ( ) ;
1224 } ) ;
1325
1426 const request = supertest ( app ) ;
@@ -19,7 +31,7 @@ describe('BlogDetail by URL route', () => {
1931 expect ( response . status ) . toBe ( 400 ) ;
2032 expect ( response . body . message ) . toMatch ( / e n d p o i n t / ) ;
2133 expect ( response . body . message ) . toMatch ( / r e q u i r e d / ) ;
22- expect ( mockBlogFindByUrl ) . not . toBeCalled ( ) ;
34+ expect ( mockPublishedBlogFindByUrl ) . not . toBeCalled ( ) ;
2335 } ) ;
2436
2537 it ( 'Should send error when url endpoint is more that 200 chars' , async ( ) => {
@@ -28,31 +40,60 @@ describe('BlogDetail by URL route', () => {
2840 expect ( response . status ) . toBe ( 400 ) ;
2941 expect ( response . body . message ) . toMatch ( / l e n g t h m u s t / ) ;
3042 expect ( response . body . message ) . toMatch ( / 2 0 0 / ) ;
31- expect ( mockBlogFindByUrl ) . not . toBeCalled ( ) ;
43+ expect ( mockPublishedBlogFindByUrl ) . not . toBeCalled ( ) ;
3244 } ) ;
3345
3446 it ( 'Should send error when blog do not exists for url' , async ( ) => {
3547 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' ) ;
4055 } ) ;
4156
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 ( ) => {
4358 const response = await addHeaders ( request . get ( endpoint ) . query ( { endpoint : BLOG_URL } ) ) ;
4459 expect ( response . status ) . toBe ( 200 ) ;
4560 expect ( response . body . message ) . toMatch ( / s u c c e s s / ) ;
4661 expect ( response . body . data ) . toBeDefined ( ) ;
4762 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 ) ;
5089 } ) ;
5190} ) ;
5291
5392describe ( 'BlogDetail by id route' , ( ) => {
5493 beforeEach ( ( ) => {
55- mockFindInfoWithTextById . mockClear ( ) ;
94+ mockBlogCacheFetchById . mockClear ( ) ;
95+ mockBlogCacheSave . mockClear ( ) ;
96+ mockPublishedBlogFindById . mockClear ( ) ;
5697 } ) ;
5798
5899 const request = supertest ( app ) ;
@@ -62,22 +103,47 @@ describe('BlogDetail by id route', () => {
62103 const response = await addHeaders ( request . get ( endpoint + 'abc' ) ) ;
63104 expect ( response . status ) . toBe ( 400 ) ;
64105 expect ( response . body . message ) . toMatch ( / i n v a l i d / ) ;
65- expect ( mockFindInfoWithTextById ) . not . toBeCalled ( ) ;
106+ expect ( mockPublishedBlogFindById ) . not . toBeCalled ( ) ;
66107 } ) ;
67108
68109 it ( 'Should send error when blog do not exists for id' , async ( ) => {
69110 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 ) ;
73114 } ) ;
74115
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 ( ) => {
76117 const response = await addHeaders ( request . get ( endpoint + BLOG_ID . toHexString ( ) ) ) ;
77118 expect ( response . status ) . toBe ( 200 ) ;
78119 expect ( response . body . message ) . toMatch ( / s u c c e s s / ) ;
79120 expect ( response . body . data ) . toBeDefined ( ) ;
80121 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 ) ;
82148 } ) ;
83149} ) ;
0 commit comments