Skip to content

Commit

Permalink
update test for newsroom videos
Browse files Browse the repository at this point in the history
  • Loading branch information
vishvamsinh28 committed Jul 14, 2024
1 parent bc4eb37 commit 129f6bc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
7 changes: 2 additions & 5 deletions scripts/build-newsroom-videos.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
53 changes: 25 additions & 28 deletions tests/build-newsroom-videos.test.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,46 @@
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);
});

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 () => {
Expand All @@ -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');
});
});

it('should throw an error with incorrect parameters', async () => {
try {
await buildNewsroomVideos('randomePath');
} catch (error) {
expect(error.message).toContain("Failed to build newsroom videos");
}
});
});

0 comments on commit 129f6bc

Please sign in to comment.