forked from renovatebot/renovate
/
index.spec.ts
179 lines (161 loc) · 5.07 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { getPkgReleases } from '..';
import * as httpMock from '../../../../test/http-mock';
import { partial } from '../../../../test/util';
import * as githubGraphql from '../../../util/github/graphql';
import type { GithubTagItem } from '../../../util/github/graphql/types';
import * as hostRules from '../../../util/host-rules';
import { GithubTagsDatasource } from '.';
const githubApiHost = 'https://api.github.com';
describe('modules/datasource/github-tags/index', () => {
const github = new GithubTagsDatasource();
beforeEach(() => {
jest.resetAllMocks();
jest.spyOn(hostRules, 'hosts').mockReturnValue([]);
jest.spyOn(hostRules, 'find').mockReturnValue({
token: 'some-token',
});
});
describe('getDigest', () => {
const packageName = 'some/dep';
it('returns commit digest', async () => {
httpMock
.scope(githubApiHost)
.get(`/repos/${packageName}/commits?per_page=1`)
.reply(200, [{ sha: 'abcdef' }]);
const res = await github.getDigest({ packageName }, undefined);
expect(res).toBe('abcdef');
});
it('returns null for missing commit', async () => {
httpMock
.scope(githubApiHost)
.get(`/repos/${packageName}/commits?per_page=1`)
.reply(200, []);
const res = await github.getDigest({ packageName }, undefined);
expect(res).toBeNull();
});
it('returns untagged commit digest', async () => {
httpMock
.scope(githubApiHost)
.get(`/repos/${packageName}/commits?per_page=1`)
.reply(200, [{ sha: 'abcdef' }]);
const res = await github.getDigest({ packageName }, undefined);
expect(res).toBe('abcdef');
});
it('returns tagged commit digest', async () => {
jest.spyOn(githubGraphql, 'queryTags').mockResolvedValueOnce([
{
version: 'v1.0.0',
gitRef: 'v1.0.0',
releaseTimestamp: '2021-01-01',
hash: '123',
},
{
version: 'v2.0.0',
gitRef: 'v2.0.0',
releaseTimestamp: '2022-01-01',
hash: 'abc',
},
]);
const res = await github.getDigest({ packageName }, 'v2.0.0');
expect(res).toBe('abc');
});
it('returns null for missing hash', async () => {
jest.spyOn(githubGraphql, 'queryTags').mockResolvedValueOnce([
{
version: 'v1.0.0',
gitRef: 'v1.0.0',
releaseTimestamp: '2021-01-01',
hash: '123',
},
partial<GithubTagItem>({
version: 'v2.0.0',
gitRef: 'v2.0.0',
releaseTimestamp: '2022-01-01',
}),
]);
const res = await github.getDigest({ packageName }, 'v2.0.0');
expect(res).toBeNull();
});
it('returns null for missing tagged commit digest', async () => {
jest.spyOn(githubGraphql, 'queryTags').mockResolvedValueOnce([
{
version: 'v1.0.0',
gitRef: 'v1.0.0',
releaseTimestamp: '2021-01-01',
hash: '123',
},
{
version: 'v2.0.0',
gitRef: 'v2.0.0',
releaseTimestamp: '2022-01-01',
hash: 'abc',
},
]);
const res = await github.getDigest({ packageName }, 'v3.0.0');
expect(res).toBeNull();
});
it('returns null for error', async () => {
jest.spyOn(githubGraphql, 'queryTags').mockRejectedValueOnce('error');
const res = await github.getDigest({ packageName }, 'v3.0.0');
expect(res).toBeNull();
});
});
describe('getReleases', () => {
const packageName = 'some/dep2';
it('returns tags', async () => {
jest.spyOn(githubGraphql, 'queryTags').mockResolvedValueOnce([
{
version: 'v1.0.0',
gitRef: 'v1.0.0',
releaseTimestamp: '2021-01-01',
hash: '123',
},
{
version: 'v2.0.0',
gitRef: 'v2.0.0',
releaseTimestamp: '2022-01-01',
hash: 'abc',
},
]);
jest.spyOn(githubGraphql, 'queryReleases').mockResolvedValueOnce([
{
id: 1,
version: 'v1.0.0',
releaseTimestamp: '2021-01-01',
isStable: true,
url: 'https://example.com',
name: 'some/dep2',
description: 'some description',
},
{
id: 2,
version: 'v2.0.0',
releaseTimestamp: '2022-01-01',
isStable: false,
url: 'https://example.com',
name: 'some/dep2',
description: 'some description',
},
]);
const res = await getPkgReleases({ datasource: github.id, packageName });
expect(res).toEqual({
registryUrl: 'https://github.com',
releases: [
{
gitRef: 'v1.0.0',
version: 'v1.0.0',
releaseTimestamp: '2021-01-01T00:00:00.000Z',
isStable: true,
},
{
gitRef: 'v2.0.0',
version: 'v2.0.0',
releaseTimestamp: '2022-01-01T00:00:00.000Z',
isStable: false,
},
],
sourceUrl: 'https://github.com/some/dep2',
});
});
});
});