Skip to content
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

Merged
merged 34 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
26ff6fc
added tests for build-newsroom-videos.js
vishvamsinh28 Jul 5, 2024
bc4eb37
Merge branch 'master' into newsroomTests
vishvamsinh28 Jul 14, 2024
129f6bc
update test for newsroom videos
vishvamsinh28 Jul 14, 2024
190a5b9
Merge branch 'master' into newsroomTests
vishvamsinh28 Jul 24, 2024
eda22a0
updated tests
vishvamsinh28 Jul 24, 2024
105b4dd
updated tests
vishvamsinh28 Jul 24, 2024
e466d17
Merge branch 'master' into newsroomTests
asyncapi-bot Jul 25, 2024
38d4229
udpated test
vishvamsinh28 Jul 26, 2024
65fc7a9
lint fix
vishvamsinh28 Jul 26, 2024
a819c46
build fix
vishvamsinh28 Jul 26, 2024
0e2a709
lint fix
vishvamsinh28 Jul 26, 2024
a81fd28
remove unwanted changes
vishvamsinh28 Aug 9, 2024
d270385
Merge branch 'master' into newsroomTests
vishvamsinh28 Aug 9, 2024
f97d120
wqfef
vishvamsinh28 Aug 9, 2024
dfcfb14
Merge branch 'master' into newsroomTests
vishvamsinh28 Aug 15, 2024
3aa3d89
script update
vishvamsinh28 Aug 18, 2024
d47fc30
test and script updated
vishvamsinh28 Aug 18, 2024
5757393
test and script updated
vishvamsinh28 Aug 18, 2024
5d3c6de
code format
vishvamsinh28 Aug 18, 2024
01aaa1f
function updated
vishvamsinh28 Aug 18, 2024
6ab9f1d
Merge branch 'master' into newsroomTests
vishvamsinh28 Aug 18, 2024
825ee57
Merge branch 'newsroomTests' of https://github.com/vishvamsinh28/webs…
vishvamsinh28 Aug 18, 2024
c74997c
test updated
vishvamsinh28 Aug 19, 2024
56d07d8
test updated again
vishvamsinh28 Aug 19, 2024
7a78f9e
fefe
vishvamsinh28 Aug 19, 2024
29a89a5
Merge branch 'master' into newsroomTests
vishvamsinh28 Aug 19, 2024
0c05cc6
Merge branch 'master' into newsroomTests
asyncapi-bot Aug 19, 2024
4f4efde
format
vishvamsinh28 Aug 20, 2024
51b02c4
nit: lint fix
anshgoyalevil Aug 25, 2024
84bd83e
Merge branch 'master' into newsroomTests
anshgoyalevil Aug 25, 2024
ea45249
ignore untestable branch
anshgoyalevil Aug 25, 2024
4ec489b
Merge branch 'newsroomTests' of https://github.com/vishvamsinh28/webs…
anshgoyalevil Aug 25, 2024
1f68ed1
Empty Commit
anshgoyalevil Aug 25, 2024
8b343cb
modify prettier lock
anshgoyalevil Aug 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
Copy link
Member

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

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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this function call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 };
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 }
Loading