node-query/test/helpers_test.js

159 lines
4.0 KiB
JavaScript
Raw Normal View History

/* eslint-env node, mocha */
'use strict';
const chai = require('chai');
const assert = chai.assert;
const expect = chai.expect;
let Helpers = require('../lib/Helpers');
2017-02-28 15:47:29 -05:00
describe('Helper Module Tests -', () => {
describe('Type-checking methods -', () => {
describe('Object wrappers are listed as their native type', () => {
it('Boolean Wrapper returns \'boolean\' not \'object\'', () => {
let item = Boolean(true);
expect(Helpers.type(item)).to.deep.equal('boolean');
});
2017-02-28 15:47:29 -05:00
it('Number Wrapper returns \'number\' not \'object\'', () => {
let item = Number(4867);
expect(Helpers.type(item)).to.deep.equal('number');
});
2017-02-28 15:47:29 -05:00
it('String Wrapper returns \'string\' not \'object\'', () => {
2015-12-07 17:03:36 -05:00
let item = String('Foo');
expect(Helpers.type(item)).to.deep.equal('string');
});
});
2017-02-28 15:47:29 -05:00
describe('is..Method methods exist -', () => {
2015-12-07 17:03:36 -05:00
let types = [
'Null',
'Undefined',
'Object',
'Array',
'String',
'Number',
'Boolean',
'Function',
'RegExp',
'NaN',
'Infinite'
2015-12-07 17:03:36 -05:00
];
types.forEach(type => {
2017-02-28 15:47:29 -05:00
it(`is${type} method exists`, () => {
assert.ok(Helpers[`is${type}`]);
});
});
});
2017-02-28 15:47:29 -05:00
describe('isScalar -', () => {
let trueCases = {
'Strings are scalar': 'foo',
'Booleans are scalar': true,
'Numbers are scalar': 545
};
Object.keys(trueCases).forEach(desc => {
2017-02-28 15:47:29 -05:00
it(desc, () => {
expect(Helpers.isScalar(trueCases[desc])).to.be.true;
});
});
let falseCases = {
'Arrays are not scalar': [],
'Objects are not scalar': []
};
Object.keys(falseCases).forEach(desc => {
2017-02-28 15:47:29 -05:00
it(desc, () => {
expect(Helpers.isScalar(falseCases[desc])).to.be.false;
});
});
});
2017-02-28 15:47:29 -05:00
describe('isInfinity -', () => {
it('The type of 1/0 is infinity', () => {
expect(Helpers.type(1 / 0)).to.equal('infinity');
});
2017-02-28 15:47:29 -05:00
it('isInfinity is the same as isInfinite', () => {
expect(Helpers.isInfinite(1 / 0)).to.be.true;
});
});
2017-02-28 15:47:29 -05:00
describe('isNaN -', () => {
it('The type of 0 / 0 is NaN', () => {
expect(Helpers.type(0 / 0)).to.equal('nan');
});
2017-02-28 15:47:29 -05:00
it('isNaN method agrees with type', () => {
expect(Helpers.isNaN(0 / 0)).to.be.true;
});
});
});
2017-02-28 15:47:29 -05:00
describe('Other helper methods -', () => {
describe('stringTrim -', () => {
it('stringTrim method works as expected', () => {
let orig = [' x y ', 'z ', ' q'];
let ret = ['x y', 'z', 'q'];
expect(orig.map(Helpers.stringTrim)).to.be.deep.equal(ret);
});
});
2017-02-28 15:47:29 -05:00
describe('arrayPluck -', () => {
2015-12-07 17:03:36 -05:00
let orig = [
{
foo: 1
2015-12-07 17:03:36 -05:00
}, {
foo: 2,
bar: 10
2015-12-07 17:03:36 -05:00
}, {
foo: 3,
bar: 15
}
2015-12-07 17:03:36 -05:00
];
2017-02-28 15:47:29 -05:00
it('Finding members in all objects', () => {
expect(Helpers.arrayPluck(orig, 'foo')).to.be.deep.equal([1, 2, 3]);
});
2017-02-28 15:47:29 -05:00
it('Some members are missing in some objects', () => {
expect(Helpers.arrayPluck(orig, 'bar')).to.be.deep.equal([10, 15]);
});
2017-02-28 15:47:29 -05:00
it('Empty case', () => {
expect(Helpers.arrayPluck([], 'apple')).to.be.deep.equal([]);
});
});
2017-02-28 15:47:29 -05:00
describe('regexInArray -', () => {
let orig = ['apple', ' string ', 6, 4, 7];
2015-12-07 17:03:36 -05:00
let cases = [
{
'Dollar sign is not in any of the array items': /\$/,
'None of the numbers in the array match /5/': /5/
2015-12-07 17:03:36 -05:00
}, {
'\' string \' matches /^ ?string/': /^ ?string/,
'\'apple\' matches /APPLE/i': /APPLE/i
}
2015-12-07 17:03:36 -05:00
];
[0, 1].forEach(i => {
let boolCase = cases[i];
Object.keys(boolCase).forEach(desc => {
2017-02-28 15:47:29 -05:00
it(desc, () => {
if (i) {
expect(Helpers.regexInArray(orig, boolCase[desc])).to.be.true;
} else {
expect(Helpers.regexInArray(orig, boolCase[desc])).to.be.false;
}
});
});
});
2017-02-28 15:47:29 -05:00
it('First argument is not an array', () => {
expect(Helpers.regexInArray(5, /5/)).to.be.false;
});
2017-02-28 15:47:29 -05:00
it('Array is empty', () => {
expect(Helpers.regexInArray([], /.*/)).to.be.false;
});
});
2017-02-28 15:47:29 -05:00
describe('upperCaseFirst -', () => {
it('Capitalizes only the first letter of the string', () => {
expect(Helpers.upperCaseFirst('foobar')).to.equal('Foobar');
expect(Helpers.upperCaseFirst('FOOBAR')).to.equal('FOOBAR');
2016-03-11 16:32:38 -05:00
});
});
});
});