/
index.spec.ts
111 lines (102 loc) · 3.15 KB
/
index.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import * as httpMock from '../../../../test/httpMock';
import { getName } from '../../../../test/util';
import { EXTERNAL_HOST_ERROR } from '../../../constants/error-messages';
import { PRESET_DEP_NOT_FOUND } from '../util';
import * as gitlab from '.';
const gitlabApiHost = 'https://gitlab.com';
const basePath = '/api/v4/projects/some%2Frepo/repository';
describe(getName(__filename), () => {
beforeEach(() => {
jest.resetAllMocks();
httpMock.setup();
});
afterEach(() => {
httpMock.reset();
});
describe('getPreset()', () => {
it('throws EXTERNAL_HOST_ERROR', async () => {
httpMock.scope(gitlabApiHost).get(`${basePath}/branches`).reply(500);
await expect(
gitlab.getPreset({
packageName: 'some/repo',
presetName: 'non-default',
})
).rejects.toThrow(EXTERNAL_HOST_ERROR);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('throws if missing', async () => {
httpMock
.scope(gitlabApiHost)
.get(`${basePath}/branches`)
.twice()
.reply(200, [])
.get(`${basePath}/files/default.json/raw?ref=master`)
.reply(404, null)
.get(`${basePath}/files/renovate.json/raw?ref=master`)
.reply(404, null);
await expect(
gitlab.getPreset({ packageName: 'some/repo' })
).rejects.toThrow(PRESET_DEP_NOT_FOUND);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('should return the preset', async () => {
httpMock
.scope(gitlabApiHost)
.get(`${basePath}/branches`)
.reply(200, [
{
name: 'devel',
},
{
name: 'master',
default: true,
},
])
.get(`${basePath}/files/default.json/raw?ref=master`)
.reply(200, { foo: 'bar' }, {});
const content = await gitlab.getPreset({ packageName: 'some/repo' });
expect(content).toEqual({ foo: 'bar' });
expect(httpMock.getTrace()).toMatchSnapshot();
});
});
describe('getPresetFromEndpoint()', () => {
it('uses default endpoint', async () => {
httpMock
.scope(gitlabApiHost)
.get(`${basePath}/branches`)
.reply(200, [
{
name: 'devel',
default: true,
},
])
.get(`${basePath}/files/some.json/raw?ref=devel`)
.reply(200, { preset: { file: {} } });
expect(
await gitlab.getPresetFromEndpoint('some/repo', 'some/preset/file')
).toEqual({});
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('uses custom endpoint', async () => {
httpMock
.scope('https://gitlab.example.org')
.get(`${basePath}/branches`)
.reply(200, [
{
name: 'devel',
default: true,
},
])
.get(`${basePath}/files/some.json/raw?ref=devel`)
.reply(404);
await expect(
gitlab.getPresetFromEndpoint(
'some/repo',
'some/preset/file',
'https://gitlab.example.org/api/v4'
)
).rejects.toThrow(PRESET_DEP_NOT_FOUND);
expect(httpMock.getTrace()).toMatchSnapshot();
});
});
});