Skip to content

Commit

Permalink
newsroom test update
Browse files Browse the repository at this point in the history
  • Loading branch information
vishvamsinh28 committed Oct 18, 2024
1 parent 9c1ef9e commit a1af4f7
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions tests/build-newsroom-videos.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { readFileSync, rmSync, mkdirSync } = require('fs');
const { readFileSync, rmSync, mkdirSync, existsSync } = require('fs');
const { resolve } = require('path');
const { buildNewsroomVideos } = require('../scripts/build-newsroom-videos');
const { mockApiResponse, expectedResult } = require('./fixtures/newsroomData');
Expand All @@ -11,12 +11,16 @@ describe('buildNewsroomVideos', () => {
const testFilePath = resolve(testDir, 'newsroom_videos.json');

beforeAll(() => {
mkdirSync(testDir, { recursive: true });
if (!existsSync(testDir)) {
mkdirSync(testDir, { recursive: true });
}
process.env.YOUTUBE_TOKEN = 'testkey';
});

afterAll(() => {
rmSync(testDir, { recursive: true, force: true });
if (existsSync(testDir)) {
rmSync(testDir, { recursive: true, force: true });
}
});

beforeEach(() => {
Expand All @@ -29,6 +33,10 @@ describe('buildNewsroomVideos', () => {
json: jest.fn().mockResolvedValue(mockApiResponse),
});

if (!existsSync(testDir)) {
mkdirSync(testDir, { recursive: true });
}

const result = await buildNewsroomVideos(testFilePath);

const expectedUrl = new URL('https://youtube.googleapis.com/youtube/v3/search');
Expand All @@ -41,6 +49,7 @@ describe('buildNewsroomVideos', () => {
expectedUrl.searchParams.set('maxResults', '5');

expect(fetch).toHaveBeenCalledWith(expectedUrl.toString());

const response = readFileSync(testFilePath, 'utf8');
expect(response).toEqual(expectedResult);
expect(result).toEqual(expectedResult);
Expand Down Expand Up @@ -97,5 +106,4 @@ describe('buildNewsroomVideos', () => {
expect(err.message).toMatch(/ENOENT|EACCES/);
}
});

});

0 comments on commit a1af4f7

Please sign in to comment.