Fixed tests
This commit is contained in:
82
tests/array.spec.ts
Normal file
82
tests/array.spec.ts
Normal file
@ -0,0 +1,82 @@
|
||||
import {addUnique, caseInsensitiveSort, flattenArr, sortByProp} from '../src';
|
||||
|
||||
describe('Array Utilities', () => {
|
||||
describe('addUnique', () => {
|
||||
const arr = [1, 2];
|
||||
|
||||
test('non-unique', () => {
|
||||
addUnique(arr, 1);
|
||||
expect(arr).toStrictEqual([1, 2]);
|
||||
});
|
||||
test('unique', () => {
|
||||
addUnique(arr, 3);
|
||||
expect(arr).toStrictEqual([1, 2, 3]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('flattenArr', () => {
|
||||
test('flat array', () => expect(flattenArr([1, 2])).toStrictEqual([1, 2]));
|
||||
test('2D array', () => expect(flattenArr([[1, 2], [3, 4]])).toStrictEqual([1, 2, 3, 4]));
|
||||
test('3D array', () => expect(flattenArr([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8]));
|
||||
test('mixed array', () => expect(flattenArr([1, 2, [3, 4], [[5, 6], [7, 8]]])).toStrictEqual([1, 2, 3, 4, 5, 6, 7, 8]));
|
||||
});
|
||||
|
||||
describe('sortByProp', () => {
|
||||
test('random letters', () => {
|
||||
let unsorted: any = Array(100).fill(null)
|
||||
.map(() => String.fromCharCode(Math.round(Math.random() * 25) + 97));
|
||||
const sorted = unsorted.sort((a: any, b: any) => {
|
||||
if(a > b) return 1;
|
||||
if(a < b) return -1;
|
||||
return 0;
|
||||
}).map((l: any) => ({a: l}));
|
||||
unsorted = unsorted.map((l: any) => ({a: l}));
|
||||
expect(unsorted.sort(sortByProp('a'))).toStrictEqual(sorted);
|
||||
});
|
||||
test('random letters reversed', () => {
|
||||
let unsorted: any = Array(100).fill(null)
|
||||
.map(() => String.fromCharCode(Math.round(Math.random() * 25) + 97));
|
||||
const sorted = unsorted.sort((a: any, b: any) => {
|
||||
if(a > b) return -1;
|
||||
if(a < b) return 1;
|
||||
return 0;
|
||||
}).map((n: any) => ({a: n}));
|
||||
unsorted = unsorted.map((n: any) => ({a: n}));
|
||||
expect(unsorted.sort(sortByProp('a', true))).toStrictEqual(sorted);
|
||||
});
|
||||
test('random numbers', () => {
|
||||
let unsorted: any = Array(100).fill(null).map(() => Math.round(Math.random() * 100));
|
||||
const sorted = unsorted.sort((a: any, b: any) => a - b).map((n: any) => ({a: n}));
|
||||
unsorted = unsorted.map((n: any) => ({a: n}));
|
||||
expect(unsorted.sort(sortByProp('a'))).toStrictEqual(sorted);
|
||||
});
|
||||
test('random numbers reversed', () => {
|
||||
let unsorted: any = Array(100).fill(null).map(() => Math.round(Math.random() * 100));
|
||||
const sorted = unsorted.sort((a: any, b: any) => b - a).map((n: any) => ({a: n}));
|
||||
unsorted = unsorted.map((n: any) => ({a: n}));
|
||||
expect(unsorted.sort(sortByProp('a', true))).toStrictEqual(sorted);
|
||||
});
|
||||
});
|
||||
|
||||
describe('caseInsensitiveSort', () => {
|
||||
test('non-string property', () => {
|
||||
const unsorted: any = [{a: 'Apple', b: 123}, {a: 'Carrot', b: 789}, {a: 'banana', b: 456}];
|
||||
const sorted: any = unsorted.map((u: any) => ({...u}));
|
||||
expect(unsorted.sort(caseInsensitiveSort('b'))).toStrictEqual(sorted);
|
||||
});
|
||||
test('simple strings', () => {
|
||||
const unsorted: any = [{a: 'Apple'}, {a: 'Carrot'}, {a: 'banana'}];
|
||||
const sorted: any = unsorted.sort((first: any, second: any) => {
|
||||
return first.a.toLowerCase().localeCompare(second.a.toLowerCase());
|
||||
}).map((u: any) => ({...u}));
|
||||
expect(unsorted.sort(caseInsensitiveSort('a'))).toStrictEqual(sorted);
|
||||
});
|
||||
test('alphanumeric strings', () => {
|
||||
const unsorted: any = [{a: '4pple'}, {a: 'Carrot'}, {a: 'b4n4n4'}];
|
||||
const sorted: any = unsorted.sort((first: any, second: any) => {
|
||||
return first.a.toLowerCase().localeCompare(second.a.toLowerCase());
|
||||
}).map((u: any) => ({...u}));
|
||||
expect(unsorted.sort(caseInsensitiveSort('a'))).toStrictEqual(sorted);
|
||||
});
|
||||
});
|
||||
});
|
36
tests/misc.spec.ts
Normal file
36
tests/misc.spec.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import {sleep, urlParser} from '../src';
|
||||
|
||||
describe('Miscellanies Utilities', () => {
|
||||
describe('sleep', () => {
|
||||
test('wait until', async () => {
|
||||
const wait = ~~(Math.random() * 500);
|
||||
const time = new Date().getTime();
|
||||
await sleep(wait);
|
||||
expect(new Date().getTime()).toBeGreaterThanOrEqual(time + wait);
|
||||
});
|
||||
});
|
||||
|
||||
describe('urlParser', () => {
|
||||
test('localhost w/ port', () => {
|
||||
const parsed = urlParser('http://localhost:4200/some/path?q1=test1&q2=test2#frag');
|
||||
expect(parsed.protocol).toStrictEqual('http');
|
||||
expect(parsed.host).toStrictEqual('localhost:4200');
|
||||
expect(parsed.domain).toStrictEqual('localhost');
|
||||
expect(parsed.port).toStrictEqual(4200);
|
||||
expect(parsed.path).toStrictEqual('/some/path');
|
||||
expect(parsed.query).toStrictEqual({q1: 'test1', q2: 'test2'});
|
||||
expect(parsed.fragment).toStrictEqual('frag');
|
||||
});
|
||||
|
||||
test('advanced URL', () => {
|
||||
const parsed = urlParser('https://sub.domain.example.com/some/path?q1=test1&q2=test2#frag');
|
||||
expect(parsed.protocol).toStrictEqual('https');
|
||||
expect(parsed.host).toStrictEqual('sub.domain.example.com');
|
||||
expect(parsed.domain).toStrictEqual('example.com');
|
||||
expect(parsed.subdomain).toStrictEqual('sub.domain');
|
||||
expect(parsed.path).toStrictEqual('/some/path');
|
||||
expect(parsed.query).toStrictEqual({q1: 'test1', q2: 'test2'});
|
||||
expect(parsed.fragment).toStrictEqual('frag');
|
||||
});
|
||||
});
|
||||
});
|
111
tests/object.spec.ts
Normal file
111
tests/object.spec.ts
Normal file
@ -0,0 +1,111 @@
|
||||
import {clean, deepCopy, dotNotation, flattenObj, includes, isEqual} from "../src";
|
||||
|
||||
describe('Object Utilities', () => {
|
||||
const TEST_OBJECT = {
|
||||
a: 1,
|
||||
b: [
|
||||
[2, 3],
|
||||
[4, 5]
|
||||
],
|
||||
c: {
|
||||
d: [
|
||||
[{e: 6, f: 7}]
|
||||
],
|
||||
},
|
||||
g: {h: 8},
|
||||
i: () => 9
|
||||
};
|
||||
|
||||
describe('clean', () => {
|
||||
test('remove null properties', () => {
|
||||
const a = {a: 1, b: null, c: undefined};
|
||||
const final = {a: 1};
|
||||
expect(clean(a)).toEqual(final);
|
||||
});
|
||||
test('remove undefined properties', () => {
|
||||
const a = {a: 1, b: null, c: undefined};
|
||||
const final = {a: 1, b: null};
|
||||
expect(clean(a, true)).toEqual(final);
|
||||
});
|
||||
});
|
||||
|
||||
describe('deepCopy', () => {
|
||||
const copy = deepCopy(TEST_OBJECT);
|
||||
test('Array of arrays', () => {
|
||||
const a = [[1, 2], [3, 4]];
|
||||
const b = deepCopy(a);
|
||||
b[0][1] = 5;
|
||||
expect(a).not.toEqual(b);
|
||||
});
|
||||
test('Change array inside object', () => {
|
||||
copy.b[1] = [1, 1, 1];
|
||||
expect(copy.b[1]).not.toEqual(TEST_OBJECT.b[1]);
|
||||
});
|
||||
test('Change object inside object', () => {
|
||||
copy.g = {h: Math.random()};
|
||||
expect(copy.g).not.toEqual(TEST_OBJECT.g);
|
||||
});
|
||||
test('Change object property inside nested array', () => {
|
||||
copy.c.d[0][0].e = -1;
|
||||
expect(copy.c.d[0][0].e).not.toEqual(TEST_OBJECT.c.d[0][0].e);
|
||||
});
|
||||
});
|
||||
|
||||
describe('dotNotation', () => {
|
||||
test('no object or properties', () => {
|
||||
expect(dotNotation(undefined, 'z')).toStrictEqual(undefined);
|
||||
expect(dotNotation(TEST_OBJECT, '')).toStrictEqual(undefined);
|
||||
});
|
||||
test('invalid property', () => expect(dotNotation(TEST_OBJECT, 'z')).toBeUndefined());
|
||||
test('by property', () => expect(dotNotation(TEST_OBJECT, 'a')).toBe(TEST_OBJECT.a));
|
||||
test('by key', () => expect(dotNotation(TEST_OBJECT, '["a"]')).toBe(TEST_OBJECT['a']));
|
||||
test('by key (single quote)', () => expect(dotNotation(TEST_OBJECT, '[\'a\']')).toBe(TEST_OBJECT['a']));
|
||||
test('by key (double quote)', () => expect(dotNotation(TEST_OBJECT, '["a"]')).toBe(TEST_OBJECT['a']));
|
||||
test('by index', () => expect(dotNotation(TEST_OBJECT, 'b[0]')).toBe(TEST_OBJECT.b[0]));
|
||||
test('by index (2d)', () => expect(dotNotation(TEST_OBJECT, 'b[1][1]')).toBe(TEST_OBJECT.b[1][1]));
|
||||
test('everything combined', () => expect(dotNotation(TEST_OBJECT, 'c["d"][0][0].e'))
|
||||
.toBe(TEST_OBJECT.c['d'][0][0].e));
|
||||
test('set value', () => {
|
||||
const COPY = JSON.parse(JSON.stringify(TEST_OBJECT));
|
||||
dotNotation(COPY, 'c["d"][0][0].e', 'test');
|
||||
expect(COPY['c']['d'][0][0]['e']).toBe('test');
|
||||
});
|
||||
test('set new value', () => {
|
||||
const COPY = JSON.parse(JSON.stringify(TEST_OBJECT));
|
||||
dotNotation(COPY, 'c.x.y.z', 'test');
|
||||
expect(COPY['c']['x']['y']['z']).toBe('test');
|
||||
});
|
||||
});
|
||||
|
||||
describe('includes', () => {
|
||||
test('simple', () => expect(includes(TEST_OBJECT, {a: 1})).toBeTruthy());
|
||||
test('nested', () => expect(includes(TEST_OBJECT, {g: {h: 8}})).toBeTruthy());
|
||||
test('array', () => expect(includes(TEST_OBJECT, {b: [[2]]})).toBeTruthy());
|
||||
test('nested array', () => expect(includes(TEST_OBJECT, {a: 1, c: {d: [[{e: 6}]]}})).toBeTruthy());
|
||||
test('wong nested array', () => expect(includes(TEST_OBJECT, {a: 1, c: {d: [{e: 7}]}})).toBeFalsy());
|
||||
test('wrong value', () => expect(includes(TEST_OBJECT, {a: 1, b: 2})).toBeFalsy());
|
||||
test('missing value', () => expect(includes(TEST_OBJECT, {a: 1, i: 10})).toBeFalsy());
|
||||
});
|
||||
|
||||
describe('isEqual', () => {
|
||||
test('boolean equal', () => expect(isEqual(true, true)).toBeTruthy());
|
||||
test('boolean not-equal', () => expect(isEqual(true, false)).toBeFalsy());
|
||||
test('number equal', () => expect(isEqual(1, 1)).toBeTruthy());
|
||||
test('number not-equal', () => expect(isEqual(1, 0)).toBeFalsy());
|
||||
test('string equal', () => expect(isEqual('abc', 'abc')).toBeTruthy());
|
||||
test('string not-equal', () => expect(isEqual('abc', '')).toBeFalsy());
|
||||
test('array equal', () => expect(isEqual([true, 1, 'a'], [true, 1, 'a'])).toBeTruthy());
|
||||
test('array not-equal', () => expect(isEqual([true, 1, 'a'], [1])).toBeFalsy());
|
||||
test('object equal', () => expect(isEqual({a: 1, b: 2}, {a: 1, b: 2})).toBeTruthy());
|
||||
test('object not-equal', () => expect(isEqual({a: 1, b: 2}, {a: 1})).toBeFalsy());
|
||||
test('complex', () => expect(isEqual(TEST_OBJECT, TEST_OBJECT)).toBeTruthy());
|
||||
});
|
||||
|
||||
describe('flattenObj', () => {
|
||||
test('simple nested object', () => expect(flattenObj({a: {b: {c: 1}}})).toEqual({"a.b.c": 1}));
|
||||
test('already flat object', () => expect(flattenObj(TEST_OBJECT['g'])).toEqual(TEST_OBJECT['g']));
|
||||
test('non-object input', () => expect(flattenObj(TEST_OBJECT['b'])).toBeUndefined());
|
||||
test('complex nested object', () => expect(flattenObj({a: 1, b: {c: 2}, d: {e: {f: {g: 3}}}}))
|
||||
.toEqual({"a": 1, "b.c": 2, "d.e.f.g": 3}));
|
||||
});
|
||||
});
|
50
tests/string.spec.ts
Normal file
50
tests/string.spec.ts
Normal file
@ -0,0 +1,50 @@
|
||||
import {matchAll, randomString, randomStringBuilder} from "../src";
|
||||
|
||||
describe('String Utilities', () => {
|
||||
describe('randomString', () => {
|
||||
test('length', () => expect(randomString(32).length).toStrictEqual(32));
|
||||
test('distribution', () => {
|
||||
const charList = '123';
|
||||
const random = randomString(32, charList);
|
||||
expect(random.split('').filter(c => c == '1').length).toBeGreaterThan(0);
|
||||
expect(random.split('').filter(c => c == '2').length).toBeGreaterThan(0);
|
||||
expect(random.split('').filter(c => c == '3').length).toBeGreaterThan(0);
|
||||
});
|
||||
test('binary', () => {
|
||||
const randomByte = randomString(8, '01');
|
||||
expect(randomByte.split('').filter(c => c == '0').length).toBeGreaterThan(0);
|
||||
expect(randomByte.split('').filter(c => c == '1').length).toBeGreaterThan(0);
|
||||
expect(randomByte.length).toStrictEqual(8);
|
||||
});
|
||||
});
|
||||
|
||||
describe('randomStringBuilder', () => {
|
||||
test('length', () => {
|
||||
const len = ~~(Math.random() * 32);
|
||||
expect(randomStringBuilder(len, true).length).toStrictEqual(len);
|
||||
});
|
||||
test('no length', () => {
|
||||
expect(randomStringBuilder(0, true)).toStrictEqual('');
|
||||
});
|
||||
test('letters only', () =>
|
||||
expect(/^[a-zA-Z]{10}$/g.test(randomStringBuilder(10, true))).toBeTruthy());
|
||||
test('numbers only', () =>
|
||||
expect(/^[0-9]{10}$/g.test(<any>randomStringBuilder(10, false, true))).toBeTruthy());
|
||||
test('symbols only', () =>
|
||||
expect(/^[^a-zA-Z0-9]{10}$/g.test(randomStringBuilder(10, false, false, true))).toBeTruthy());
|
||||
test('everything', () => {
|
||||
const randomString = randomStringBuilder(30, true, true, true);
|
||||
expect(/[a-zA-Z]/g.test(randomString)).toBeTruthy();
|
||||
expect(/[0-9]/g.test(randomString)).toBeTruthy();
|
||||
expect(/[^a-zA-Z0-9]/g.test(randomString)).toBeTruthy();
|
||||
});
|
||||
test('no pool', () =>
|
||||
expect(() => randomStringBuilder(10, false, false, false)).toThrow());
|
||||
});
|
||||
|
||||
describe('matchAll', () => {
|
||||
test('using string', () => expect(matchAll('fooBar fooBar FooBar', 'fooBar').length).toBe(2));
|
||||
test('using regex', () => expect(matchAll('fooBar fooBar FooBar', /fooBar/g).length).toBe(2));
|
||||
test('using malformed regex', () => expect(() => matchAll('fooBar fooBar FooBar', /fooBar/)).toThrow());
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user