Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added tests for build-newsroom-videos.js #3075

Merged
merged 34 commits into from
Aug 25, 2024

Conversation

vishvamsinh28
Copy link
Contributor

This PR adds tests for build-newsroom-videos.js

Copy link

netlify bot commented Jul 5, 2024

Deploy Preview for asyncapi-website ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 8b343cb
🔍 Latest deploy log https://app.netlify.com/sites/asyncapi-website/deploys/66cac8279dce5d0008d4dbc2
😎 Deploy Preview https://deploy-preview-3075--asyncapi-website.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@asyncapi-bot
Copy link
Contributor

asyncapi-bot commented Jul 5, 2024

⚡️ Lighthouse report for the changes in this PR:

Category Score
🔴 Performance 32
🟢 Accessibility 98
🟢 Best practices 92
🟢 SEO 100
🟠 PWA 56

Lighthouse ran on https://deploy-preview-3075--asyncapi-website.netlify.app/

@anshgoyalevil anshgoyalevil added the gsoc This label should be used for issues or discussions related to ideas for Google Summer of Code label Jul 9, 2024
@vishvamsinh28
Copy link
Contributor Author

@anshgoyalevil i should not mock fs module in this PR as well correct? I will update it

@anshgoyalevil
Copy link
Member

Yes. Kindly use actual fs module.

@vishvamsinh28
Copy link
Contributor Author

@anshgoyalevil okay will update it 👍

@sambhavgupta0705
Copy link
Member

@vishvamsinh28 can you please check for conflicts once
I think it is because of node-fetch version change

@vishvamsinh28
Copy link
Contributor Author

@sambhavgupta0705 fixed it

@@ -1,10 +1,9 @@
const { writeFileSync } = require('fs');
const { resolve } = require('path');
const fetch = require('node-fetch-2')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert this one
For now we are trying fixing this

}));

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Error(`HTTP error! status: ${response.status}`);
throw new Error(`HTTP error! with status code: ${response.status}`);

describe('buildNewsroomVideos', () => {
beforeEach(() => {
fetch.mockClear();
process.env.YOUTUBE_TOKEN = 'testkey';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is hardcoded for testing only?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

const testDir = resolve(__dirname, 'test_config');
const testFilePath = resolve(testDir, 'newsroom_videos.json');

jest.mock('node-fetch', () => jest.fn());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to see will this node-fetch work or not

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is working because we are not actually calling it, but instead we are mocking it.

module.exports = { buildNewsroomVideos };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a newline to avoid lint error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

},
], null, ' ');

module.exports = {mockApiResponse,expectedResult}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a new line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@vishvamsinh28
Copy link
Contributor Author

@sambhavgupta0705 @anshgoyalevil Updated the test

}
}

buildNewsroomVideos()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this function call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe by mistake while updating the script, will add it again 👍👍

@anshgoyalevil
Copy link
Member

image

We can aim for 100% statement and branch coverage here.

image

  • Try adding in an extra test case for that uncovered if condition.


async function buildNewsroomVideos(writePath = resolve(__dirname, '../config', 'newsroom_videos.json')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are initializing writhPath here. This would make this branch uncovered, like you saw in the coverage report.

One solution would be to pass this thing as an argument while calling this function, like we did in

website/scripts/index.js

Lines 16 to 18 in 1ab6e4a

await buildCaseStudiesList(
'config/casestudies',
resolve(__dirname, '../config', 'case-studies.json')

@vishvamsinh28
Copy link
Contributor Author

@anshgoyalevil @sambhavgupta0705 updated the test

@vishvamsinh28
Copy link
Contributor Author

@anshgoyalevil @sambhavgupta0705 updated the test

Comment on lines 4 to 24

async function buildNewsroomVideos(writePath) {
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,
}));

if (!response.ok) {
throw new Error(`HTTP error! with status code: ${response.status}`);
}

const data = await response.json();
console.log(data)

if (!data.items || !Array.isArray(data.items)) {
throw new Error('Invalid data structure received from YouTube API');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have you removed the try catch blocks? Error handling should be in place.

Comment on lines 65 to 67
it('should throw an error with incorrect parameters', async () => {
await expect(buildNewsroomVideos('randomPath')).rejects.toThrow('HTTP error! with status code: 404');
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test can be improved since the error you are throwing here isn't related to the randomPath thing. That's basically an issue with creating a file named randomPath, and isn't related to the HTTP call.


const result = await buildNewsroomVideos(testFilePath);

expect(fetch).toHaveBeenCalledWith(expect.stringContaining('https://youtube.googleapis.com/youtube/v3/search?'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other query parameters. Consider testing them also.

For example:

const expectedUrl = new URL('https://youtube.googleapis.com/youtube/v3/search');
expectedUrl.searchParams.set('key', 'testkey');
expectedUrl.searchParams.set('eventType', 'completed');
...etc
expect(fetch).toHaveBeenCalledWith(expectedUrl.toString());

@vishvamsinh28
Copy link
Contributor Author

@anshgoyalevil Updated the test

Screenshot 2024-08-19 at 8 16 55 PM

This part of the script is not covered in the test because adding a test for it would cause the test to fail. This is due to the absence of an API key locally, which would result in errors because it will call actual function from the script. Additionally, running this test during PR checks could potentially modify the newsroom.json file, as the function would be called each time the tests are executed.

anshgoyalevil
anshgoyalevil previously approved these changes Aug 25, 2024
anshgoyalevil
anshgoyalevil previously approved these changes Aug 25, 2024
@anshgoyalevil
Copy link
Member

/rtm

@asyncapi-bot asyncapi-bot merged commit bf61566 into asyncapi:master Aug 25, 2024
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gsoc This label should be used for issues or discussions related to ideas for Google Summer of Code ready-to-merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants