From 7318a17caf6302f0ba418b005307c948b0e9ca11 Mon Sep 17 00:00:00 2001 From: Aadrika Bhargava <64789514+reachaadrika@users.noreply.github.com> Date: Mon, 14 Aug 2023 15:11:29 +0530 Subject: [PATCH] feat: add unit tests for dashboard script (#2062) Co-authored-by: Akshat Nema <76521428+akshatnema@users.noreply.github.com>%0ACo-authored-by: akshatnema --- cypress.config.js | 1 + cypress/fixtures/scripts-dashboard.js | 106 ++++++++++++++++++ .../scripts/dashboard/build-dashboard.cy.js | 69 ++++++++++++ scripts/casestudies/index.js | 1 - scripts/dashboard/build-dashboard.js | 23 ++-- 5 files changed, 190 insertions(+), 10 deletions(-) create mode 100644 cypress/fixtures/scripts-dashboard.js create mode 100644 cypress/test/scripts/dashboard/build-dashboard.cy.js diff --git a/cypress.config.js b/cypress.config.js index 4af0ca6de59..dd3723acd94 100644 --- a/cypress.config.js +++ b/cypress.config.js @@ -6,5 +6,6 @@ module.exports = defineConfig({ framework: "next", bundler: "webpack", }, + }, }); diff --git a/cypress/fixtures/scripts-dashboard.js b/cypress/fixtures/scripts-dashboard.js new file mode 100644 index 00000000000..df2251805cd --- /dev/null +++ b/cypress/fixtures/scripts-dashboard.js @@ -0,0 +1,106 @@ +export const mockIssue = { + labels: { + nodes: [ + { name: 'category/bug' }, + { name: 'status/in-progress' }, + { name: 'priority/high' } + ] + } +}; + +export const mockIssues = [ + { + "id": 1, + "title": "Sample Issue 1", + "assignees": { "totalCount": 1 }, + "resourcePath": "/sample/issue/1", + "repository": { "name": "sample-repo" }, + "author": { "login": "user1" }, + "labels": { + "nodes": [ + { "name": "bug" } + ] + } + }, + { + "id": 2, + "title": "Sample Issue 2", + "assignees": { "totalCount": 0 }, + "resourcePath": "/sample/issue/2", + "repository": { "name": "sample-repo" }, + "author": { "login": "user2" }, + "labels": { + "nodes": [ + { "name": "good first issue" }, + { "name": "enhancement" } + ] + } + } + // Add more sample issues as needed +] + + +// expected output +export const expectedOutput = [ + { + "area": "Unknown", + "author": "user1", + "id": 1, + "isAssigned": true, + "labels": [ + { + "name": "bug" + } + ], + "repo": "asyncapi/sample-repo", + "resourcePath": "/sample/issue/1", + "title": "Sample Issue 1" + }, + { + "area": "Unknown", + "author": "user2", + "id": 2, + "isAssigned": false, + "labels": [ + { + "name": "enhancement" + } + ], + "repo": "asyncapi/sample-repo", + "resourcePath": "/sample/issue/2", + "title": "Sample Issue 2" + } +] + +export const mockData = { + isPr: true, + discussion: { + pullRequest: { + "id": "PR_kwDOBW5R_c5TUuUu", + "isPR": true, + "isAssigned": false, + "title": "chore(blog): adding 3 New Articles as Blog Posts", + "author": "ivangsa", + "resourcePath": "/asyncapi/website/pull/1805", + "repo": "asyncapi/website", + "labels": [], + "score": 20.67657038994663 + }, + issue: { + "id": "MDU6SXNzdWU5OTMxODc5ODM=", + "isPR": false, + "isAssigned": false, + "title": "Proposal to allow defining schema format other than default one (AsyncAPI Schema)", + "author": "magicmatatjahu", + "resourcePath": "/asyncapi/spec/issues/622", + "repo": "asyncapi/spec", + "labels": [ + { + "name": "💭 Strawman (RFC 0)", + "color": "C2E0C6" + } + ], + "score": 22.68679251119144 + } + } +}; \ No newline at end of file diff --git a/cypress/test/scripts/dashboard/build-dashboard.cy.js b/cypress/test/scripts/dashboard/build-dashboard.cy.js new file mode 100644 index 00000000000..cbcee3e09da --- /dev/null +++ b/cypress/test/scripts/dashboard/build-dashboard.cy.js @@ -0,0 +1,69 @@ +/* eslint-disable cypress/no-async-tests */ +import { getLabel, monthsSince, mapGoodFirstIssues, getDiscussionByID } from "../../../../scripts/dashboard/build-dashboard"; +import Queries from '../../../../scripts/dashboard/issue-queries' +import { mockIssue, mockIssues, expectedOutput, mockData } from "../../../fixtures/scripts-dashboard"; +describe('getLabel function', () => { + + it('returns the correct label name when filter matches', () => { + cy.wrap(mockIssue).then((issue) => { + const filter = 'status/'; + const expectedResult = 'in-progress'; + const actual = getLabel(issue, filter); + cy.wrap(actual).should('equal', expectedResult); + }); + }); +}); + +describe('monthsSince function', () => { + // Define some sample dates and expected results + const today = new Date(); + const oneMonthAgo = new Date(today.getFullYear(), today.getMonth() - 1, today.getDate()); + const twoMonthsAgo = new Date(today.getFullYear(), today.getMonth() - 2, today.getDate()); + const threeMonthsAgo = new Date(today.getFullYear(), today.getMonth() - 3, today.getDate()); + + // Write the test cases + it('should return 0 for the same date', () => { + expect(monthsSince(today)).to.equal(0); + }); + + it('should return 1 for one month ago', () => { + expect(monthsSince(oneMonthAgo)).to.equal(1); + }); + + it('should return 2 for two months ago', () => { + expect(monthsSince(twoMonthsAgo)).to.equal(2); + }); + + it('should return 3 for three months ago', () => { + expect(monthsSince(threeMonthsAgo)).to.equal(3); + }); +}); + +describe('mapGoodFirstIssues function', () => { + it('should map the issues correctly', async () => { + // Call the function with the mock data + const result = await mapGoodFirstIssues(mockIssues); + expect(result).to.deep.equal(expectedOutput); + }); +}); + +describe('getDiscussionsById function', () => { + it('should fetch a discussion (pull request or issue)', async () => { + // Stub the GraphQL request with mock data + const isPR = true; + /** + * if IsPr == true then we get Pull request if false we get issue + * for current situation we will be getting pull request + */ + cy.intercept('POST', 'https://api.github.com/graphql', (req) => { + if (req.body.operationName === Queries.pullRequestById && isPR === true) { + req.reply({ body: { data: { pullRequest: mockData.discussion.pullRequest } } }); + } else if (req.body.operationName === Queries.issueById) { + req.reply({ body: { data: { issue: mockData.discussion.issue } } }); + } + }).as('graphqlRequest'); + await getDiscussionByID(mockData.isPr).then((result) => { + expect(result).to.exist; + }); + }); +}); diff --git a/scripts/casestudies/index.js b/scripts/casestudies/index.js index 6a1a1f20e48..f0ac45bc3a7 100644 --- a/scripts/casestudies/index.js +++ b/scripts/casestudies/index.js @@ -3,7 +3,6 @@ const { convertToJson } = require('../utils'); const { resolve } = require('path'); const dirWithCaseStudy = 'config/casestudies'; - module.exports = async function buildCaseStudiesList() { let files = await readdir(dirWithCaseStudy); let caseStudiesList = []; diff --git a/scripts/dashboard/build-dashboard.js b/scripts/dashboard/build-dashboard.js index be9c1abc629..40146128876 100644 --- a/scripts/dashboard/build-dashboard.js +++ b/scripts/dashboard/build-dashboard.js @@ -24,11 +24,11 @@ async function getHotDiscussions(discussions) { const finalInteractionsCount = isPR ? interactionsCount + - discussion.reviews.totalCount + - discussion.reviews.nodes.reduce( - (acc, curr) => acc + curr.comments.totalCount, - 0 - ) + discussion.reviews.totalCount + + discussion.reviews.nodes.reduce( + (acc, curr) => acc + curr.comments.totalCount, + 0 + ) : interactionsCount; return { id: discussion.id, @@ -59,7 +59,7 @@ async function getHotDiscussions(discussions) { } async function writeToFile(content) { writeFileSync( - resolve(__dirname,'..', '..', 'dashboard.json'), + resolve(__dirname, '..', '..', 'dashboard.json'), JSON.stringify(content, null, ' ') ); } @@ -129,12 +129,15 @@ async function getDiscussions(query, pageSize, endCursor = null) { } async function getDiscussionByID(isPR, id) { try { - return await graphql(isPR ? Queries.pullRequestById : Queries.issueById, { + let result = await graphql(isPR ? Queries.pullRequestById : Queries.issueById, { id, headers: { authorization: `token ${process.env.GITHUB_TOKEN}`, }, - }); + + } + ); + return result; } catch (e) { console.error(e); } @@ -157,4 +160,6 @@ async function start() { console.log(e); } } -start(); \ No newline at end of file +start(); + +module.exports = { getLabel, monthsSince, mapGoodFirstIssues, getHotDiscussions, getDiscussionByID } \ No newline at end of file