-
-
Notifications
You must be signed in to change notification settings - Fork 717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: added tests for build-newsroom-videos.js #3075
Changes from all commits
26ff6fc
bc4eb37
129f6bc
190a5b9
eda22a0
105b4dd
e466d17
38d4229
65fc7a9
a819c46
0e2a709
a81fd28
d270385
f97d120
dfcfb14
3aa3d89
d47fc30
5757393
5d3c6de
01aaa1f
6ab9f1d
825ee57
c74997c
56d07d8
7a78f9e
29a89a5
0c05cc6
4f4efde
51b02c4
84bd83e
ea45249
4ec489b
1f68ed1
8b343cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
const { writeFileSync } = require('fs'); | ||
const { resolve } = require('path'); | ||
const fetch = require('node-fetch-2') | ||
async function buildNewsroomVideos() { | ||
const fetch = require('node-fetch-2'); | ||
|
||
async function buildNewsroomVideos(writePath) { | ||
try { | ||
let data; | ||
const response = await fetch('https://youtube.googleapis.com/youtube/v3/search?' + new URLSearchParams({ | ||
key: process.env.YOUTUBE_TOKEN, | ||
part: 'snippet', | ||
|
@@ -13,32 +12,40 @@ async function buildNewsroomVideos() { | |
type: 'video', | ||
order: 'Date', | ||
maxResults: 5, | ||
})) | ||
data = await response.json() | ||
const videoDataItems = data.items.map((video) => { | ||
return { | ||
image_url: video.snippet.thumbnails.high.url, | ||
title: video.snippet.title, | ||
description: video.snippet.description, | ||
videoId: video.id.videoId, | ||
} | ||
}) | ||
const videoData = JSON.stringify(videoDataItems, null, ' '); | ||
console.log('The following are the Newsroom Youtube videos: ', videoData) | ||
|
||
try { | ||
writeFileSync( | ||
resolve(__dirname, '../config', 'newsroom_videos.json'), | ||
videoData | ||
); | ||
} catch (err) { | ||
console.error(err); | ||
})); | ||
|
||
if (!response.ok) { | ||
throw new Error(`HTTP error! with status code: ${response.status}`); | ||
} | ||
|
||
const data = await response.json(); | ||
console.log(data) | ||
|
||
if (!data.items || !Array.isArray(data.items)) { | ||
throw new Error('Invalid data structure received from YouTube API'); | ||
} | ||
|
||
const videoDataItems = data.items.map((video) => ({ | ||
image_url: video.snippet.thumbnails.high.url, | ||
title: video.snippet.title, | ||
description: video.snippet.description, | ||
videoId: video.id.videoId, | ||
})); | ||
|
||
const videoData = JSON.stringify(videoDataItems, null, ' '); | ||
console.log('The following are the Newsroom Youtube videos: ', videoData); | ||
|
||
writeFileSync(writePath, videoData); | ||
|
||
return videoData; | ||
} catch (err) { | ||
console.log(err) | ||
throw new Error(`Failed to build newsroom videos: ${err.message}`); | ||
} | ||
} | ||
|
||
buildNewsroomVideos() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed this function call? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe by mistake while updating the script, will add it again 👍👍 |
||
module.exports={buildNewsroomVideos} | ||
/* istanbul ignore next */ | ||
if (require.main === module) { | ||
buildNewsroomVideos(resolve(__dirname, '../config', 'newsroom_videos.json')) | ||
} | ||
|
||
module.exports = { buildNewsroomVideos }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
const { readFileSync, rmSync, mkdirSync } = require('fs'); | ||
const { resolve } = require('path'); | ||
const { buildNewsroomVideos } = require('../scripts/build-newsroom-videos'); | ||
const { mockApiResponse, expectedResult } = require('./fixtures/newsroomData'); | ||
const fetch = require('node-fetch-2'); | ||
|
||
jest.mock('node-fetch-2', () => jest.fn()); | ||
|
||
describe('buildNewsroomVideos', () => { | ||
const testDir = resolve(__dirname, 'test_config'); | ||
const testFilePath = resolve(testDir, 'newsroom_videos.json'); | ||
|
||
beforeAll(() => { | ||
mkdirSync(testDir, { recursive: true }); | ||
process.env.YOUTUBE_TOKEN = 'testkey'; | ||
}); | ||
|
||
afterAll(() => { | ||
rmSync(testDir, { recursive: true, force: true }); | ||
}); | ||
|
||
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 = '/invalid_dir/newsroom_videos.json'; | ||
|
||
try { | ||
await buildNewsroomVideos(invalidPath); | ||
} catch (err) { | ||
expect(err.message).toMatch(/ENOENT|EACCES/); | ||
} | ||
}); | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const mockApiResponse = { | ||
items: [ | ||
{ | ||
snippet: { | ||
thumbnails: { | ||
high: { | ||
url: 'https://i.ytimg.com/vi/K7fvKbOfqOg/hqdefault.jpg', | ||
}, | ||
}, | ||
title: 'Developer Experience Working Group, 14:00 UTC Thursday May 23rd 2024', | ||
description: 'Define our vision and plans https://github.com/asyncapi/community/issues/1220.', | ||
}, | ||
id: { | ||
videoId: 'K7fvKbOfqOg', | ||
}, | ||
}, | ||
{ | ||
snippet: { | ||
thumbnails: { | ||
high: { | ||
url: 'https://i.ytimg.com/vi/94SSXX78VCU/hqdefault.jpg', | ||
}, | ||
}, | ||
title: 'Essential Building Blocks Working Group, 18:00 UTC Tuesday May 14th 2024', | ||
description: 'https://github.com/asyncapi/community/issues/1200.', | ||
}, | ||
id: { | ||
videoId: '94SSXX78VCU', | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const expectedResult = JSON.stringify([ | ||
{ | ||
"image_url": "https://i.ytimg.com/vi/K7fvKbOfqOg/hqdefault.jpg", | ||
"title": "Developer Experience Working Group, 14:00 UTC Thursday May 23rd 2024", | ||
"description": "Define our vision and plans https://github.com/asyncapi/community/issues/1220.", | ||
"videoId": "K7fvKbOfqOg" | ||
}, | ||
{ | ||
"image_url": "https://i.ytimg.com/vi/94SSXX78VCU/hqdefault.jpg", | ||
"title": "Essential Building Blocks Working Group, 18:00 UTC Tuesday May 14th 2024", | ||
"description": "https://github.com/asyncapi/community/issues/1200.", | ||
"videoId": "94SSXX78VCU" | ||
}, | ||
], null, ' '); | ||
|
||
module.exports = { mockApiResponse, expectedResult } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
revert this one
For now we are trying fixing this