diff --git a/scripts/build-newsroom-videos.js b/scripts/build-newsroom-videos.js index fc5ab6e0e94c..8ce4dc9d320d 100644 --- a/scripts/build-newsroom-videos.js +++ b/scripts/build-newsroom-videos.js @@ -2,7 +2,7 @@ const { writeFileSync } = require('fs'); const { resolve } = require('path'); const fetch = require('node-fetch'); -async function buildNewsroomVideos() { +async function buildNewsroomVideos(writePath = resolve(__dirname, '../config', 'newsroom_videos.json')) { try { const response = await fetch('https://youtube.googleapis.com/youtube/v3/search?' + new URLSearchParams({ key: process.env.YOUTUBE_TOKEN, @@ -34,10 +34,7 @@ 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 - ); + writeFileSync(writePath,videoData); return videoData; } catch (err) { diff --git a/tests/build-newsroom-videos.test.js b/tests/build-newsroom-videos.test.js index e9830a4e038d..7a086b453471 100644 --- a/tests/build-newsroom-videos.test.js +++ b/tests/build-newsroom-videos.test.js @@ -1,35 +1,38 @@ const { buildNewsroomVideos } = require('../scripts/build-newsroom-videos'); -const { writeFileSync } = require('fs'); const fetch = require('node-fetch'); const { resolve } = require('path'); +const { mkdirSync, readFileSync, rmSync } = require('fs'); +const { mockApiResponse, expectedResult } = require('./fixtures/newsroomData'); -const { mockApiResponse,expectedResult } = require('./fixtures/newsroomData') - -const resolvedPath = resolve(__dirname, '../config', 'newsroom_videos.json'); +const testDir = resolve(__dirname, 'test_config'); +const testFilePath = resolve(testDir, 'newsroom_videos.json'); jest.mock('node-fetch', () => jest.fn()); -jest.mock('fs', () => ({ - writeFileSync: jest.fn(), -})); describe('buildNewsroomVideos', () => { beforeEach(() => { fetch.mockClear(); - writeFileSync.mockClear(); process.env.YOUTUBE_TOKEN = 'testkey'; + + mkdirSync(testDir, { recursive: true }); }); - it('should fetch video data and write to file', async () => { + afterEach(() => { + rmSync(testDir, { recursive: true, force: true }); + }); + it('should fetch video data and write to file', async () => { fetch.mockResolvedValue({ ok: true, json: jest.fn().mockResolvedValue(mockApiResponse), }); - const result = await buildNewsroomVideos(); + const result = await buildNewsroomVideos(testFilePath); expect(fetch).toHaveBeenCalledWith(expect.stringContaining('https://youtube.googleapis.com/youtube/v3/search?')); - expect(writeFileSync).toHaveBeenCalledWith(resolvedPath,expectedResult); + + const response = readFileSync(testFilePath, 'utf8'); + expect(response).toEqual(expectedResult); expect(result).toEqual(expectedResult); }); @@ -37,21 +40,7 @@ describe('buildNewsroomVideos', () => { it('should handle fetch errors', async () => { fetch.mockRejectedValue(new Error('Fetch error')); - await expect(buildNewsroomVideos()).rejects.toThrow('Failed to build newsroom videos: Fetch error'); - }); - - it('should handle file write errors', async () => { - - fetch.mockResolvedValue({ - ok: true, - json: jest.fn().mockResolvedValue(mockApiResponse), - }); - - writeFileSync.mockImplementation(() => { - throw new Error('Write error'); - }); - - await expect(buildNewsroomVideos()).rejects.toThrow('Failed to build newsroom videos: Write error'); + await expect(buildNewsroomVideos(testFilePath)).rejects.toThrow('Failed to build newsroom videos: Fetch error'); }); it('should handle invalid API response', async () => { @@ -60,6 +49,14 @@ describe('buildNewsroomVideos', () => { json: jest.fn().mockResolvedValue({}), }); - await expect(buildNewsroomVideos()).rejects.toThrow('Failed to build newsroom videos: Invalid data structure received from YouTube API'); + await expect(buildNewsroomVideos(testFilePath)).rejects.toThrow('Failed to build newsroom videos: Invalid data structure received from YouTube API'); }); -}); \ No newline at end of file + + it('should throw an error with incorrect parameters', async () => { + try { + await buildNewsroomVideos('randomePath'); + } catch (error) { + expect(error.message).toContain("Failed to build newsroom videos"); + } + }); +});