Skip to content

Commit

Permalink
fixes #78 : Add UnitTests for modules
Browse files Browse the repository at this point in the history
Refactoring local storage module and unit tests to conform to Common JS control flow instead of using event based callback
  • Loading branch information
remie committed Sep 20, 2015
1 parent 13b22b2 commit 84d097d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
9 changes: 2 additions & 7 deletions lib/localstorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ var path = require('path');
var mime = require('mime');
var _ = require("lodash");
var archiver = require('archiver');
var EventEmitter = require('events').EventEmitter;
var util = require('util');

// ------------------------------------------------------------------------------------------ Module Exposure

Expand All @@ -21,7 +19,6 @@ module.exports = function(options) {
// ------------------------------------------------------------------------------------------ Module definition

function LocalFileStorage(options) {
EventEmitter.call(this);
options = options || {};
this.options = options;

Expand All @@ -34,8 +31,6 @@ function LocalFileStorage(options) {
}
}

util.inherits(LocalFileStorage, EventEmitter);

LocalFileStorage.prototype.upload = function(file, context, next) {
var basedir = path.dirname(context.path);
fs.mkdir(basedir, function() {
Expand Down Expand Up @@ -129,7 +124,7 @@ LocalFileStorage.prototype.download = function(token, res, next) {
}
};

LocalFileStorage.prototype.purge = function() {
LocalFileStorage.prototype.purge = function(next) {
var self = this;
var basedir = this.localstoragepath;
fs.readdir(basedir, function(err, files) {
Expand All @@ -149,7 +144,7 @@ LocalFileStorage.prototype.purge = function() {
});

del(filesToDelete, function(err, files) {
self.emit('localstorage.purged', err, filesToDelete);
next(err, filesToDelete);
});
});
};
20 changes: 8 additions & 12 deletions test/modules/localstorage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ describe('YouTransfer Local Storage module', function() {

// -------------------------------------------------------------------------------------- Testing constructor

it('should be possible to set options by Object', function() {
it('should accept options by Object', function() {
var instance = localstorage({ localstoragepath: __dirname });
instance.localstoragepath.should.equals(__dirname);
});

it('should be possible to set options by Null Object', function() {
it('should accept options by Null Object', function() {
var instance = localstorage(null);
instance.localstoragepath.should.equals(path.resolve('./lib'));
});

it('should be possible to set options by empty Object', function() {
it('should accept options by empty Object', function() {
var instance = localstorage({});
instance.localstoragepath.should.equals(path.resolve('./lib'));
});

it('should be possible to set options by String', function() {
it('should accept options by String', function() {
var instance = localstorage(__dirname);
instance.localstoragepath.should.equals(__dirname);
});
Expand All @@ -65,7 +65,7 @@ describe('YouTransfer Local Storage module', function() {

// -------------------------------------------------------------------------------------- Testing file upload

it('should be possible to upload a file', function(done) {
it('should implement the "upload" method and enable local storage of a file', function(done) {
var uploadedFile = {
path: path.join(__dirname, 'file.tmp'),
data: 'my awesome content',
Expand Down Expand Up @@ -434,13 +434,12 @@ describe('YouTransfer Local Storage module', function() {
callback(null, JSON.stringify({ expires: 1234, path: 'file.binary', jsonPath: 'file.json' }));
});

provider.on('localstorage.purged', function(err, paths) {
provider.purge(function(err, paths) {
var paths = JSON.stringify(paths);
paths.should.equals('["file.binary","file.json"]');
done();
});

provider.purge();
});

it('should not purge files if there is no expire date set', function(done) {
Expand All @@ -453,13 +452,11 @@ describe('YouTransfer Local Storage module', function() {
callback(null, JSON.stringify({ path: 'file.binary', jsonPath: 'file.json' }));
});

provider.on('localstorage.purged', function(err, paths) {
provider.purge(function(err, paths) {
var paths = JSON.stringify(paths);
paths.should.equals('[]');
done();
});

provider.purge();
});

it('should not purge files if there is expire date is set in the future', function(done) {
Expand All @@ -472,13 +469,12 @@ describe('YouTransfer Local Storage module', function() {
callback(null, JSON.stringify({ expires: Date.tomorrow(), path: 'file.binary', jsonPath: 'file.json' }));
});

provider.on('localstorage.purged', function(err, paths) {
provider.purge(function(err, paths) {
var paths = JSON.stringify(paths);
paths.should.equals('[]');
done();
});

provider.purge();
});

});

0 comments on commit 84d097d

Please sign in to comment.