ADD unit tests, split and refactor source code

This commit is contained in:
RecuencoJones
2019-03-25 20:38:29 +01:00
parent 6eaf5fc9af
commit 8257010937
18 changed files with 782 additions and 392 deletions

View File

@@ -0,0 +1,91 @@
const { EventEmitter } = require('events');
const request = require('request');
const common = require('../../src/common');
const install = require('../../src/actions/install');
const move = require('../../src/assets/move');
const untar = require('../../src/assets/untar');
const verifyAndPlaceCallback = require('../../src/assets/binary');
jest.mock('fs');
jest.mock('mkdirp');
jest.mock('request');
jest.mock('../../src/common');
jest.mock('../../src/assets/move');
jest.mock('../../src/assets/untar');
jest.mock('../../src/assets/binary');
describe('install()', () => {
let callback, requestEvents;
beforeEach(() => {
callback = jest.fn();
requestEvents = new EventEmitter();
});
it('should call callback with error if package.json did not return value' , () => {
common.parsePackageJson.mockReturnValueOnce(undefined);
install(callback);
expect(callback).toHaveBeenCalledWith('Invalid inputs');
});
it('should call callback with error on request error', () => {
request.mockReturnValueOnce(requestEvents);
common.parsePackageJson.mockReturnValueOnce({ url: 'http://url' });
install(callback);
requestEvents.emit('error');
expect(callback).toHaveBeenCalledWith('Error downloading from URL: http://url');
});
it('should call callback with error on response with status code different than 200', () => {
request.mockReturnValueOnce(requestEvents);
common.parsePackageJson.mockReturnValueOnce({ url: 'http://url' });
install(callback);
requestEvents.emit('response', { statusCode: 404 });
expect(callback).toHaveBeenCalledWith('Error downloading binary. HTTP Status Code: 404');
});
it('should pick move strategy if url is an uncompressed binary', () => {
request.mockReturnValueOnce(requestEvents);
common.parsePackageJson.mockReturnValueOnce({ url: 'http://url' });
install(callback);
requestEvents.emit('response', { statusCode: 200 });
expect(move).toHaveBeenCalled();
});
it('should pick untar strategy if url ends with .tar.gz', () => {
request.mockReturnValueOnce(requestEvents);
common.parsePackageJson.mockReturnValueOnce({ url: 'http://url.tar.gz' });
install(callback);
requestEvents.emit('response', { statusCode: 200 });
expect(untar).toHaveBeenCalled();
});
it('should call verifyAndPlaceCallback on success', () => {
request.mockReturnValueOnce(requestEvents);
common.parsePackageJson.mockReturnValueOnce({ url: 'http://url', binName: 'command', binPath: './bin' });
move.mockImplementationOnce(({ onSuccess }) => onSuccess());
install(callback);
requestEvents.emit('response', { statusCode: 200 });
expect(verifyAndPlaceCallback).toHaveBeenCalledWith('command', './bin', callback);
});
});

View File

@@ -0,0 +1,59 @@
const fs = require('fs');
const common = require('../../src/common');
const uninstall = require('../../src/actions/uninstall');
jest.mock('fs');
jest.mock('../../src/common');
describe('uninstall()', () => {
let callback;
beforeEach(() => {
callback = jest.fn();
common.parsePackageJson.mockReturnValueOnce({ binName: 'command' });
});
it('should call callback with error if binary not found', () => {
const error = new Error();
common.getInstallationPath.mockImplementationOnce((cb) => cb(error));
uninstall(callback);
expect(callback).toHaveBeenCalledWith(error);
});
it('should call unlinkSync with binary and installation path', () => {
common.getInstallationPath.mockImplementationOnce((cb) => cb(null, './bin'));
uninstall(callback);
expect(fs.unlinkSync).toHaveBeenCalledWith('bin/command');
});
it('should call callback on success', () => {
common.getInstallationPath.mockImplementationOnce((cb) => cb(null, './bin'));
uninstall(callback);
expect(callback).toHaveBeenCalledWith(null);
});
it('should call callback regardless of errors on unlink', () => {
common.getInstallationPath.mockImplementationOnce((cb) => cb(null, './bin'));
fs.unlinkSync.mockImplementationOnce(() => {
throw new Error();
});
uninstall(callback);
expect(callback).toHaveBeenCalledWith(null);
});
});