-
-
Notifications
You must be signed in to change notification settings - Fork 713
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added tests for build-newsroom-videos.js (#3075)
Co-authored-by: Ansh Goyal <[email protected]>%0ACo-authored-by: asyncapi-bot <[email protected]>
- Loading branch information
1 parent
315f888
commit bf61566
Showing
5 changed files
with
188 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
const { readFileSync, rmSync, mkdirSync } = require('fs'); | ||
const { resolve } = require('path'); | ||
const { buildNewsroomVideos } = require('../scripts/build-newsroom-videos'); | ||
const { mockApiResponse, expectedResult } = require('./fixtures/newsroomData'); | ||
const fetch = require('node-fetch-2'); | ||
|
||
jest.mock('node-fetch-2', () => jest.fn()); | ||
|
||
describe('buildNewsroomVideos', () => { | ||
const testDir = resolve(__dirname, 'test_config'); | ||
const testFilePath = resolve(testDir, 'newsroom_videos.json'); | ||
|
||
beforeAll(() => { | ||
mkdirSync(testDir, { recursive: true }); | ||
process.env.YOUTUBE_TOKEN = 'testkey'; | ||
}); | ||
|
||
afterAll(() => { | ||
rmSync(testDir, { recursive: true, force: true }); | ||
}); | ||
|
||
beforeEach(() => { | ||
fetch.mockClear(); | ||
}); | ||
|
||
it('should fetch video data and write to file', async () => { | ||
fetch.mockResolvedValue({ | ||
ok: true, | ||
json: jest.fn().mockResolvedValue(mockApiResponse), | ||
}); | ||
|
||
const result = await buildNewsroomVideos(testFilePath); | ||
|
||
const expectedUrl = new URL('https://youtube.googleapis.com/youtube/v3/search'); | ||
expectedUrl.searchParams.set('key', 'testkey'); | ||
expectedUrl.searchParams.set('part', 'snippet'); | ||
expectedUrl.searchParams.set('channelId', 'UCIz9zGwDLbrYQcDKVXdOstQ'); | ||
expectedUrl.searchParams.set('eventType', 'completed'); | ||
expectedUrl.searchParams.set('type', 'video'); | ||
expectedUrl.searchParams.set('order', 'Date'); | ||
expectedUrl.searchParams.set('maxResults', '5'); | ||
|
||
expect(fetch).toHaveBeenCalledWith(expectedUrl.toString()); | ||
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')); | ||
|
||
try { | ||
await buildNewsroomVideos(testFilePath); | ||
} catch (err) { | ||
expect(err.message).toContain('Fetch error'); | ||
} | ||
}); | ||
|
||
it('should handle invalid API response', async () => { | ||
fetch.mockResolvedValue({ | ||
ok: true, | ||
json: jest.fn().mockResolvedValue({}), | ||
}); | ||
|
||
try { | ||
await buildNewsroomVideos(testFilePath); | ||
} catch (err) { | ||
expect(err.message).toContain('Invalid data structure received from YouTube API'); | ||
} | ||
}); | ||
|
||
it('should handle HTTP status code', async () => { | ||
fetch.mockResolvedValue({ | ||
ok: false, | ||
status: 404, | ||
json: jest.fn().mockResolvedValue({}), | ||
}); | ||
|
||
try { | ||
await buildNewsroomVideos(testFilePath); | ||
} catch (err) { | ||
expect(err.message).toContain('HTTP error! with status code: 404'); | ||
} | ||
}); | ||
|
||
it('should handle file write errors', async () => { | ||
fetch.mockResolvedValue({ | ||
ok: true, | ||
json: jest.fn().mockResolvedValue(mockApiResponse), | ||
}); | ||
|
||
const invalidPath = '/invalid_dir/newsroom_videos.json'; | ||
|
||
try { | ||
await buildNewsroomVideos(invalidPath); | ||
} catch (err) { | ||
expect(err.message).toMatch(/ENOENT|EACCES/); | ||
} | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const mockApiResponse = { | ||
items: [ | ||
{ | ||
snippet: { | ||
thumbnails: { | ||
high: { | ||
url: 'https://i.ytimg.com/vi/K7fvKbOfqOg/hqdefault.jpg', | ||
}, | ||
}, | ||
title: 'Developer Experience Working Group, 14:00 UTC Thursday May 23rd 2024', | ||
description: 'Define our vision and plans https://github.com/asyncapi/community/issues/1220.', | ||
}, | ||
id: { | ||
videoId: 'K7fvKbOfqOg', | ||
}, | ||
}, | ||
{ | ||
snippet: { | ||
thumbnails: { | ||
high: { | ||
url: 'https://i.ytimg.com/vi/94SSXX78VCU/hqdefault.jpg', | ||
}, | ||
}, | ||
title: 'Essential Building Blocks Working Group, 18:00 UTC Tuesday May 14th 2024', | ||
description: 'https://github.com/asyncapi/community/issues/1200.', | ||
}, | ||
id: { | ||
videoId: '94SSXX78VCU', | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const expectedResult = JSON.stringify([ | ||
{ | ||
"image_url": "https://i.ytimg.com/vi/K7fvKbOfqOg/hqdefault.jpg", | ||
"title": "Developer Experience Working Group, 14:00 UTC Thursday May 23rd 2024", | ||
"description": "Define our vision and plans https://github.com/asyncapi/community/issues/1220.", | ||
"videoId": "K7fvKbOfqOg" | ||
}, | ||
{ | ||
"image_url": "https://i.ytimg.com/vi/94SSXX78VCU/hqdefault.jpg", | ||
"title": "Essential Building Blocks Working Group, 18:00 UTC Tuesday May 14th 2024", | ||
"description": "https://github.com/asyncapi/community/issues/1200.", | ||
"videoId": "94SSXX78VCU" | ||
}, | ||
], null, ' '); | ||
|
||
module.exports = { mockApiResponse, expectedResult } |