2019-11-11 18:33:20 +02:00
|
|
|
import API from '../GitHubAPI';
|
|
|
|
|
|
|
|
describe('github API', () => {
|
|
|
|
describe('request', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
const fetch = jest.fn();
|
|
|
|
global.fetch = fetch;
|
|
|
|
global.Date = jest.fn(() => ({ getTime: () => 1000 }));
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should fetch url with authorization header', async () => {
|
|
|
|
const api = new API({
|
|
|
|
api_root: 'https://site.netlify.com/.netlify/git/github',
|
|
|
|
tokenPromise: () => Promise.resolve('token'),
|
|
|
|
});
|
|
|
|
|
|
|
|
fetch.mockResolvedValue({
|
|
|
|
text: jest.fn().mockResolvedValue('some response'),
|
|
|
|
ok: true,
|
|
|
|
status: 200,
|
|
|
|
headers: { get: () => '' },
|
|
|
|
});
|
|
|
|
const result = await api.request('/some-path');
|
|
|
|
expect(result).toEqual('some response');
|
|
|
|
expect(fetch).toHaveBeenCalledTimes(1);
|
|
|
|
expect(fetch).toHaveBeenCalledWith(
|
|
|
|
'https://site.netlify.com/.netlify/git/github/some-path?ts=1000',
|
|
|
|
{
|
2019-12-18 18:16:02 +02:00
|
|
|
headers: {
|
|
|
|
Authorization: 'Bearer token',
|
|
|
|
'Content-Type': 'application/json; charset=utf-8',
|
|
|
|
},
|
2019-11-11 18:33:20 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw error on not ok response with message property', async () => {
|
|
|
|
const api = new API({
|
|
|
|
api_root: 'https://site.netlify.com/.netlify/git/github',
|
|
|
|
tokenPromise: () => Promise.resolve('token'),
|
|
|
|
});
|
|
|
|
|
|
|
|
fetch.mockResolvedValue({
|
|
|
|
text: jest.fn().mockResolvedValue({ message: 'some error' }),
|
|
|
|
ok: false,
|
|
|
|
status: 404,
|
|
|
|
headers: { get: () => '' },
|
|
|
|
});
|
|
|
|
|
|
|
|
await expect(api.request('some-path')).rejects.toThrow(
|
|
|
|
expect.objectContaining({
|
|
|
|
message: 'some error',
|
|
|
|
name: 'API_ERROR',
|
|
|
|
status: 404,
|
|
|
|
api: 'Git Gateway',
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw error on not ok response with msg property', async () => {
|
|
|
|
const api = new API({
|
|
|
|
api_root: 'https://site.netlify.com/.netlify/git/github',
|
|
|
|
tokenPromise: () => Promise.resolve('token'),
|
|
|
|
});
|
|
|
|
|
|
|
|
fetch.mockResolvedValue({
|
|
|
|
text: jest.fn().mockResolvedValue({ msg: 'some error' }),
|
|
|
|
ok: false,
|
|
|
|
status: 404,
|
|
|
|
headers: { get: () => '' },
|
|
|
|
});
|
|
|
|
|
|
|
|
await expect(api.request('some-path')).rejects.toThrow(
|
|
|
|
expect.objectContaining({
|
|
|
|
message: 'some error',
|
|
|
|
name: 'API_ERROR',
|
|
|
|
status: 404,
|
|
|
|
api: 'Git Gateway',
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|