Files
go-npm/__test__/actions/uninstall.spec.js
2021-07-27 04:08:09 +01:00

61 lines
1.4 KiB
JavaScript

const fs = require('fs');
const common = require('../../src/common');
const path = require('path');
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(path.join('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);
});
});