Skip to content
Snippets Groups Projects
Unverified Commit d5aeb500 authored by Sergio Zharinov's avatar Sergio Zharinov Committed by GitHub
Browse files

refactor(gitlab): Use nock for tests (#6267)

parent d03b7e61
Branches
Tags
No related merge requests found
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`platform/gitlab/gl-got-wrapper attempts to paginate 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "gitlab.com",
"private-token": "abc123",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://gitlab.com/api/v4/some-url",
},
]
`;
exports[`platform/gitlab/gl-got-wrapper paginates 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "gitlab.com",
"private-token": "abc123",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://gitlab.com/api/v4/some-url",
},
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "gitlab.com",
"private-token": "abc123",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://gitlab.com/api/v4/some-url&page=2",
},
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "gitlab.com",
"private-token": "abc123",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://gitlab.com/api/v4/some-url&page=3",
},
]
`;
exports[`platform/gitlab/gl-got-wrapper posts 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate",
"host": "gitlab.com",
"private-token": "abc123",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "POST",
"url": "https://gitlab.com/api/v4/some-url",
},
]
`;
This diff is collapsed.
import * as httpMock from '../../../test/httpMock';
import { PLATFORM_TYPE_GITLAB } from '../../constants/platforms';
import _got from '../../util/got';
import * as runCache from '../../util/cache/run';
import * as hostRules from '../../util/host-rules';
import { api } from './gl-got-wrapper';
jest.mock('../../util/got');
const got: any = _got;
hostRules.add({
hostType: PLATFORM_TYPE_GITLAB,
token: 'abc123',
});
const gitlabApiHost = 'https://gitlab.com';
describe('platform/gitlab/gl-got-wrapper', () => {
const body = ['a', 'b'];
beforeEach(() => {
// (delay as any).mockImplementation(() => Promise.resolve());
httpMock.setup();
});
afterEach(() => {
jest.resetAllMocks();
httpMock.reset();
runCache.clear();
});
it('paginates', async () => {
got.mockReturnValueOnce({
headers: {
httpMock
.scope(gitlabApiHost)
.get('/api/v4/some-url')
.reply(200, ['a'], {
link:
'<https://api.gitlab.com/search/code?q=addClass+user%3Amozilla&page=2>; rel="next", <https://api.gitlab.com/search/code?q=addClass+user%3Amozilla&page=34>; rel="last"',
},
body: ['a'],
});
got.mockReturnValueOnce({
headers: {
'<https://gitlab.com/api/v4/some-url&page=2>; rel="next", <https://gitlab.com/api/v4/some-url&page=3>; rel="last"',
})
.get('/api/v4/some-url&page=2')
.reply(200, ['b', 'c'], {
link:
'<https://api.gitlab.com/search/code?q=addClass+user%3Amozilla&page=3>; rel="next", <https://api.gitlab.com/search/code?q=addClass+user%3Amozilla&page=34>; rel="last"',
},
body: ['b', 'c'],
});
got.mockReturnValueOnce({
headers: {},
body: ['d'],
});
const res = await api.get('some-url', { paginate: true });
'<https://gitlab.com/api/v4/some-url&page=3>; rel="next", <https://gitlab.com/api/v4/some-url&page=3>; rel="last"',
})
.get('/api/v4/some-url&page=3')
.reply(200, ['d']);
const res = await api.get('/some-url', { paginate: true });
expect(res.body).toHaveLength(4);
expect(got).toHaveBeenCalledTimes(3);
const trace = httpMock.getTrace();
expect(trace).toHaveLength(3);
expect(trace).toMatchSnapshot();
});
it('attempts to paginate', async () => {
got.mockReturnValueOnce({
headers: {
link:
'<https://api.gitlab.com/search/code?q=addClass+user%3Amozilla&page=34>; rel="last"',
},
body: ['a'],
httpMock.scope(gitlabApiHost).get('/api/v4/some-url').reply(200, ['a'], {
link: '<https://gitlab.com/api/v4/some-url&page=3>; rel="last"',
});
got.mockReturnValueOnce({
headers: {},
body: ['b'],
});
const res = await api.get('some-url', { paginate: true });
const res = await api.get('/some-url', { paginate: true });
expect(res.body).toHaveLength(1);
expect(got).toHaveBeenCalledTimes(1);
const trace = httpMock.getTrace();
expect(trace).toHaveLength(1);
expect(trace).toMatchSnapshot();
});
it('posts', async () => {
got.mockImplementationOnce(() => ({
body,
}));
const res = await api.post('some-url');
httpMock.scope(gitlabApiHost).post('/api/v4/some-url').reply(200, body);
const res = await api.post('/some-url');
expect(res.body).toEqual(body);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('sets baseUrl', () => {
api.setBaseUrl('https://gitlab.renovatebot.com/api/v4/');
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment