mirror of
https://github.com/ershisan99/go-npm.git
synced 2026-01-31 05:12:14 +00:00
ADD unit tests, split and refactor source code
This commit is contained in:
59
__test__/actions/uninstall.spec.js
Normal file
59
__test__/actions/uninstall.spec.js
Normal 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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user