Skip to content

Commit 6b954d5

Browse files
committed
fix(getFilesList): call using package object
clean up tests too
1 parent dc56195 commit 6b954d5

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

src/jsDelivr/__test__/index.test.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
import * as api from '../index.js';
2+
import log from '../../log.js';
3+
4+
jest.mock('../../log.js', () => {
5+
return {
6+
info: jest.fn(),
7+
warn: jest.fn(),
8+
};
9+
});
10+
11+
beforeEach(() => {
12+
jest.resetAllMocks();
13+
});
214

315
describe('hits', () => {
416
beforeAll(async () => {
@@ -51,34 +63,48 @@ describe('hits', () => {
5163
describe('files', () => {
5264
describe('getFilesList()', () => {
5365
it('should get a flat list of files', async () => {
54-
const files = await api.getFilesList({ name: 'jest@24.8.0' });
66+
const files = await api.getFilesList({
67+
name: 'jest',
68+
version: '24.8.0',
69+
});
5570
expect(files).toMatchSnapshot();
5671
});
5772

58-
it('should not get a files list', async () => {
73+
it('should not get a files list for fake package', async () => {
5974
const files = await api.getFilesList({
60-
name: 'thispackagedoesnotexist@3.33.0',
75+
name: 'thispackagedoesnotexist',
76+
version: '3.33.0',
6177
});
6278
expect(files).toEqual([]);
79+
expect(log.warn.mock.calls[0][0].message).toMatchInlineSnapshot(
80+
`"Response code 404 (Not Found)"`
81+
);
6382
});
6483
});
6584

6685
describe('getAllFilesList()', () => {
6786
it('should get a flat list of files', async () => {
68-
const files = await api.getAllFilesList([{ name: 'jest@24.8.0' }]);
87+
const files = await api.getAllFilesList([
88+
{ name: 'jest', version: '24.8.0' },
89+
]);
6990
expect(files).toMatchSnapshot();
7091
});
7192

7293
it('should get multiple flat list of files', async () => {
7394
const files = await api.getAllFilesList([
7495
{
75-
name: 'jest@24.8.0',
96+
name: 'jest',
97+
version: '24.8.0',
7698
},
7799
{
78-
name: 'thispackagedoesnotexist@3.33.0',
100+
name: 'thispackagedoesnotexist',
101+
version: '3.33.0',
79102
},
80103
]);
81104
expect(files).toMatchSnapshot();
105+
expect(log.warn.mock.calls[0][0].message).toMatchInlineSnapshot(
106+
`"Response code 404 (Not Found)"`
107+
);
82108
});
83109
});
84110
});

src/jsDelivr/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async function getAllFilesList(pkgs) {
6969
*/
7070
async function getFilesList(pkg) {
7171
const start = Date.now();
72-
if (!pkg.name || !pkg.name.includes('@')) {
72+
if (!pkg.name || !pkg.version) {
7373
throw new Error(
7474
`Package name should contain a version number: ${pkg.name}`
7575
);
@@ -78,7 +78,7 @@ async function getFilesList(pkg) {
7878
let files = [];
7979
try {
8080
const response = await got(
81-
`${config.jsDelivrPackageEndpoint}/${pkg.name}/flat`,
81+
`${config.jsDelivrPackageEndpoint}/${pkg.name}@${pkg.version}/flat`,
8282
{
8383
json: true,
8484
}

0 commit comments

Comments
 (0)