dl
Version:
DreamLab Libs
103 lines (84 loc) • 3.21 kB
JavaScript
describe('Uuid', function() {
var Uuid = null;
var normalizeField = function(value, length) {
var sValue = value.toString();
if (sValue.length < length) {
for(var i = sValue.length ; i < length ; i++) {
sValue = '0' + sValue;
}
} else if (sValue.length > length) {
return sValue.slice(-length);
}
return sValue;
};
it('require', function () {
expect(function () {
Uuid = require('../../lib/events/Uuid.js').Uuid;
}).not.toThrow();
});
describe('isValid', function () {
it('should return true', function(){
var uuid = '201511041543463453300964';
expect(Uuid.isValid(uuid)).toBeTruthy();
});
it('should return false', function(){
var uuid = null;
expect(Uuid.isValid(uuid)).toBeFalsy();
});
it('should return false', function(){
var uuid = '1234111';
expect(Uuid.isValid(uuid)).toBeFalsy();
});
it('should return true', function(){
var uuid = '20151104154346345330096a';
expect(Uuid.isValid(uuid)).toBeFalsy();
});
});
describe('isFresh', function () {
it('should return true', function(){
var uuid = [];
var date = new Date();
uuid.push(date.getFullYear().toString());
uuid.push(normalizeField(date.getMonth() + 1, 2));
uuid.push(normalizeField(date.getDate(), 2));
uuid.push(normalizeField(date.getHours(), 2));
uuid.push(normalizeField(0, 14));
uuid = uuid.join('');
expect(Uuid.isFresh(uuid)).toBeTruthy();
});
it('should return false', function(){
var uuid = [];
var date = new Date();
date.setDate(date.getDate() - 5);
uuid.push(date.getFullYear().toString());
uuid.push(normalizeField(date.getMonth() + 1, 2));
uuid.push(normalizeField(date.getDate(), 2));
uuid.push(normalizeField(date.getHours(), 2));
uuid.push(normalizeField(0, 14));
uuid = uuid.join('');
expect(Uuid.isFresh(uuid)).toBeFalsy();
});
it('should return false', function(){
var uuid = '12344343';
expect(Uuid.isFresh(uuid)).toBeFalsy();
});
it('should return false', function(){
var uuid = null;
expect(Uuid.isFresh(uuid)).toBeFalsy();
});
});
describe('getCounter', function () {
it('should return 13', function(){
var uuid = '201511131200001300000000';
expect(Uuid.getCounter(uuid, 2)).toBe(13);
});
it('should return 1300', function(){
var uuid = '201511131200001300000000';
expect(Uuid.getCounter(uuid, 4)).toBe(1300);
});
it('should return -1', function(){
var uuid = 'fuuu';
expect(Uuid.getCounter(uuid, 4)).toBe(-1);
});
});
});