Skip to content

Commit

Permalink
feat: added tests for build-newsroom-videos.js (#3075)
Browse files Browse the repository at this point in the history
Co-authored-by: Ansh Goyal <[email protected]>%0ACo-authored-by: asyncapi-bot <[email protected]>
  • Loading branch information
vishvamsinh28 and asyncapi-bot authored Aug 25, 2024
1 parent 315f888 commit bf61566
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 31 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"node-fetch": "^3.3.2",
"node-fetch-2": "npm:node-fetch@^2.7.0",
"postcss": "^8.4.35",
"prettier": "^3.3.0",
"prettier": "^3.3.3",
"react": "^18",
"react-dom": "^18",
"react-ga": "^3.3.1",
Expand Down
59 changes: 33 additions & 26 deletions scripts/build-newsroom-videos.js
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',
Expand All @@ -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()
module.exports={buildNewsroomVideos}
/* istanbul ignore next */
if (require.main === module) {
buildNewsroomVideos(resolve(__dirname, '../config', 'newsroom_videos.json'))
}

module.exports = { buildNewsroomVideos };
101 changes: 101 additions & 0 deletions tests/build-newsroom-videos.test.js
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/);
}
});

});
49 changes: 49 additions & 0 deletions tests/fixtures/newsroomData.js
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 }

0 comments on commit bf61566

Please sign in to comment.