/
bitbucket.spec.ts
88 lines (77 loc) · 2.07 KB
/
bitbucket.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
import * as httpMock from '../../../test/http-mock';
import * as hostRules from '../host-rules';
import { BitbucketHttp, setBaseUrl } from './bitbucket';
const baseUrl = 'https://api.bitbucket.org';
describe('util/http/bitbucket', () => {
let api: BitbucketHttp;
beforeEach(() => {
api = new BitbucketHttp();
// reset module
jest.resetAllMocks();
// clean up hostRules
hostRules.clear();
hostRules.add({
hostType: 'bitbucket',
matchHost: baseUrl,
token: 'token',
});
setBaseUrl(baseUrl);
});
it('posts', async () => {
const body = ['a', 'b'];
httpMock.scope(baseUrl).post('/some-url').reply(200, body);
const res = await api.postJson('some-url');
expect(res.body).toEqual(body);
});
it('accepts custom baseUrl', async () => {
const customBaseUrl = 'https://api-test.bitbucket.org';
httpMock.scope(baseUrl).post('/some-url').reply(200, {});
httpMock.scope(customBaseUrl).post('/some-url').reply(200, {});
expect(await api.postJson('some-url')).toEqual({
authorization: true,
body: {},
headers: {
'content-type': 'application/json',
},
statusCode: 200,
});
setBaseUrl(customBaseUrl);
expect(await api.postJson('some-url')).toEqual({
authorization: false,
body: {},
headers: {
'content-type': 'application/json',
},
statusCode: 200,
});
});
it('paginates', async () => {
httpMock
.scope(baseUrl)
.get('/some-url')
.reply(200, {
values: ['a'],
page: '1',
next: `${baseUrl}/some-url?page=2`,
})
.get('/some-url?page=2')
.reply(200, {
values: ['b', 'c'],
page: '2',
next: `${baseUrl}/some-url?page=3`,
})
.get('/some-url?page=3')
.reply(200, {
values: ['d'],
page: '3',
});
const res = await api.getJson('some-url', { paginate: true });
expect(res.body).toEqual({
page: '1',
pagelen: 4,
size: 4,
values: ['a', 'b', 'c', 'd'],
next: undefined,
});
});
});