2017-02-06 13:40:28 -05:00
|
|
|
// Imports
|
2018-05-10 08:56:07 -04:00
|
|
|
import {GithubApi} from '../../lib/common/github-api';
|
2017-02-06 13:40:28 -05:00
|
|
|
import {GithubPullRequests} from '../../lib/common/github-pull-requests';
|
|
|
|
|
|
|
|
// Tests
|
|
|
|
describe('GithubPullRequests', () => {
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
let githubApi: jasmine.SpyObj<GithubApi>;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
githubApi = jasmine.createSpyObj('githubApi', ['post', 'get', 'getPaginated']);
|
|
|
|
});
|
|
|
|
|
2017-02-06 13:40:28 -05:00
|
|
|
describe('constructor()', () => {
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
it('should throw if \'githubOrg\' is missing or empty', () => {
|
|
|
|
expect(() => new GithubPullRequests(githubApi, '', 'bar')).
|
|
|
|
toThrowError('Missing or empty required parameter \'githubOrg\'!');
|
2017-02-06 13:40:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
it('should throw if \'githubRepo\' is missing or empty', () => {
|
|
|
|
expect(() => new GithubPullRequests(githubApi, 'foo', '')).
|
|
|
|
toThrowError('Missing or empty required parameter \'githubRepo\'!');
|
2017-02-06 13:40:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('addComment()', () => {
|
|
|
|
let prs: GithubPullRequests;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2018-05-10 08:56:07 -04:00
|
|
|
prs = new GithubPullRequests(githubApi, 'foo', 'bar');
|
2017-02-06 13:40:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should throw if the PR number is invalid', () => {
|
|
|
|
expect(() => prs.addComment(-1337, 'body')).toThrowError(`Invalid PR number: -1337`);
|
|
|
|
expect(() => prs.addComment(NaN, 'body')).toThrowError(`Invalid PR number: NaN`);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should throw if the comment body is invalid or empty', () => {
|
|
|
|
expect(() => prs.addComment(42, '')).toThrowError(`Invalid or empty comment body: `);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
it('should make a POST request to Github with the correct pathname, params and data', () => {
|
|
|
|
githubApi.post.and.callFake(() => Promise.resolve());
|
2017-02-06 13:40:28 -05:00
|
|
|
prs.addComment(42, 'body');
|
2018-05-10 08:56:07 -04:00
|
|
|
expect(githubApi.post).toHaveBeenCalledWith('/repos/foo/bar/issues/42/comments', null, {body: 'body'});
|
2017-02-06 13:40:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should reject if the request fails', done => {
|
2018-05-10 08:56:07 -04:00
|
|
|
githubApi.post.and.callFake(() => Promise.reject('Test'));
|
2017-02-06 13:40:28 -05:00
|
|
|
prs.addComment(42, 'body').catch(err => {
|
|
|
|
expect(err).toBe('Test');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
it('should resolve with the data from the Github POST', done => {
|
|
|
|
githubApi.post.and.callFake(() => Promise.resolve('Test'));
|
2017-02-06 13:40:28 -05:00
|
|
|
prs.addComment(42, 'body').then(data => {
|
2018-05-10 08:56:07 -04:00
|
|
|
expect(data).toBe('Test');
|
2017-02-06 13:40:28 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-06-18 18:07:27 -04:00
|
|
|
describe('fetch()', () => {
|
|
|
|
let prs: GithubPullRequests;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2018-05-10 08:56:07 -04:00
|
|
|
prs = new GithubPullRequests(githubApi, 'foo', 'bar');
|
2017-06-18 18:07:27 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
it('should make a GET request to GitHub with the correct pathname', () => {
|
2017-06-18 18:07:27 -04:00
|
|
|
prs.fetch(42);
|
2018-05-10 08:56:07 -04:00
|
|
|
expect(githubApi.get).toHaveBeenCalledWith('/repos/foo/bar/issues/42');
|
2017-06-18 18:07:27 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
it('should resolve with the data returned from GitHub', done => {
|
|
|
|
const expected: any = {number: 42};
|
|
|
|
githubApi.get.and.callFake(() => Promise.resolve(expected));
|
|
|
|
prs.fetch(42).then(data => {
|
|
|
|
expect(data).toEqual(expected);
|
|
|
|
done();
|
|
|
|
});
|
2017-06-18 18:07:27 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-02-06 13:40:28 -05:00
|
|
|
describe('fetchAll()', () => {
|
|
|
|
let prs: GithubPullRequests;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2018-05-10 08:56:07 -04:00
|
|
|
prs = new GithubPullRequests(githubApi, 'foo', 'bar');
|
2017-02-28 07:01:20 -05:00
|
|
|
spyOn(console, 'log');
|
2017-02-06 13:40:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-02-28 07:01:20 -05:00
|
|
|
it('should call \'getPaginated()\' with the correct pathname and params', () => {
|
|
|
|
const expectedPathname = '/repos/foo/bar/pulls';
|
2017-02-06 13:40:28 -05:00
|
|
|
|
2017-02-28 07:01:20 -05:00
|
|
|
prs.fetchAll('all');
|
|
|
|
prs.fetchAll('closed');
|
2017-02-06 13:40:28 -05:00
|
|
|
prs.fetchAll('open');
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
expect(githubApi.getPaginated).toHaveBeenCalledTimes(3);
|
|
|
|
expect(githubApi.getPaginated.calls.argsFor(0)).toEqual([expectedPathname, {state: 'all'}]);
|
|
|
|
expect(githubApi.getPaginated.calls.argsFor(1)).toEqual([expectedPathname, {state: 'closed'}]);
|
|
|
|
expect(githubApi.getPaginated.calls.argsFor(2)).toEqual([expectedPathname, {state: 'open'}]);
|
2017-02-06 13:40:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should default to \'all\' if no state is specified', () => {
|
|
|
|
prs.fetchAll();
|
2018-05-10 08:56:07 -04:00
|
|
|
expect(githubApi.getPaginated).toHaveBeenCalledWith('/repos/foo/bar/pulls', {state: 'all'});
|
2017-02-06 13:40:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-02-28 07:01:20 -05:00
|
|
|
it('should forward the value returned by \'getPaginated()\'', () => {
|
2018-05-10 08:56:07 -04:00
|
|
|
githubApi.getPaginated.and.returnValue('Test');
|
2017-06-17 14:03:10 -04:00
|
|
|
expect(prs.fetchAll() as any).toBe('Test');
|
2017-02-06 13:40:28 -05:00
|
|
|
});
|
2018-05-10 08:56:07 -04:00
|
|
|
});
|
2017-02-06 13:40:28 -05:00
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
describe('fetchFiles()', () => {
|
|
|
|
let prs: GithubPullRequests;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
prs = new GithubPullRequests(githubApi, 'foo', 'bar');
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should make a GET request to GitHub with the correct pathname', () => {
|
|
|
|
prs.fetchFiles(42);
|
|
|
|
expect(githubApi.get).toHaveBeenCalledWith('/repos/foo/bar/pulls/42/files');
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should resolve with the data returned from GitHub', done => {
|
|
|
|
const expected: any = [{ sha: 'ABCDE', filename: 'a/b/c'}, { sha: '12345', filename: 'x/y/z' }];
|
|
|
|
githubApi.get.and.callFake(() => Promise.resolve(expected));
|
|
|
|
prs.fetch(42).then(data => {
|
|
|
|
expect(data).toEqual(expected);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-02-06 13:40:28 -05:00
|
|
|
});
|
|
|
|
|
2018-05-10 08:56:07 -04:00
|
|
|
|
2017-02-06 13:40:28 -05:00
|
|
|
});
|