Skip to content

Commit

Permalink
Merge branch 'release/1.3.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovic-gonthier committed Sep 6, 2017
2 parents e22cbfb + 98e96a2 commit de71005
Show file tree
Hide file tree
Showing 10 changed files with 790 additions and 562 deletions.
197 changes: 112 additions & 85 deletions __tests__/common/github/fetcher.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import '../../../config';
import config from '../../../config';
import formatter from '../../../common/github/url-formatter';
import request from '../../../common/github/request';

Expand All @@ -15,29 +15,17 @@ import {
} from '../../../common/github/fetcher';

jest.mock('../../../common/github/url-formatter', () => jest.fn());
jest.mock('../../../common/github/request', () => jest.fn());
jest.mock('../../../config', () => ({
get: jest.fn((key) => {
switch (key) {
case 'snowshoe.display_pr_title':
return true;
default: return '';
}
}),
}));

const stubs = {
jest.mock('../../../common/github/request', () => ({
call: jest.fn(),
paginate: jest.fn(),
};
}));
jest.mock('../../../config', () => ({
get: jest.fn(),
}));

describe('fetcher', () => {
beforeEach(() => {
formatter.mockReset();
stubs.call.mockReset();
stubs.paginate.mockReset();

request.mockImplementation(() => stubs);
jest.resetAllMocks();
});

describe('.organizations()', () => {
Expand All @@ -46,7 +34,7 @@ describe('fetcher', () => {

formatter
.mockImplementationOnce(() => 'https://api.github.com/user/orgs?per_page=100');
stubs.paginate
request.paginate
.mockImplementationOnce(() => Promise.resolve(object));

return organizations('test_token')
Expand All @@ -55,21 +43,25 @@ describe('fetcher', () => {
.toEqual({ json: { test: 'succeed' } });
expect(formatter)
.toBeCalledWith('/user/orgs', { per_page: 100 });
expect(stubs.paginate)
.toBeCalledWith('https://api.github.com/user/orgs?per_page=100', 'organization');
expect(request.paginate)
.toBeCalledWith(
'https://api.github.com/user/orgs?per_page=100',
'organization',
'test_token'
);
});
});

it('should reject the promise on error', () => {
formatter
.mockImplementationOnce(() => 'https://api.github.com/user/orgs?per_page=100');
stubs.paginate
request.paginate
.mockImplementationOnce(() => Promise.reject(new Error('Pagination error')));

return new Promise((resolve, reject) => {
organizations('test_token')
.then(() => reject(Error('Promise should have been rejected')))
.catch(resolve);
.then(() => reject(Error('Promise should have been rejected')))
.catch(resolve);
});
});
});
Expand All @@ -80,7 +72,7 @@ describe('fetcher', () => {

formatter
.mockImplementationOnce(() => 'https://api.github.com/orgs/snowshoe/teams?per_page=100');
stubs.paginate
request.paginate
.mockImplementationOnce(() => Promise.resolve(object));

return teams('test_token', 'snowshoe')
Expand All @@ -90,15 +82,19 @@ describe('fetcher', () => {

expect(formatter)
.toBeCalledWith('orgs/snowshoe/teams', { per_page: 100 });
expect(stubs.paginate)
.toBeCalledWith('https://api.github.com/orgs/snowshoe/teams?per_page=100', 'team');
expect(request.paginate)
.toBeCalledWith(
'https://api.github.com/orgs/snowshoe/teams?per_page=100',
'team',
'test_token'
);
});
});

it('should reject the promise on error', () => {
formatter
.mockImplementationOnce(() => 'https://api.github.com/orgs/snowshoe/teams?per_page=100');
stubs.paginate
request.paginate
.mockImplementationOnce(() => Promise.reject(new Error('Pagination error')));

return new Promise((resolve, reject) => {
Expand All @@ -115,7 +111,7 @@ describe('fetcher', () => {

formatter
.mockImplementationOnce(() => 'https://api.github.com/user/repos?per_page=100');
stubs.paginate
request.paginate
.mockImplementationOnce(() => Promise.resolve(object));

return repositories('test_token', 'user/repos')
Expand All @@ -125,14 +121,18 @@ describe('fetcher', () => {

expect(formatter)
.toBeCalledWith('user/repos', { per_page: 100 });
expect(stubs.paginate)
.toBeCalledWith('https://api.github.com/user/repos?per_page=100', 'repository');
expect(request.paginate)
.toBeCalledWith(
'https://api.github.com/user/repos?per_page=100',
'repository',
'test_token'
);
});
});
it('should reject the promise on error', () => {
formatter
.mockImplementationOnce(() => 'https://api.github.com/user/repos?per_page=100');
stubs.paginate
request.paginate
.mockImplementationOnce(() => Promise.reject(new Error('Pagination error')));

return new Promise((resolve, reject) => {
Expand All @@ -144,6 +144,16 @@ describe('fetcher', () => {
});

describe('.pulls()', () => {
beforeEach(() => {
config.get.mockImplementation((key) => {
switch (key) {
case 'snowshoe.display_pr_title':
return true;
default: return '';
}
});
});

it('should returns a list of pull request', () => {
const object = fixtures.pulls_simple;
const repository = {
Expand All @@ -152,21 +162,26 @@ describe('fetcher', () => {

formatter
.mockImplementationOnce(() => 'https://api.github.com/repos/ludovic-gonthier/snowshoe/pulls?per_page=100');
stubs.paginate
request.paginate
.mockImplementationOnce(() => Promise.resolve(object));


return pulls('test_token', repository.pulls_url)
.then((data) => {
expect(data)
.toEqual(object);

expect(formatter)
.toBeCalledWith(
'https://api.github.com/repos/ludovic-gonthier/snowshoe/pulls{/number}',
{ per_page: 100 }
);
expect(stubs.paginate)
.toBeCalledWith('https://api.github.com/repos/ludovic-gonthier/snowshoe/pulls?per_page=100', 'pull');
'https://api.github.com/repos/ludovic-gonthier/snowshoe/pulls{/number}',
{ per_page: 100 }
);
expect(request.paginate)
.toBeCalledWith(
'https://api.github.com/repos/ludovic-gonthier/snowshoe/pulls?per_page=100',
'pull',
'test_token'
);
});
});

Expand Down Expand Up @@ -199,21 +214,25 @@ describe('fetcher', () => {

formatter
.mockImplementationOnce(() => 'https://api.github.com/repos/ludovic-gonthier/snowshoe/pulls?per_page=100');
stubs.paginate
request.paginate
.mockImplementationOnce(() => Promise.resolve(object));

pulls('test_token', repository.pulls_url)
return pulls('test_token', repository.pulls_url)
.then((data) => {
expect(data)
.toEqual(expected);

expect(formatter)
.toBeCalledWith(
'https://api.github.com/repos/ludovic-gonthier/snowshoe/pulls{/number}',
{ per_page: 100 }
);
expect(stubs.paginate)
.toBeCalledWith('https://api.github.com/repos/ludovic-gonthier/snowshoe/pulls?per_page=100', 'pull');
'https://api.github.com/repos/ludovic-gonthier/snowshoe/pulls{/number}',
{ per_page: 100 }
);
expect(request.paginate)
.toBeCalledWith(
'https://api.github.com/repos/ludovic-gonthier/snowshoe/pulls?per_page=100',
'pull',
'test_token'
);
});
});
it('should reject the promise on error', () => {
Expand All @@ -223,7 +242,7 @@ describe('fetcher', () => {

formatter
.mockImplementationOnce(() => 'https://api.github.com/repos/ludovic-gonthier/snowshoe/pulls?per_page=100');
stubs.paginate
request.paginate
.mockImplementationOnce(() => Promise.reject(new Error('Pagination error')));

return new Promise((resolve, reject) => {
Expand All @@ -243,7 +262,7 @@ describe('fetcher', () => {
formatter
.mockImplementationOnce(() => 'https://api.github.com/repos/ludovic-gonthier/snowshoe/issues?per_page=100');

stubs.paginate
request.paginate
.mockImplementationOnce(() => Promise.resolve(fixtures.issues));

return issues('test_token', repository.issues_url)
Expand All @@ -255,11 +274,15 @@ describe('fetcher', () => {

expect(formatter)
.toBeCalledWith(
'https://api.github.com/repos/ludovic-gonthier/snowshoe/issues{/number}',
{ per_page: 100 }
);
expect(stubs.paginate)
.toBeCalledWith('https://api.github.com/repos/ludovic-gonthier/snowshoe/issues?per_page=100', 'issue');
'https://api.github.com/repos/ludovic-gonthier/snowshoe/issues{/number}',
{ per_page: 100 }
);
expect(request.paginate)
.toBeCalledWith(
'https://api.github.com/repos/ludovic-gonthier/snowshoe/issues?per_page=100',
'issue',
'test_token'
);
});
});

Expand All @@ -271,7 +294,7 @@ describe('fetcher', () => {
formatter
.mockImplementationOnce(() => 'https://api.github.com/repos/ludovic-gonthier/snowshoe/issues?per_page=100');

stubs.paginate
request.paginate
.mockImplementationOnce(() => Promise.reject(new Error('Pagination error')));

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -302,7 +325,7 @@ describe('fetcher', () => {
}];

fpulls.forEach((item) => {
stubs.call
request.call
.mockImplementationOnce(() => Promise.resolve(item.stub));
});

Expand All @@ -326,8 +349,12 @@ describe('fetcher', () => {
]);

fpulls.forEach((item, index) => {
expect(stubs.call.mock.calls[index])
.toEqual([`https://api.github.com/repos/ludovic-gonthier/snowshoe/commits/${item.head.sha}/status`, 'status']);
expect(request.call.mock.calls[index])
.toEqual([
`https://api.github.com/repos/ludovic-gonthier/snowshoe/commits/${item.head.sha}/status`,
'status',
'test_token'
]);
});
});
});
Expand All @@ -336,7 +363,7 @@ describe('fetcher', () => {
base: { repo: { url: 'https://api.github.com/repos/ludovic-gonthier/snowshoe' } },
head: { sha: '67sd687ad4adsd6' },
}];
stubs.call
request.call
.mockImplementationOnce(() => Promise.reject(new Error('Pagination error')));

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -370,45 +397,45 @@ describe('fetcher', () => {
}];

fpulls.forEach((item) => {
stubs.call
request.call
.mockImplementationOnce(() => Promise.resolve(item.stub));
});
const expected = [
{
headers: {},
json: {
pull_request: {
id: 1,
},
reviews: fpulls[0].stub.json,
const expected = [{
headers: {},
json: {
pull_request: {
id: 1,
},
reviews: fpulls[0].stub.json,
},
{
headers: {},
json: {
pull_request: {
id: 2,
},
reviews: fpulls[1].stub.json,
}, {
headers: {},
json: {
pull_request: {
id: 2,
},
reviews: fpulls[1].stub.json,
},
{
headers: {},
json: {
pull_request: {
id: 3,
},
reviews: fpulls[2].stub.json,
}, {
headers: {},
json: {
pull_request: {
id: 3,
},
reviews: fpulls[2].stub.json,
},
];
}];

return reviews('test_token', fpulls)
.then((data) => {
expect(data).toEqual(expected);

fpulls.forEach((item, index) => expect(stubs.call.mock.calls[index])
.toEqual([`${item.base.repo.url}/pulls/${item.number}/reviews`, 'review']));
fpulls.forEach((item, index) => expect(request.call.mock.calls[index])
.toEqual([
`${item.base.repo.url}/pulls/${item.number}/reviews`,
'review',
'test_token',
]));
});
});

Expand All @@ -418,7 +445,7 @@ describe('fetcher', () => {
head: { sha: '67sd687ad4adsd6' },
}];

stubs.call
request.call
.mockImplementationOnce(() => Promise.reject(new Error('Pagination error')));

return new Promise((resolve, reject) => {
Expand Down
Loading

0 comments on commit de71005

Please sign in to comment.