Skip to content

Commit 56988b7

Browse files
Merge pull request #11 from surenpoghosian/ScrapersFactory-tests
[ScrapersFactory-tests] added tests for ScrapersFactory and its products Https and Puppetteer
2 parents 2dbe669 + bb219c2 commit 56988b7

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

.github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313

1414
strategy:
1515
matrix:
16-
node-version: [14, 16, 18, 20]
16+
node-version: [16, 18, 20]
1717

1818
steps:
1919
- name: Checkout repository

src/__tests__/scrapersFactory.test.ts

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import ScrapersFactory from '../ScrapersFactory';
2+
import Https from '../ScrapersFactory/products/Https';
3+
import https from 'https';
4+
import Puppetter from '../ScrapersFactory/products/Puppetter';
5+
import { ScraperType } from '../configs/types';
6+
7+
jest.mock('../db/MongoDBConnection', () => ({
8+
getInstance: jest.fn().mockResolvedValue({
9+
getDb: jest.fn().mockReturnValue({
10+
collection: jest.fn().mockReturnValue({
11+
insertOne: jest.fn().mockResolvedValue({}),
12+
}),
13+
}),
14+
}),
15+
}));
16+
17+
jest.mock('../db/MongoCRUD', () => {
18+
return jest.fn().mockImplementation(() => ({
19+
create: jest.fn().mockResolvedValue({}),
20+
}));
21+
});
22+
23+
describe('ScrapersFactory', () => {
24+
it('should create an instance of Https scraper', () => {
25+
const scraper = ScrapersFactory.createScraper(ScraperType.HTTPS);
26+
expect(scraper).toBeInstanceOf(Https);
27+
});
28+
29+
it('should create an instance of Puppetter scraper', () => {
30+
const scraper = ScrapersFactory.createScraper(ScraperType.PUPPETTER);
31+
expect(scraper).toBeInstanceOf(Puppetter);
32+
});
33+
34+
it('should throw an error for unsupported scraper types', () => {
35+
expect(() => ScrapersFactory.createScraper('UNSUPPORTED_TYPE' as ScraperType))
36+
.toThrow('Unsupported scrape type: UNSUPPORTED_TYPE');
37+
});
38+
});
39+
40+
describe('Https Scraper', () => {
41+
let httpsScraper: Https;
42+
43+
beforeAll(() => {
44+
httpsScraper = new Https();
45+
});
46+
47+
afterAll(() => {
48+
// Clean up any open handles, especially https requests
49+
https.globalAgent.destroy();
50+
});
51+
52+
it('should get HTML content from a given URL', async () => {
53+
const mockHost = 'example.com';
54+
const mockPath = '/';
55+
const htmlContent = await httpsScraper.get(mockHost, mockPath);
56+
expect(htmlContent).toContain('<html>');
57+
});
58+
59+
it('should parse HTML content correctly', () => {
60+
const mockHtml = '<html><body><h1>Test</h1></body></html>';
61+
const rootElement = httpsScraper.parse(mockHtml);
62+
expect(rootElement.querySelector('h1')?.textContent).toBe('Test');
63+
});
64+
65+
it('should save data correctly in MongoDB', async () => {
66+
const mockData = { key: 'value' };
67+
const spySave = jest.spyOn(httpsScraper, 'save').mockImplementation(async () => {});
68+
69+
await httpsScraper.save(mockData);
70+
expect(spySave).toHaveBeenCalledWith(mockData);
71+
});
72+
});
73+
74+
describe('Puppetter Scraper', () => {
75+
let puppetterScraper: Puppetter;
76+
77+
beforeAll(() => {
78+
puppetterScraper = new Puppetter();
79+
});
80+
81+
it('should get HTML content from a given URL using Puppeteer', async () => {
82+
const mockHost = 'https://example.com';
83+
const mockPath = '/';
84+
const htmlContent = await puppetterScraper.get(mockHost, mockPath);
85+
expect(htmlContent).toContain('<html>');
86+
});
87+
88+
it('should parse HTML content correctly', () => {
89+
const mockHtml = '<html><body><h1>Test</h1></body></html>';
90+
const rootElement = puppetterScraper.parse(mockHtml);
91+
expect(rootElement.querySelector('h1')?.textContent).toBe('Test');
92+
});
93+
94+
it('should save data correctly in MongoDB', async () => {
95+
const mockData = { key: 'value' };
96+
const spySave = jest.spyOn(puppetterScraper, 'save').mockImplementation(async () => {});
97+
98+
await puppetterScraper.save(mockData);
99+
expect(spySave).toHaveBeenCalledWith(mockData);
100+
});
101+
});

0 commit comments

Comments
 (0)