Skip to content

Commit

Permalink
Add initial tests for /github/:project
Browse files Browse the repository at this point in the history
  • Loading branch information
JerryHue committed Apr 6, 2022
1 parent 9fa248e commit 4c0e61d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/api/dependency-discovery/src/__mocks__/dependency-list.js
@@ -1,10 +1,23 @@
let depsList = [];
let depsGitHubIssuesList = {};

module.exports = {
__setMockDepList: (newDepsList) => {
depsList = newDepsList;
},
__setMockDepsGitHubIssuesList: (newDepsGitHubIssuesList) => {
depsGitHubIssuesList = newDepsGitHubIssuesList;
},
getDependencyList: jest.fn().mockImplementation(() => {
return Promise.resolve(depsList);
}),
isPackageDependency: jest.fn().mockImplementation((dependencyName) => {
return Promise.resolve(depsList.includes(dependencyName));
}),
getGitHubIssues: jest.fn().mockImplementation((dependencyName) => {
if (depsGitHubIssuesList[dependencyName]) {
return Promise.resolve(depsGitHubIssuesList[dependencyName]);
}
return Promise.resolve([]);
}),
};
67 changes: 66 additions & 1 deletion src/api/dependency-discovery/test/dependency.test.js
Expand Up @@ -11,10 +11,36 @@ const depsList = [
'@babel/helper-compilation-targets',
'@babel/helper-create-class-features-plugin',
'@babel/helper-create-regexp-features-plugin',
'faker-package',
];

const depsGitHubIssuesList = {
'@babel/code-frame': [
{
htmlUrl: 'https://github.com/babel/babel/issues/123',
title: 'This is a babel-related issue',
body: 'This is urgent!!',
createdAt: '2019-04-24T14:10:34Z',
},
],
'faker-package': [
{
htmlUrl: 'https://github.com/faker-org/faker/issues/112',
title: 'This is a fake issue',
body: 'This is the body of a fake issue',
createdAt: '2018-02-08T20:49:23Z',
},
{
htmlUrl: 'https://github.com/faker-org/faker/issues/113',
title: 'This is a follow up issue from a fake PR',
body: 'This is the body of a fake issue',
createdAt: '2018-01-26T16:51:33Z',
},
],
};

jest.mock('../src/dependency-list');
const { __setMockDepList } = require('../src/dependency-list');
const { __setMockDepList, __setMockDepsGitHubIssuesList } = require('../src/dependency-list');

describe('GET /projects', () => {
beforeEach(() => {
Expand All @@ -28,3 +54,42 @@ describe('GET /projects', () => {
expect(res.body.length).toBe(depsList.length);
});
});

describe('GET /github/:project', () => {
beforeEach(() => {
__setMockDepList(depsList);
__setMockDepsGitHubIssuesList(depsGitHubIssuesList);
});

test('Should return 200 and an object with the GitHub issues', async () => {
const dependencyName = 'faker-package';
const gitHubIssuesForDep = depsGitHubIssuesList[dependencyName];
const res = await request(app).get(`/github/${dependencyName}`);
expect(res.status).toBe(200);
expect(res.body).toEqual(gitHubIssuesForDep);
});

test('Should return 200 and an object with the GitHub issues if dependency name has namespace', async () => {
const dependencyName = '@babel/code-frame';
const gitHubIssuesForDep = depsGitHubIssuesList[dependencyName];
const res = await request(app).get(`/github/${dependencyName}`);
expect(res.status).toBe(200);
console.log(res.body);
expect(res.body).toEqual(gitHubIssuesForDep);
});

test('Should return 404 if dependency does not exist', async () => {
const dependencyName = 'this-is-not-a-dependency';
const res = await request(app).get(`/github/${dependencyName}`);
console.log(res.body);
expect(res.status).toBe(404);
});

test('Should return 200 and no list if dependency does not have GitHub issues', async () => {
const dependencyName = '@apidevtools/json-schema-ref-parser';
const res = await request(app).get(`/github/${dependencyName}`);
console.log(res.body);
expect(res.status).toBe(200);
expect(res.body).toEqual([]);
});
});

0 comments on commit 4c0e61d

Please sign in to comment.