mirror of
https://github.com/ershisan99/go-npm.git
synced 2025-12-17 20:59:33 +00:00
ADD unit tests, split and refactor source code
This commit is contained in:
65
__test__/assets/untar.spec.js
Normal file
65
__test__/assets/untar.spec.js
Normal file
@@ -0,0 +1,65 @@
|
||||
const { EventEmitter } = require('events');
|
||||
const zlib = require('zlib');
|
||||
const tar = require('tar');
|
||||
const untar = require('../../src/assets/untar');
|
||||
|
||||
jest.mock('zlib');
|
||||
jest.mock('tar', () => ({
|
||||
Extract: jest.fn()
|
||||
}));
|
||||
|
||||
describe('untar()', () => {
|
||||
|
||||
let ungzEvents, untarEvents, pipe, onSuccess, onError;
|
||||
|
||||
beforeEach(() => {
|
||||
ungzEvents = new EventEmitter();
|
||||
untarEvents = new EventEmitter();
|
||||
|
||||
pipe = jest.fn();
|
||||
onSuccess = jest.fn();
|
||||
onError = jest.fn();
|
||||
|
||||
pipe.mockReturnValueOnce({ pipe });
|
||||
tar.Extract.mockReturnValueOnce(untarEvents);
|
||||
zlib.createGunzip.mockReturnValueOnce(ungzEvents);
|
||||
});
|
||||
|
||||
it('should download resource and untar to given binPath', () => {
|
||||
|
||||
untar({ opts: { binPath: './bin', binName: 'command' }, req: { pipe }, onSuccess, onError });
|
||||
|
||||
expect(tar.Extract).toHaveBeenCalledWith({ path: './bin' });
|
||||
});
|
||||
|
||||
it('should call onSuccess on untar end', () => {
|
||||
|
||||
untar({ opts: { binPath: './bin', binName: 'command' }, req: { pipe }, onSuccess, onError });
|
||||
|
||||
untarEvents.emit('end');
|
||||
|
||||
expect(onSuccess).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should call onError with error on ungz error', () => {
|
||||
|
||||
const error = new Error();
|
||||
|
||||
untar({ opts: { binPath: './bin', binName: 'command' }, req: { pipe }, onSuccess, onError });
|
||||
|
||||
ungzEvents.emit('error', error);
|
||||
|
||||
expect(onError).toHaveBeenCalledWith(error);
|
||||
});
|
||||
|
||||
it('should call onError with error on untar error', () => {
|
||||
|
||||
const error = new Error();
|
||||
|
||||
untar({ opts: { binPath: './bin', binName: 'command' }, req: { pipe }, onSuccess, onError });
|
||||
|
||||
untarEvents.emit('error', error);
|
||||
|
||||
expect(onError).toHaveBeenCalledWith(error);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user