2017-02-06 13:40:28 -05:00
|
|
|
// Imports
|
|
|
|
import {GithubPullRequests} from '../../lib/common/github-pull-requests';
|
|
|
|
|
|
|
|
// Tests
|
|
|
|
describe('GithubPullRequests', () => {
|
|
|
|
|
|
|
|
describe('constructor()', () => {
|
|
|
|
|
2017-02-28 07:17:20 -05:00
|
|
|
it('should throw if \'githubToken\' is missing or empty', () => {
|
|
|
|
expect(() => new GithubPullRequests('', 'foo/bar')).
|
|
|
|
toThrowError('Missing or empty required parameter \'githubToken\'!');
|
2017-02-06 13:40:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-02-28 07:17:20 -05:00
|
|
|
it('should throw if \'repoSlug\' is missing or empty', () => {
|
|
|
|
expect(() => new GithubPullRequests('12345', '')).
|
|
|
|
toThrowError('Missing or empty required parameter \'repoSlug\'!');
|
2017-02-06 13:40:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('addComment()', () => {
|
|
|
|
let prs: GithubPullRequests;
|
|
|
|
let deferred: {resolve: Function, reject: Function};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2017-02-28 07:17:20 -05:00
|
|
|
prs = new GithubPullRequests('12345', 'foo/bar');
|
2017-02-06 13:40:28 -05:00
|
|
|
|
|
|
|
spyOn(prs, 'post').and.callFake(() => new Promise((resolve, reject) => deferred = {resolve, reject}));
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should return a promise', () => {
|
|
|
|
expect(prs.addComment(42, 'body')).toEqual(jasmine.any(Promise));
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
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: `);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should call \'post()\' with the correct pathname, params and data', () => {
|
|
|
|
prs.addComment(42, 'body');
|
|
|
|
|
|
|
|
expect(prs.post).toHaveBeenCalledWith('/repos/foo/bar/issues/42/comments', null, {body: 'body'});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should reject if the request fails', done => {
|
|
|
|
prs.addComment(42, 'body').catch(err => {
|
|
|
|
expect(err).toBe('Test');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
deferred.reject('Test');
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should resolve with the returned response', done => {
|
|
|
|
prs.addComment(42, 'body').then(data => {
|
2017-06-17 14:03:10 -04:00
|
|
|
expect(data as any).toBe('Test');
|
2017-02-06 13:40:28 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
deferred.resolve('Test');
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-06-18 18:07:27 -04:00
|
|
|
describe('fetch()', () => {
|
|
|
|
let prs: GithubPullRequests;
|
|
|
|
let prsGetSpy: jasmine.Spy;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
prs = new GithubPullRequests('12345', 'foo/bar');
|
|
|
|
prsGetSpy = spyOn(prs as any, 'get');
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should call \'get()\' with the correct pathname', () => {
|
|
|
|
prs.fetch(42);
|
|
|
|
expect(prsGetSpy).toHaveBeenCalledWith('/repos/foo/bar/issues/42');
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should forward the value returned by \'get()\'', () => {
|
|
|
|
prsGetSpy.and.returnValue('Test');
|
|
|
|
expect(prs.fetch(42) as any).toBe('Test');
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-02-06 13:40:28 -05:00
|
|
|
describe('fetchAll()', () => {
|
|
|
|
let prs: GithubPullRequests;
|
2017-02-28 07:01:20 -05:00
|
|
|
let prsGetPaginatedSpy: jasmine.Spy;
|
2017-02-06 13:40:28 -05:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2017-02-28 07:17:20 -05:00
|
|
|
prs = new GithubPullRequests('12345', 'foo/bar');
|
2017-02-28 07:01:20 -05:00
|
|
|
prsGetPaginatedSpy = spyOn(prs as any, 'getPaginated');
|
|
|
|
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');
|
|
|
|
|
2017-02-28 07:01:20 -05:00
|
|
|
expect(prsGetPaginatedSpy).toHaveBeenCalledTimes(3);
|
|
|
|
expect(prsGetPaginatedSpy.calls.argsFor(0)).toEqual([expectedPathname, {state: 'all'}]);
|
|
|
|
expect(prsGetPaginatedSpy.calls.argsFor(1)).toEqual([expectedPathname, {state: 'closed'}]);
|
|
|
|
expect(prsGetPaginatedSpy.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();
|
2017-02-28 07:01:20 -05:00
|
|
|
expect(prsGetPaginatedSpy).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()\'', () => {
|
|
|
|
prsGetPaginatedSpy.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
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|