From c175f86aedca6157999ddd93b300a005a2086272 Mon Sep 17 00:00:00 2001 From: Aadrika Bhargava <64789514+reachaadrika@users.noreply.github.com> Date: Tue, 1 Aug 2023 02:10:59 +0530 Subject: [PATCH] feat: add unit tests for newsroom-videos scripts (#1988) Co-authored-by: akshatnema <20bcs022@iiitdmj.ac.in>%0ACo-authored-by: Akshat Nema <76521428+akshatnema@users.noreply.github.com>%0ACo-authored-by: akshatnema --- cypress/test/mockData/mockData.json | 18 +++++++ .../test/scripts/build-newsroom-videos.cy.js | 51 +++++++++++++++++++ scripts/build-newsroom-videos.js | 38 +++++++------- 3 files changed, 90 insertions(+), 17 deletions(-) create mode 100644 cypress/test/mockData/mockData.json create mode 100644 cypress/test/scripts/build-newsroom-videos.cy.js diff --git a/cypress/test/mockData/mockData.json b/cypress/test/mockData/mockData.json new file mode 100644 index 00000000000..139b01fde36 --- /dev/null +++ b/cypress/test/mockData/mockData.json @@ -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" + } + ] +} \ No newline at end of file diff --git a/cypress/test/scripts/build-newsroom-videos.cy.js b/cypress/test/scripts/build-newsroom-videos.cy.js new file mode 100644 index 00000000000..34ceab2af87 --- /dev/null +++ b/cypress/test/scripts/build-newsroom-videos.cy.js @@ -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; + }); + }); +}); \ No newline at end of file diff --git a/scripts/build-newsroom-videos.js b/scripts/build-newsroom-videos.js index 99bb7ddcef7..2b4b4dac4cf 100644 --- a/scripts/build-newsroom-videos.js +++ b/scripts/build-newsroom-videos.js @@ -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, @@ -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) } }