Skip to content

Commit

Permalink
ADD unit testing for resource strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
RecuencoJones committed Mar 21, 2019
1 parent 6204be6 commit 9071f9b
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 2 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
node_modules
src
__test__
127 changes: 127 additions & 0 deletions __test__/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
const rewire = require('rewire');
const EventEmitter = require('events').EventEmitter;

describe('go-npm', function() {
let mod;

beforeEach(function() {
mod = rewire('../src/index.js');
});

describe('Resource handling strategies', function() {

describe('untarStrategy()', function() {

let untarStrategy, ungzEvents, untarEvents, pipe, callback, zlib, createGunzip, tar;

beforeEach(function() {
untarStrategy = mod.__get__('untarStrategy');
zlib = mod.__get__('zlib');
tar = mod.__get__('tar');
ungzEvents = new EventEmitter();
untarEvents = new EventEmitter();

createGunzip = jest.fn();
pipe = jest.fn();
callback = jest.fn();

pipe.mockReturnValueOnce({ pipe });
createGunzip.mockReturnValueOnce(ungzEvents);
jest.spyOn(tar, 'Extract').mockReturnValueOnce(untarEvents);

// jest.spyOn not working on read-only properties
Object.defineProperty(zlib, 'createGunzip', { value: createGunzip });
});

it('should download resource and untar to given binPath', function() {

untarStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback);

expect(tar.Extract).toHaveBeenCalledWith({ path: './bin' });
});

it('should call verifyAndPlaceBinary on untar end', function() {

const verifyAndPlaceBinary = jest.fn();

mod.__set__('verifyAndPlaceBinary', verifyAndPlaceBinary);

untarStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback);

untarEvents.emit('end');

expect(verifyAndPlaceBinary).toHaveBeenCalledWith('command', './bin', callback);
});

it('should call callback with error on ungz error', function() {

const error = new Error();

untarStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback);

ungzEvents.emit('error', error);

expect(callback).toHaveBeenCalledWith(error);
});

it('should call callback with error on untar error', function() {

const error = new Error();

untarStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback);

untarEvents.emit('error', error);

expect(callback).toHaveBeenCalledWith(error);
});
});

describe('moveStrategy()', function() {

let moveStrategy, streamEvents, pipe, callback, fs;

beforeEach(function() {

moveStrategy = mod.__get__('moveStrategy');
fs = mod.__get__('fs');
streamEvents = new EventEmitter();

pipe = jest.fn();
callback = jest.fn();

jest.spyOn(fs, 'createWriteStream').mockReturnValueOnce(streamEvents);
});

it('should download resource to given binPath', function() {

moveStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback);

expect(fs.createWriteStream).toHaveBeenCalledWith('bin/command');
});

it('should call verifyAndPlaceBinary on stream closed', function() {

const verifyAndPlaceBinary = jest.fn();

mod.__set__('verifyAndPlaceBinary', verifyAndPlaceBinary);

moveStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback);

streamEvents.emit('close');

expect(verifyAndPlaceBinary).toHaveBeenCalledWith('command', './bin', callback);
});

it('should call callback with error on write stream error', function() {

const error = new Error();

moveStrategy({ binPath: './bin', binName: 'command' }, { pipe }, callback);

streamEvents.emit('error', error);

expect(callback).toHaveBeenCalledWith(error);
});
});
});
});
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"go-npm": "./bin/index.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "jest",
"prepublish": "node_modules/babel-cli/bin/babel.js src --out-dir bin --presets es2015"
},
"author": "Sanath Kumar Ramesh <dayanandasaraswati@gmail.com>",
Expand All @@ -25,6 +25,8 @@
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-preset-es2015": "^6.24.1"
"babel-preset-es2015": "^6.24.1",
"jest": "^24.5.0",
"rewire": "^4.0.1"
}
}

0 comments on commit 9071f9b

Please sign in to comment.