Skip to content

Commit 59a0bc0

Browse files
committed
add blogdetail route tests
1 parent afc02be commit 59a0bc0

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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(/endpoint/);
24+
expect(response.body.message).toMatch(/required/);
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(/length must/);
35+
expect(response.body.message).toMatch(/200/);
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(/do not exists/);
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(/success/);
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(/invalid/);
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(/do not exists/);
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(/success/);
93+
expect(response.body.data).toBeDefined();
94+
expect(response.body.data).toHaveProperty('_id');
95+
expect(mockFindInfoWithTextById).toBeCalledTimes(1);
96+
});
97+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { IBlog } from '../../../../../src/database/model/Blog';
2+
import { Types } from 'mongoose';
3+
4+
jest.unmock('../../../../../src/database/repository/BlogRepo');
5+
6+
export const BLOG_ID = new Types.ObjectId();
7+
export const BLOG_URL = 'abc';
8+
9+
export const mockBlogFindByUrl = jest.fn(async (blogUrl: string): Promise<IBlog> => {
10+
if (blogUrl === BLOG_URL) return <IBlog>{
11+
_id: BLOG_ID,
12+
blogUrl: blogUrl
13+
};
14+
return null;
15+
});
16+
17+
export const mockFindInfoWithTextById = jest.fn(async (id: Types.ObjectId): Promise<IBlog> => {
18+
if (BLOG_ID.equals(id)) return <IBlog>{
19+
_id: BLOG_ID,
20+
blogUrl: BLOG_URL
21+
};
22+
return null;
23+
});
24+
25+
jest.mock('../../../../../src/database/repository/BlogRepo', () => ({
26+
get findByUrl() { return mockBlogFindByUrl; },
27+
get findInfoWithTextById() { return mockFindInfoWithTextById; }
28+
}));

0 commit comments

Comments
 (0)