Browse Source

Adds additional util tests

mbroad/unittest/lib/shared
MacLeod Broad 6 years ago
parent
commit
c19e5243f5
No known key found for this signature in database GPG Key ID: F1B295D13C3CC9CF
2 changed files with 166 additions and 1 deletions
  1. +1
    -1
      lib/util.js
  2. +165
    -0
      test/util.test.js

+ 1
- 1
lib/util.js View File

@ -1237,7 +1237,7 @@ exports.insertSort = function (a,compare) {
a[j] = k;
}
return a;
}
};
/**

+ 165
- 0
test/util.test.js View File

@ -676,4 +676,169 @@ describe('mergeOptions', function () {
assert.equal(util.getType(), 'undefined');
});
});
describe('easingFunctions', function () {
it('take a number and output a number', function () {
for (var key in util.easingFunctions) {
if (util.easingFunctions.hasOwnProperty(key)) {
assert.equal(typeof util.easingFunctions[key](1), 'number');
assert.equal(typeof util.easingFunctions[key](0.2), 'number');
}
}
});
});
describe('getScrollBarWidth', function () {
beforeEach(function() {
this.jsdom_global = jsdom_global();
});
afterEach(function() {
this.jsdom_global();
});
it('returns 0 when there is no content', function () {
assert.equal(util.getScrollBarWidth(), 0);
});
});
describe('equalArray', function () {
it('arrays of different lengths are not equal', function () {
assert.equal(util.equalArray([1, 2, 3], [1, 2]), false)
});
it('arrays with different content are not equal', function () {
assert.equal(util.equalArray([1, 2, 3], [3, 2, 1]), false)
});
it('same content arrays are equal', function () {
assert(util.equalArray([1, 2, 3], [1, 2, 3]))
});
it('empty arrays are equal', function () {
assert(util.equalArray([], []))
});
it('the same array is equal', function () {
var arr = [1, 2, 3];
assert(util.equalArray(arr, arr))
});
});
describe('asBoolean', function () {
it('resolves value from a function', function () {
assert(util.option.asBoolean(function () {return true}, false));
});
it('returns default value for null', function () {
assert(util.option.asBoolean(null, true));
});
it('returns true for other types', function () {
assert(util.option.asBoolean('should be true', false));
});
it('returns null for undefined', function () {
assert.equal(util.option.asBoolean(), null);
});
});
describe('asNumber', function () {
it('resolves value from a function', function () {
assert.equal(util.option.asNumber(function () {return 777}, 13), 777);
});
it('returns default value for null', function () {
assert.equal(util.option.asNumber(null, 13), 13);
});
it('returns number for other types', function () {
assert.equal(util.option.asNumber('777', 13), 777);
});
it('returns default for NaN', function () {
assert.equal(util.option.asNumber(NaN, 13), 13);
});
it('returns null for undefined', function () {
assert.equal(util.option.asNumber(), null);
});
});
describe('asString', function () {
it('resolves value from a function', function () {
assert.equal(util.option.asString(function () {return 'entered'}, 'default'), 'entered');
});
it('returns default value for null', function () {
assert.equal(util.option.asString(null, 'default'), 'default');
});
it('returns string for other types', function () {
assert.equal(util.option.asString(777, 'default'), '777');
});
it('returns default for undefined', function () {
assert.equal(util.option.asString(undefined, 'default'), 'default');
});
it('returns null for undefined', function () {
assert.equal(util.option.asString(), null);
});
});
describe('asSize', function () {
it('resolves value from a function', function () {
assert.equal(util.option.asSize(function () {return '100px'}, '50px'), '100px');
});
it('returns default value for null', function () {
assert.equal(util.option.asSize(null, '50px'), '50px');
});
it('returns string with px for other number', function () {
assert.equal(util.option.asSize(100, '50px'), '100px');
});
it('returns default for undefined', function () {
assert.equal(util.option.asSize(undefined, '50px'), '50px');
});
it('returns null for undefined', function () {
assert.equal(util.option.asSize(), null);
});
});
describe('asElement', function () {
before(function() {
this.jsdom_global = jsdom_global();
this.value = document.createElement("div");
this.defaultValue = document.createElement("div");
});
it('resolves value from a function', function () {
var me = this;
assert.equal(util.option.asElement(function () {return me.value}, this.defaultValue), this.value);
});
it('returns Element', function () {
assert.equal(util.option.asElement(this.value, this.defaultValue), this.value);
});
it('returns default value for null', function () {
assert.equal(util.option.asElement(null, this.defaultValue), this.defaultValue);
});
it('returns null for undefined', function () {
assert.equal(util.option.asElement(), null);
});
});
describe('binarySearchValue', function () {
it('Finds center target on odd sized array', function () {
assert.equal(
util.binarySearchValue(
[{id: 'a', val: 0}, {id: 'b', val: 1}, {id: 'c', val: 2}],
1,
'val'
),
1
);
});
it('Finds target on odd sized array', function () {
assert.equal(
util.binarySearchValue(
[{id: 'a', val: 0}, {id: 'b', val: 1}, {id: 'c', val: 2}],
2,
'val'
),
2
);
});
it('Cannot find target', function () {
assert.equal(
util.binarySearchValue(
[{id: 'a', val: 0}, {id: 'b', val: 1}, {id: 'c', val: 2}],
7,
'val'
),
-1
);
});
});
});

Loading…
Cancel
Save