node-query/test/helpers_test.js

152 lines
3.9 KiB
JavaScript
Raw Normal View History

const 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)).toEqual('boolean');
});
2017-02-28 15:47:29 -05:00
it('Number Wrapper returns \'number\' not \'object\'', () => {
let item = Number(4867);
expect(Helpers.type(item)).toEqual('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)).toEqual('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`, () => {
expect(Helpers[`is${type}`]).toBeDefined();
});
});
});
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])).toBe(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])).toBe(false);
});
});
});
2017-02-28 15:47:29 -05:00
describe('isInfinity -', () => {
it('The type of 1/0 is infinity', () => {
expect(Helpers.type(1 / 0)).toBe('infinity');
});
2017-02-28 15:47:29 -05:00
it('isInfinity is the same as isInfinite', () => {
expect(Helpers.isInfinite(1 / 0)).toBe(true);
});
});
2017-02-28 15:47:29 -05:00
describe('isNaN -', () => {
it('The type of 0 / 0 is NaN', () => {
expect(Helpers.type(0 / 0)).toBe('nan');
});
2017-02-28 15:47:29 -05:00
it('isNaN method agrees with type', () => {
expect(Helpers.isNaN(0 / 0)).toBe(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)).toEqual(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')).toEqual([1, 2, 3]);
});
2017-02-28 15:47:29 -05:00
it('Some members are missing in some objects', () => {
expect(Helpers.arrayPluck(orig, 'bar')).toEqual([10, 15]);
});
2017-02-28 15:47:29 -05:00
it('Empty case', () => {
expect(Helpers.arrayPluck([], 'apple')).toEqual([]);
});
});
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])).toBe(true);
} else {
expect(Helpers.regexInArray(orig, boolCase[desc])).toBe(false);
}
});
});
});
2017-02-28 15:47:29 -05:00
it('First argument is not an array', () => {
expect(Helpers.regexInArray(5, /5/)).toBe(false);
});
2017-02-28 15:47:29 -05:00
it('Array is empty', () => {
expect(Helpers.regexInArray([], /.*/)).toBe(false);
});
});
2017-02-28 15:47:29 -05:00
describe('upperCaseFirst -', () => {
it('Capitalizes only the first letter of the string', () => {
expect(Helpers.upperCaseFirst('foobar')).toBe('Foobar');
expect(Helpers.upperCaseFirst('FOOBAR')).toBe('FOOBAR');
2016-03-11 16:32:38 -05:00
});
});
});
});