forked from renovatebot/renovate
/
gitea.spec.ts
85 lines (71 loc) · 2.6 KB
/
gitea.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
import * as httpMock from '../../../test/httpMock';
import { getName } from '../../../test/util';
import { GiteaHttp, setBaseUrl } from './gitea';
describe(getName(__filename), () => {
const baseUrl = 'https://gitea.renovatebot.com/api/v1';
let giteaHttp: GiteaHttp;
beforeEach(() => {
giteaHttp = new GiteaHttp();
jest.resetAllMocks();
httpMock.reset();
httpMock.setup();
setBaseUrl(baseUrl);
});
it('supports responses without pagination when enabled', async () => {
httpMock
.scope(baseUrl)
.get('/pagination-example-1')
.reply(200, { hello: 'world' });
const res = await giteaHttp.getJson('pagination-example-1', {
paginate: true,
});
expect(res.body).toEqual({ hello: 'world' });
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('supports root-level pagination', async () => {
httpMock
.scope(baseUrl)
.get('/pagination-example-1')
.reply(200, ['abc', 'def', 'ghi'], { 'x-total-count': '5' })
.get('/pagination-example-1?page=2')
.reply(200, ['jkl'])
.get('/pagination-example-1?page=3')
.reply(200, ['mno', 'pqr']);
const res = await giteaHttp.getJson(`${baseUrl}/pagination-example-1`, {
paginate: true,
});
expect(res.body).toHaveLength(6);
expect(res.body).toEqual(['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr']);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('supports pagination on data property', async () => {
httpMock
.scope(baseUrl)
.get('/pagination-example-2')
.reply(200, { data: ['abc', 'def', 'ghi'] }, { 'x-total-count': '5' })
.get('/pagination-example-2?page=2')
.reply(200, { data: ['jkl'] })
.get('/pagination-example-2?page=3')
.reply(200, { data: ['mno', 'pqr'] });
const res = await giteaHttp.getJson<{ data: any }>('pagination-example-2', {
paginate: true,
});
expect(res.body.data).toHaveLength(6);
expect(res.body.data).toEqual(['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr']);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('handles pagination with empty response', async () => {
httpMock
.scope(baseUrl)
.get('/pagination-example-3')
.reply(200, { data: ['abc', 'def', 'ghi'] }, { 'x-total-count': '5' })
.get('/pagination-example-3?page=2')
.reply(200, { data: [] });
const res = await giteaHttp.getJson<{ data: any }>('pagination-example-3', {
paginate: true,
});
expect(res.body.data).toHaveLength(3);
expect(res.body.data).toEqual(['abc', 'def', 'ghi']);
expect(httpMock.getTrace()).toMatchSnapshot();
});
});