-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
87 lines (72 loc) · 3.04 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Import necessary testing libraries and the function to be tested
import { fetchAndDisplayRSSLinks } from '/Users/bahito/Library/Mobile Documents/com~apple~CloudDocs/Developer/rss_feed/script.js';
import { jest } from '@jest/globals';
// Mock the dependencies
jest.mock('node-fetch', () => require('fetch-mock-jest').sandbox());
const fetchMock = require('node-fetch');
// Helper functions
const mockFetchHTML = (url, html) => {
fetchMock.mock(url, { body: html, status: 200 });
};
const mockFetchHTMLError = (url, statusCode) => {
fetchMock.mock(url, { status: statusCode });
};
// Test: Successful RSS link fetch and display using the first proxy
test('fetchAndDisplayRSSLinks successfully fetches and displays RSS links using the first proxy', async () => {
// Arrange
const url = 'https://example.com';
const encodedUrl = encodeURIComponent(url);
const proxyUrl = 'https://cors-anywhere.herokuapp.com/';
const fullUrl = proxyUrl + encodedUrl;
const htmlContent = '<html><head><link rel="alternate" type="application/rss+xml" href="https://example.com/feed.xml"></head></html>';
const expectedRSSUrls = ['https://example.com/feed.xml'];
mockFetchHTML(fullUrl, htmlContent);
const displayRSSUrls = jest.fn();
// Act
await fetchAndDisplayRSSLinks(url);
// Assert
expect(displayRSSUrls).toHaveBeenCalledWith(expectedRSSUrls);
});
// Test: Handling fetch errors and trying next proxy
test('fetchAndDisplayRSSLinks handles fetch error and tries the next proxy', async () => {
// Arrange
const url = 'https://example.com';
const encodedUrl = encodeURIComponent(url);
const firstProxyUrl = 'https://cors-anywhere.herokuapp.com/';
const secondProxyUrl = 'https://cors.bridged.cc/';
const firstFullUrl = firstProxyUrl + encodedUrl;
const secondFullUrl = secondProxyUrl + encodedUrl;
const htmlContent = '<html><head><link rel="alternate" type="application/rss+xml" href="https://example.com/feed.xml"></head></html>';
const expectedRSSUrls = ['https://example.com/feed.xml'];
mockFetchHTMLError(firstFullUrl, 500);
mockFetchHTML(secondFullUrl, htmlContent);
const displayRSSUrls = jest.fn();
// Act
await fetchAndDisplayRSSLinks(url);
// Assert
expect(displayRSSUrls).toHaveBeenCalledWith(expectedRSSUrls);
});
// Test: All proxies fail
test('fetchAndDisplayRSSLinks logs error when all proxies fail', async () => {
// Arrange
const url = 'https://example.com';
const encodedUrl = encodeURIComponent(url);
const proxyUrls = [
'https://cors-anywhere.herokuapp.com/',
'https://cors.bridged.cc/',
'http://localhost:8080/proxy?url='
];
proxyUrls.forEach(proxyUrl => {
const fullUrl = proxyUrl + encodedUrl;
mockFetchHTMLError(fullUrl, 500);
});
const consoleError = jest.spyOn(console, 'error');
// Act
await fetchAndDisplayRSSLinks(url);
// Assert
expect(consoleError).toHaveBeenCalledTimes(proxyUrls.length);
});
// Reset fetch mocks after each test
afterEach(() => {
fetchMock.reset();
});