Skip to content

Commit

Permalink
fixes #78 : Add UnitTests for modules
Browse files Browse the repository at this point in the history
Making adjustments to scheduler module to enable unit testing
  • Loading branch information
remie committed Sep 16, 2015
1 parent fa31372 commit afdaa63
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
66 changes: 38 additions & 28 deletions lib/scheduler.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,52 @@
'use strict';

// ------------------------------------------------------------------------------------------ Dependencies

var _ = require('lodash');
var nconf = require('nconf');
var schedule = require('node-schedule');

module.exports = (function() {
// ------------------------------------------------------------------------------------------ Module definition

var self = {};
var schedules = [];
module.exports = new Scheduler();

self.add = function(name, cron, job) {
if(!schedules[name]) {
schedules[name] = schedule.scheduleJob(cron, job);
return true;
} else {
return false;
}
}
function Scheduler() {
this.schedules = [];
};

Scheduler.prototype.get = function(name) {
return this.schedules[name];
};

self.clear = function(name) {
if(name) {
schedules[name].cancel();
schedules = _.remove(schedules, schedules[name]);
} else {
_.each(schedules, function(schedule) {
schedule.cancel();
});
schedules = Array();
}
Scheduler.prototype.add = function(name, cron, job) {
if(!this.schedules[name]) {
this.schedules[name] = schedule.scheduleJob(name, cron, job);
return true;
} else {
return false;
}
};

self.reschedule = function(name, cron, job) {
if(schedules[name]) {
self.clear(name);
}
return self.add(name, cron, job);
Scheduler.prototype.remove = function(name) {
this.clear(name);
};

Scheduler.prototype.clear = function(name) {
if(name) {
this.schedules[name].cancel();
this.schedules = _.remove(this.schedules, this.schedules[name]);
} else {
_.each(this.schedules, function(schedule) {
schedule.cancel();
});
this.schedules = Array();
}
return true;
};

return self;
})()
Scheduler.prototype.reschedule = function(name, cron, job) {
if(this.schedules[name]) {
this.clear(name);
}
return this.add(name, cron, job);
};
10 changes: 10 additions & 0 deletions test/modules/settings.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@

// ------------------------------------------------------------------------------------------ Test Dependencies

var should = require('chai').should();
var settings = require('../../lib/settings.js')({
path: './test/modules/settings.json'
});

// ------------------------------------------------------------------------------------------ Test Definition

describe('YouTransfer Settings module', function() {
var title = "My Awesome Title";

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

it('should be possible to set options', function() {
settings.options.path.should.equal('./test/modules/settings.json');
});

// -------------------------------------------------------------------------------------- Testing write

it('should be possible to set title', function() {
settings.push({ title: title }, function(err) {
should.not.exist(err);
});
});

// -------------------------------------------------------------------------------------- Testing read

it('should be possible to get title', function() {
settings.get(function(err, output) {
should.not.exist(err);
Expand Down

0 comments on commit afdaa63

Please sign in to comment.