Skip to content

Commit

Permalink
fix(getFilesList): call using package object
Browse files Browse the repository at this point in the history
clean up tests too
  • Loading branch information
Haroenv committed Aug 12, 2019
1 parent dc56195 commit 6b954d5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
38 changes: 32 additions & 6 deletions src/jsDelivr/__test__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import * as api from '../index.js';
import log from '../../log.js';

jest.mock('../../log.js', () => {
return {
info: jest.fn(),
warn: jest.fn(),
};
});

beforeEach(() => {
jest.resetAllMocks();
});

describe('hits', () => {
beforeAll(async () => {
Expand Down Expand Up @@ -51,34 +63,48 @@ describe('hits', () => {
describe('files', () => {
describe('getFilesList()', () => {
it('should get a flat list of files', async () => {
const files = await api.getFilesList({ name: 'jest@24.8.0' });
const files = await api.getFilesList({
name: 'jest',
version: '24.8.0',
});
expect(files).toMatchSnapshot();
});

it('should not get a files list', async () => {
it('should not get a files list for fake package', async () => {
const files = await api.getFilesList({
name: 'thispackagedoesnotexist@3.33.0',
name: 'thispackagedoesnotexist',
version: '3.33.0',
});
expect(files).toEqual([]);
expect(log.warn.mock.calls[0][0].message).toMatchInlineSnapshot(
`"Response code 404 (Not Found)"`
);
});
});

describe('getAllFilesList()', () => {
it('should get a flat list of files', async () => {
const files = await api.getAllFilesList([{ name: 'jest@24.8.0' }]);
const files = await api.getAllFilesList([
{ name: 'jest', version: '24.8.0' },
]);
expect(files).toMatchSnapshot();
});

it('should get multiple flat list of files', async () => {
const files = await api.getAllFilesList([
{
name: 'jest@24.8.0',
name: 'jest',
version: '24.8.0',
},
{
name: 'thispackagedoesnotexist@3.33.0',
name: 'thispackagedoesnotexist',
version: '3.33.0',
},
]);
expect(files).toMatchSnapshot();
expect(log.warn.mock.calls[0][0].message).toMatchInlineSnapshot(
`"Response code 404 (Not Found)"`
);
});
});
});
4 changes: 2 additions & 2 deletions src/jsDelivr/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async function getAllFilesList(pkgs) {
*/
async function getFilesList(pkg) {
const start = Date.now();
if (!pkg.name || !pkg.name.includes('@')) {
if (!pkg.name || !pkg.version) {
throw new Error(
`Package name should contain a version number: ${pkg.name}`
);
Expand All @@ -78,7 +78,7 @@ async function getFilesList(pkg) {
let files = [];
try {
const response = await got(
`${config.jsDelivrPackageEndpoint}/${pkg.name}/flat`,
`${config.jsDelivrPackageEndpoint}/${pkg.name}@${pkg.version}/flat`,
{
json: true,
}
Expand Down

0 comments on commit 6b954d5

Please sign in to comment.