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,51 @@
const fs = require('fs');
const common = require('../../src/common');
const verifyAndPlaceBinary = require('../../src/assets/binary');
jest.mock('fs');
jest.mock('../../src/common');
describe('verifyAndPlaceBinary()', () => {
let callback;
beforeEach(() => {
callback = jest.fn();
});
it('should call callback with error if binary downloaded differs from config', () => {
fs.existsSync.mockReturnValueOnce(false);
verifyAndPlaceBinary('command', './bin', callback);
expect(callback).toHaveBeenCalledWith('Downloaded binary does not contain the binary specified in configuration - command');
});
it('should call callback with error if installation path cannot be found', () => {
const error = new Error();
fs.existsSync.mockReturnValueOnce(true);
common.getInstallationPath.mockImplementationOnce((cb) => cb(error));
verifyAndPlaceBinary('command', './bin', callback);
expect(callback).toHaveBeenCalledWith(error);
});
it('should call callback with null on success', () => {
fs.existsSync.mockReturnValueOnce(true);
common.getInstallationPath.mockImplementationOnce((cb) => cb(null, '/usr/local/bin'));
verifyAndPlaceBinary('command', './bin', callback);
expect(callback).toHaveBeenCalledWith(null);
});
it('should move the binary to installation directory', () => {
fs.existsSync.mockReturnValueOnce(true);
common.getInstallationPath.mockImplementationOnce((cb) => cb(null, '/usr/local/bin'));
verifyAndPlaceBinary('command', './bin', callback);
expect(fs.renameSync).toHaveBeenCalledWith('bin/command', '/usr/local/bin/command');
});
});