Skip to content

Commit

Permalink
feat: add unit tests for dashboard script (#2062)
Browse files Browse the repository at this point in the history
Co-authored-by: Akshat Nema <[email protected]>%0ACo-authored-by: akshatnema <[email protected]>
  • Loading branch information
reachaadrika and akshatnema authored Aug 14, 2023
1 parent ad69579 commit 7318a17
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 10 deletions.
1 change: 1 addition & 0 deletions cypress.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ module.exports = defineConfig({
framework: "next",
bundler: "webpack",
},

},
});
106 changes: 106 additions & 0 deletions cypress/fixtures/scripts-dashboard.js
Original file line number Diff line number Diff line change
@@ -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
}
}
};
69 changes: 69 additions & 0 deletions cypress/test/scripts/dashboard/build-dashboard.cy.js
Original file line number Diff line number Diff line change
@@ -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;
});
});
});
1 change: 0 additions & 1 deletion scripts/casestudies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
23 changes: 14 additions & 9 deletions scripts/dashboard/build-dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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, ' ')
);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -157,4 +160,6 @@ async function start() {
console.log(e);
}
}
start();
start();

module.exports = { getLabel, monthsSince, mapGoodFirstIssues, getHotDiscussions, getDiscussionByID }

0 comments on commit 7318a17

Please sign in to comment.