0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00
verdaccio/packages/core/local-storage/tests/utils.test.ts
Juan Picado 3838d3d212 feat: relocate core packages (#1906)
* chore: clean lint warnings

* refactor: move core packages
2021-04-09 17:54:13 +02:00

74 lines
2.1 KiB
TypeScript

import path from 'path';
import fs from 'fs';
import { findPackages } from '../src/utils';
import { loadPrivatePackages } from '../src/pkg-utils';
import { noSuchFile } from '../src/local-fs';
// FIXME: remove this mocks imports
// eslint-disable-next-line jest/no-mocks-import
import logger from './__mocks__/Logger';
describe('Utitlies', () => {
const loadDb = (name): string => path.join(__dirname, '__fixtures__/databases', `${name}.json`);
beforeEach(() => {
jest.resetModules();
});
test('should load private packages', () => {
const database = loadDb('ok');
const db = loadPrivatePackages(database, logger);
expect(db.list).toHaveLength(15);
});
test('should load and empty private packages if database file is valid and empty', () => {
const database = loadDb('empty');
const db = loadPrivatePackages(database, logger);
expect(db.list).toHaveLength(0);
});
test('should fails on load private packages', () => {
const database = loadDb('corrupted');
expect(() => {
loadPrivatePackages(database, logger);
}).toThrow();
});
test('should handle null read values and return empty database', () => {
const spy = jest.spyOn(fs, 'readFileSync');
spy.mockReturnValue(null);
const database = loadDb('ok');
const db = loadPrivatePackages(database, logger);
expect(db.list).toHaveLength(0);
spy.mockRestore();
});
describe('find packages', () => {
test('should fails on wrong storage path', async () => {
try {
await findPackages(
'./no_such_folder_fake',
jest.fn(() => true)
);
} catch (e) {
expect(e.code).toEqual(noSuchFile);
}
});
test('should fetch all packages from valid storage', async () => {
const storage = path.join(__dirname, '__fixtures__/findPackages');
const validator = jest.fn((file) => file.indexOf('.') === -1);
const pkgs = await findPackages(storage, validator);
// the result is 7 due number of packages on "findPackages" directory
expect(pkgs).toHaveLength(5);
expect(validator).toHaveBeenCalledTimes(8);
});
});
});