-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
index.spec.ts
87 lines (83 loc) · 2.7 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
import fs from 'fs';
import { DATASOURCE_FAILURE } from '../../constants/error-messages';
import _got from '../../util/got';
import { getReleases } from '.';
const got: jest.Mock<any> = _got as any;
jest.mock('../../util/got');
let res1 = fs.readFileSync(
'lib/datasource/cdnjs/__fixtures__/d3-force.json',
'utf8'
);
res1 = JSON.parse(res1);
let res2 = fs.readFileSync(
'lib/datasource/cdnjs/__fixtures__/bulma.json',
'utf8'
);
res2 = JSON.parse(res2);
describe('datasource/cdnjs', () => {
describe('getReleases', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('throws for empty result', async () => {
got.mockResolvedValueOnce(null);
await expect(getReleases({ lookupName: 'foo/bar' })).rejects.toThrow(
DATASOURCE_FAILURE
);
});
it('throws for missing fields', async () => {
got.mockResolvedValueOnce({});
await expect(getReleases({ lookupName: 'foo/bar' })).rejects.toThrow(
DATASOURCE_FAILURE
);
});
it('returns null for 404', async () => {
got.mockRejectedValueOnce({ statusCode: 404 });
expect(await getReleases({ lookupName: 'foo/bar' })).toBeNull();
});
it('returns null for empty 200 OK', async () => {
got.mockResolvedValueOnce({ body: {} });
expect(
await getReleases({ lookupName: 'doesnotexist/doesnotexist' })
).toBeNull();
});
it('throws for 401', async () => {
got.mockRejectedValueOnce({ statusCode: 401 });
await expect(getReleases({ lookupName: 'foo/bar' })).rejects.toThrow(
DATASOURCE_FAILURE
);
});
it('throws for 429', async () => {
got.mockRejectedValueOnce({ statusCode: 429 });
await expect(getReleases({ lookupName: 'foo/bar' })).rejects.toThrow(
DATASOURCE_FAILURE
);
});
it('throws for 5xx', async () => {
got.mockRejectedValueOnce({ statusCode: 502 });
await expect(getReleases({ lookupName: 'foo/bar' })).rejects.toThrow(
DATASOURCE_FAILURE
);
});
it('returns null for unknown error', async () => {
got.mockImplementationOnce(() => {
throw new Error();
});
await expect(getReleases({ lookupName: 'foo/bar' })).rejects.toThrow(
DATASOURCE_FAILURE
);
});
it('processes real data', async () => {
got.mockResolvedValueOnce({ body: res1 });
const res = await getReleases({ lookupName: 'd3-force/d3-force.js' });
expect(res).toMatchSnapshot();
});
it('filters releases by asset presence', async () => {
got.mockResolvedValueOnce({ body: res2 });
const res = await getReleases({
lookupName: 'bulma/only/0.7.5/style.css',
});
expect(res).toMatchSnapshot();
});
});
});