forked from asyncapi/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-newsroom-videos.test.js
110 lines (91 loc) · 3.3 KB
/
build-newsroom-videos.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const { readFileSync, removeSync, mkdirpSync, outputFileSync } = require('fs-extra');
const { resolve, join } = require('path');
const fetch = require('node-fetch-2');
const os = require('os');
const { buildNewsroomVideos } = require('../scripts/build-newsroom-videos.ts');
const { mockApiResponse, expectedResult } = require('./fixtures/newsroomData');
jest.mock('node-fetch-2', () => jest.fn());
describe('buildNewsroomVideos', () => {
const testDir = join(os.tmpdir(), 'test_config');
const testFilePath = resolve(testDir, 'newsroom_videos.json');
beforeAll(() => {
mkdirpSync(testDir);
outputFileSync(testFilePath, JSON.stringify({}));
process.env.YOUTUBE_TOKEN = 'testkey';
});
afterAll(() => {
removeSync(testDir);
});
beforeEach(() => {
fetch.mockClear();
});
it('should fetch video data and write to file', async () => {
fetch.mockResolvedValue({
ok: true,
json: jest.fn().mockResolvedValue(mockApiResponse)
});
const result = await buildNewsroomVideos(testFilePath);
const expectedUrl = new URL('https://youtube.googleapis.com/youtube/v3/search');
expectedUrl.searchParams.set('key', 'testkey');
expectedUrl.searchParams.set('part', 'snippet');
expectedUrl.searchParams.set('channelId', 'UCIz9zGwDLbrYQcDKVXdOstQ');
expectedUrl.searchParams.set('eventType', 'completed');
expectedUrl.searchParams.set('type', 'video');
expectedUrl.searchParams.set('order', 'Date');
expectedUrl.searchParams.set('maxResults', '5');
expect(fetch).toHaveBeenCalledWith(expectedUrl.toString());
const response = readFileSync(testFilePath, 'utf8');
expect(response).toEqual(expectedResult);
expect(result).toEqual(expectedResult);
});
it('should handle fetch errors', async () => {
fetch.mockRejectedValue(new Error('Fetch error'));
try {
await buildNewsroomVideos(testFilePath);
} catch (err) {
expect(err.message).toContain('Fetch error');
}
});
it('should handle invalid API response', async () => {
fetch.mockResolvedValue({
ok: true,
json: jest.fn().mockResolvedValue({})
});
try {
await buildNewsroomVideos(testFilePath);
} catch (err) {
expect(err.message).toContain('Invalid data structure received from YouTube API');
}
});
it('should handle HTTP status code', async () => {
fetch.mockResolvedValue({
ok: false,
status: 404,
json: jest.fn().mockResolvedValue({})
});
try {
await buildNewsroomVideos(testFilePath);
} catch (err) {
expect(err.message).toContain('HTTP error! with status code: 404');
}
});
it('should handle file write errors', async () => {
fetch.mockResolvedValue({
ok: true,
json: jest.fn().mockResolvedValue(mockApiResponse)
});
const invalidPath = resolve(os.tmpdir(), 'invalid_dir', 'newsroom_videos.json');
try {
await buildNewsroomVideos(invalidPath);
} catch (err) {
expect(err.message).toMatch(/ENOENT|EACCES/);
}
});
it('should throw an error if YOUTUBE_TOKEN environment variable is not set', async () => {
delete process.env.YOUTUBE_TOKEN;
await expect(buildNewsroomVideos('/path/to/write')).rejects.toThrow(
'YOUTUBE_TOKEN environment variable is required'
);
process.env.YOUTUBE_TOKEN = 'testkey';
});
});