Skip to content

Commit

Permalink
feat: add unit tests for newsroom-videos scripts (#1988)
Browse files Browse the repository at this point in the history
Co-authored-by: akshatnema <[email protected]>%0ACo-authored-by: Akshat Nema <[email protected]>%0ACo-authored-by: akshatnema <[email protected]>
  • Loading branch information
reachaadrika and akshatnema authored Jul 31, 2023
1 parent 4d92a68 commit c175f86
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 17 deletions.
18 changes: 18 additions & 0 deletions cypress/test/mockData/mockData.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"items": [
{
"summary": "Event 1",
"start": {
"dateTime": "2023-07-23T10:00:00Z"
},
"htmlLink": "https://www.example.com/event1"
},
{
"summary": "Event 2",
"start": {
"dateTime": "2023-07-24T15:30:00Z"
},
"htmlLink": "https://www.example.com/event2"
}
]
}
51 changes: 51 additions & 0 deletions cypress/test/scripts/build-newsroom-videos.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { buildNewsroomVideos } from '../../../scripts/build-newsroom-videos';

describe('Newsroom Videos', () => {
// eslint-disable-next-line cypress/no-async-tests
it('fetches and saves newsroom videos', async () => {
// Define the data that the API should return (stubbed response)
const stubbedResponse = {
items: [
{
snippet: {
thumbnails: {
high: {
url: 'https://example.com/image.jpg',
},
},
title: 'Test Video 1',
description: 'This is a test video 1',
},
id: {
videoId: 'videoId1',
},
},
{
snippet: {
thumbnails: {
high: {
url: 'https://example.com/image2.jpg',
},
},
title: 'Test Video 2',
description: 'This is a test video 2',
},
id: {
videoId: 'videoId2',
},
},
],
};

// Intercept the API request and stub the response
cy.intercept('GET', 'https://youtube.googleapis.com/youtube/v3/search*', {
statusCode: 200,
body: stubbedResponse,
}).as('getYoutubeVideos');

// Manually trigger the function
await buildNewsroomVideos().then((videoData) => {
expect(videoData).to.exist;
});
});
});
38 changes: 21 additions & 17 deletions scripts/build-newsroom-videos.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
const { writeFileSync } = require('fs');
const { resolve } = require('path');
const fetch = require('node-fetch')
export async function buildNewsroomVideos() {

async function buildNewsroomVideos () {

try{
try {
let data;
const response = await fetch('https://youtube.googleapis.com/youtube/v3/search?' + new URLSearchParams({
key: process.env.YOUTUBE_TOKEN,
part: 'snippet',
channelId: 'UCIz9zGwDLbrYQcDKVXdOstQ',
eventType: 'completed',
type:'video',
order: 'Date',
maxResults: 5,
key: process.env.YOUTUBE_TOKEN,
part: 'snippet',
channelId: 'UCIz9zGwDLbrYQcDKVXdOstQ',
eventType: 'completed',
type: 'video',
order: 'Date',
maxResults: 5,
}))
data = await response.json()
const videoDataItems = data.items.map((video) =>{
const videoDataItems = data.items.map((video) => {
return {
image_url:video.snippet.thumbnails.high.url,
image_url: video.snippet.thumbnails.high.url,
title: video.snippet.title,
description: video.snippet.description,
videoId: video.id.videoId,
Expand All @@ -27,11 +26,16 @@ async function buildNewsroomVideos () {
const videoData = JSON.stringify(videoDataItems, null, ' ');
console.log('The following are the Newsroom Youtube videos: ', videoData)

writeFileSync(
resolve(__dirname, '../config', 'newsroom_videos.json'),
videoData
);
}catch(err){
try {
writeFileSync(
resolve(__dirname, '../config', 'newsroom_videos.json'),
videoData
);
} catch (err) {
console.error(err);
}
return videoData;
} catch (err) {
console.log(err)
}
}
Expand Down

0 comments on commit c175f86

Please sign in to comment.