/
index.spec.ts
96 lines (92 loc) · 3.55 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
import fs from 'fs';
import * as httpMock from '../../../test/httpMock';
import { getReleases } from '.';
const res1 = fs.readFileSync('lib/datasource/crate/__fixtures__/libc', 'utf8');
const res2 = fs.readFileSync(
'lib/datasource/crate/__fixtures__/amethyst',
'utf8'
);
const res3 = fs.readFileSync(
'lib/datasource/crate/__fixtures__/invalid_crate_data',
'utf8'
);
const baseUrl =
'https://raw.githubusercontent.com/rust-lang/crates.io-index/master/';
describe('datasource/crate', () => {
describe('getReleases', () => {
it('returns null for empty result', async () => {
httpMock.scope(baseUrl).get('/no/n_/non_existent_crate').reply(200, {});
expect(
await getReleases({ lookupName: 'non_existent_crate' })
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for missing fields', async () => {
httpMock
.scope(baseUrl)
.get('/no/n_/non_existent_crate')
.reply(200, undefined);
expect(
await getReleases({ lookupName: 'non_existent_crate' })
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for empty list', async () => {
httpMock.scope(baseUrl).get('/no/n_/non_existent_crate').reply(200, '\n');
expect(
await getReleases({ lookupName: 'non_existent_crate' })
).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for 404', async () => {
httpMock.scope(baseUrl).get('/so/me/some_crate').reply(404);
expect(await getReleases({ lookupName: 'some_crate' })).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('throws for 5xx', async () => {
httpMock.scope(baseUrl).get('/so/me/some_crate').reply(502);
let e;
try {
await getReleases({ lookupName: 'some_crate' });
} catch (err) {
e = err;
}
expect(e).toBeDefined();
expect(e).toMatchSnapshot();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for unknown error', async () => {
httpMock.scope(baseUrl).get('/so/me/some_crate').replyWithError('');
expect(await getReleases({ lookupName: 'some_crate' })).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('processes real data: libc', async () => {
httpMock.scope(baseUrl).get('/li/bc/libc').reply(200, res1);
const res = await getReleases({ lookupName: 'libc' });
expect(res).toMatchSnapshot();
expect(res).not.toBeNull();
expect(res).toBeDefined();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('processes real data: amethyst', async () => {
httpMock.scope(baseUrl).get('/am/et/amethyst').reply(200, res2);
const res = await getReleases({ lookupName: 'amethyst' });
expect(res).toMatchSnapshot();
expect(res).not.toBeNull();
expect(res).toBeDefined();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null if crate name is invalid', async () => {
httpMock.scope(baseUrl).get('/in/va/invalid-crate-name').reply(200, res2);
const res = await getReleases({ lookupName: 'invalid-crate-name' });
expect(res).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('returns null for invalid crate data', async () => {
httpMock.scope(baseUrl).get('/so/me/some_crate').reply(200, res3);
const res = await getReleases({ lookupName: 'some_crate' });
expect(res).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
});
});
});