Skip to content

Commit

Permalink
#67 increasing coverage to >80%
Browse files Browse the repository at this point in the history
  • Loading branch information
itayw committed Apr 8, 2014
1 parent d89cbd5 commit 95050a4
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 71 deletions.
16 changes: 9 additions & 7 deletions lib/common/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,13 @@ var stringifyPrimitive = function (v) {
switch (typeof v) {
case 'string':
return v;

case 'boolean':
return v ? 'true' : 'false';

case 'number':
return isFinite(v) ? v : '';

case 'object':
return JSON.stringify(v);

/* istanbul ignore next */
default:
return '';
}
Expand Down Expand Up @@ -69,8 +66,8 @@ querystring.stringify = querystring.encode = function (obj, sep, eq, name) {
}
}
catch (ex) {
/* istanbul ignore next */
console.log(ex);
console.log(ex.stack);
}
};
/* END OF Add support for JSON parsing of query string */
Expand Down Expand Up @@ -126,6 +123,7 @@ api.fetch = function (endpoint, objOptions, callback) {
}
}
else {
/* istanbul ignore next */
api.waitingRequests.push(arguments);
}
};
Expand All @@ -143,8 +141,12 @@ api.getJSON = function (options, objOptions, callback) {
if (!joolaio.io || joolaio.options.ajax || options.ajax) {
var qs = querystring.stringify(objOptions);
options.path += '?' + qs;
options.headers['joolaio-token'] = joolaio.TOKEN;
options.headers['joolaio-apitoken'] = joolaio.APITOKEN;
if (options && options.headers) {
options.headers['joolaio-token'] = joolaio.TOKEN;
options.headers['joolaio-apitoken'] = joolaio.APITOKEN;
}
else
return callback(new Error('Failed to set request headers'));
// options.headers['Content-Length'] = qs.length;
var timerID, aborted;
try {
Expand Down
56 changes: 1 addition & 55 deletions lib/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,8 @@ common.extend = common._extend;

require('./modifiers');

common.mixin = function (origin, add, overwrite) {
// Don't do anything if add isn't an object
if (!add || typeof add !== 'object') return origin;

var keys = Object.keys(add);
var i = 0;//keys.length;
while (i < keys.length) {
if (origin.hasOwnProperty(keys[i])) {
if (overwrite)
origin[keys[i]] = add[keys[i]];
//else
//common.extend(origin[keys[i]], add[keys[i]]);

}
else
origin[keys[i]] = add[keys[i]];
i++;
}
return origin;
};

//hook functions for timings
/* istanbul ignore next */
common.hookEvents = function (obj) {
if (!obj)
return;
Expand Down Expand Up @@ -102,37 +82,3 @@ common.parse = function (string, callback) {
common.hash = function (string) {
return require('crypto').createHash('md5').update(string).digest("hex");
};

common.toType = (function toType(global) {
return function (obj) {
if (obj === global) {
return "global";
}
return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
};
})(this);

common.sanitize = function (obj) {
if (obj instanceof Object) {
for (var k in obj) {
if (obj.hasOwnProperty(k)) {
//recursive call to scan property
if (k.substring(0, 1) === '_' && k !== '_')
delete obj[k];
else
common.sanitize(obj[k]);
}
}
}
};

common.typeof = function (obj) {
if (typeof(obj) == "object") {
if (obj === null) return "null";
if (obj.constructor == ([]).constructor) return "array";
if (obj.constructor == (new Date()).constructor) return "date";
if (obj.constructor == (new RegExp()).constructor) return "regex";
return "object";
}
return typeof(obj);
};
26 changes: 17 additions & 9 deletions test/unit/1_globals/globals.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
describe("globals", function () {
it("should have sinon defined", function () {
expect(sinon).to.be.ok;
});
it("should have sinon defined", function () {
expect(sinon).to.be.ok;
});

it("should have expect defined", function () {
expect(expect).to.be.ok;
});
it("should have expect defined", function () {
expect(expect).to.be.ok;
});

it("should have underscore defined", function () {
expect(_).to.be.ok;
});
it("should have underscore defined", function () {
expect(_).to.be.ok;
});

it("should set timezone [0]", function () {
joolaio.timezone(0);
});

it("should set timezone [2]", function () {
joolaio.timezone(2);
});
});
50 changes: 50 additions & 0 deletions test/unit/2_common/api.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
describe("api", function () {
before(function () {
this._enabled = joolaio.options.debug.enabled;
joolaio.options.debug.enabled = true;
});

it("should fetch api", function (done) {
joolaio.api.fetch('/api/system/nodeDetails', {}, function (err, result) {
console.log(err, result);
done();
});
});

it("should fail to fetch api w/ wrong token", function (done) {
joolaio.api.fetch('/api/system/nodeDetails?token=1234', {}, function (err, result) {
console.log(err, result);
done();
});
});

it("should test stringify of different types", function (done) {
var options = {
ajax: true
};
var objOptions = {
a: 'test',
b: 123,
c: true,
d: {
a: 1
},
e: []
};
joolaio.api.getJSON(options, objOptions, function (err, result) {
console.log(err, result);
done();
});
});

it("should test stringify of different string", function (done) {
var options = {
ajax: true
};
var objOptions = 'test';
joolaio.api.getJSON(options, objOptions, function (err, result) {
console.log(err, result);
done();
});
});
});
60 changes: 60 additions & 0 deletions test/unit/2_common/dateformat.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* joola.io
*
* Copyright Joola Smart Solutions, Ltd. <[email protected]>
*
* Licensed under GNU General Public License 3.0 or later.
* Some rights reserved. See LICENSE, AUTHORS.
*
* @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
*/


describe("common-dateformat", function () {
it("should format a date correctly yyyy-mm-dd", function () {
var date = new Date();

var yyyy = date.getFullYear(), m = (date.getMonth() + 1), d = date.getDate();

var mm = m < 10 ? "0" + m : m;
var dd = d < 10 ? "0" + d : d;

var expected = yyyy + '-' + mm + '-' + dd;
var actual = date.format('yyyy-mm-dd');
expect(actual).to.equal(expected);
});

it("should format a date correctly yyyy-mm-dd hh:nn:ss", function () {
var date = new Date();

var yyyy = date.getFullYear(), m = (date.getMonth() + 1), d = date.getDate(), h = date.getHours(), n = date.getMinutes(), s = date.getSeconds();

var mm = m < 10 ? "0" + m : m;
var dd = d < 10 ? "0" + d : d;
var hh = h < 10 ? "0" + h : h;
var nn = n < 10 ? "0" + n : n;
var ss = s < 10 ? "0" + s : s;

var expected = yyyy + '-' + mm + '-' + dd + ' ' + hh + ':' + nn + ':' + ss;
var actual = date.format('yyyy-mm-dd hh:nn:ss');
expect(actual).to.equal(expected);
});

it("should format a date correctly yyyy-mm-dd hh:nn:ss.fff", function () {
var date = new Date();

var yyyy = date.getFullYear(), m = (date.getMonth() + 1), d = date.getDate(), h = date.getHours(), n = date.getMinutes(), s = date.getSeconds(), f = date.getMilliseconds();

var mm = m < 10 ? "0" + m : m;
var dd = d < 10 ? "0" + d : d;
var hh = h < 10 ? "0" + h : h;
var nn = n < 10 ? "0" + n : n;
var ss = s < 10 ? "0" + s : s;
var ff = f < 10 ? "0" + f : f;
var fff = ff < 100 ? "0" + ff : ff;

var expected = yyyy + '-' + mm + '-' + dd + ' ' + hh + ':' + nn + ':' + ss + '.' + fff;
var actual = date.format('yyyy-mm-dd hh:nn:ss.fff');
expect(actual).to.equal(expected);
});
});
53 changes: 53 additions & 0 deletions test/unit/2_common/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
describe("common", function () {
it("should async stringify objects", function (done) {
var obj = {
a: 1,
b: 2
};

joola.common.stringify(obj, function (err, str) {
expect(str).to.equal(JSON.stringify(obj));
done();
});
});

it("should async parse json string", function (done) {
var obj = {
a: 1,
b: 2
};
var str = JSON.stringify(obj);

joola.common.parse(str, function (err, _obj) {
expect(str).to.equal(JSON.stringify(_obj));
done();
});
});

it("should hash strings correctly", function () {
var expected = '26207976637e23e1bed51683c33a6d73';
var actual = joola.common.hash('thisisatestforhash');
expect(actual).to.equal(expected);
});

it("should generate uuid - 9 chars long", function () {
var expected = 9;
var actual = joola.common.uuid().length;
expect(actual).to.equal(expected);
});

it("should generate uuid - unique", function (done) {
var ids = [];

for (var i = 0; i < 3200; i++) {
var uuid = joola.common.uuid();
if (ids.indexOf(uuid) > -1) {
return done(new Error('Found duplicate uuid [' + uuid + ']'));
}
ids.push(uuid);
}

return done();
});

});
7 changes: 7 additions & 0 deletions test/unit/2_common/modifiers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe("modifiers", function () {
it("should format toJSON", function (done) {
var err = new Error('This is a test error');
console.log(err.toJSON());
done();
});
});

0 comments on commit 95050a4

Please sign in to comment.