Add basic tests for all non DOM/Window calls
Note that these are very basic tests and just a start to learn vitest. There are no tests for DOM/Window because I do not know how to do those tests the best way with some headless brower testing suit or which to use
This commit is contained in:
@@ -148,7 +148,8 @@ Currently covered:
|
|||||||
- StringHelpers
|
- StringHelpers
|
||||||
- UniqIdGenerators (Part)
|
- UniqIdGenerators (Part)
|
||||||
- UrlParser
|
- UrlParser
|
||||||
|
- JavaScriptHelpers
|
||||||
|
|
||||||
TODO:
|
TODO:
|
||||||
|
|
||||||
- JavaScriptHelpers
|
Find a mocker/or some headless browser checker so we can test DOM/window based calls
|
||||||
|
|||||||
@@ -5,8 +5,11 @@ Creator: Clemens Schwaighofer
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export {
|
export {
|
||||||
errorCatch, isFunction, executeFunctionByName, isObject, getObjectCount,
|
errorCatch, isFunction, executeFunctionByName,
|
||||||
keyInObject, getKeyByValue,valueInObject, deepCopyFunction
|
isObject, getObjectCount,
|
||||||
|
keyInObject, objectKeyExists,
|
||||||
|
getKeyByValue, valueInObject, objectValueExists,
|
||||||
|
deepCopyFunction
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -90,10 +93,13 @@ function isObject(val)
|
|||||||
/**
|
/**
|
||||||
* get the length of an object (entries)
|
* get the length of an object (entries)
|
||||||
* @param {Object} object object to check
|
* @param {Object} object object to check
|
||||||
* @return {Number} number of entry
|
* @return {Number} number of entry, or -1 if not object
|
||||||
*/
|
*/
|
||||||
function getObjectCount(object)
|
function getObjectCount(object)
|
||||||
{
|
{
|
||||||
|
if (!isObject(object)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
return Object.keys(object).length;
|
return Object.keys(object).length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,8 +108,20 @@ function getObjectCount(object)
|
|||||||
* @param {String} key key name
|
* @param {String} key key name
|
||||||
* @param {Object} object object to search key in
|
* @param {Object} object object to search key in
|
||||||
* @return {Boolean} true/false if key exists in object
|
* @return {Boolean} true/false if key exists in object
|
||||||
|
* @deprecated Use objectKeyExists
|
||||||
*/
|
*/
|
||||||
function keyInObject(key, object)
|
function keyInObject(key, object)
|
||||||
|
{
|
||||||
|
return objectKeyExists(object, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the correct order and will superseed keyInObject
|
||||||
|
* @param {Object} object object to search key in
|
||||||
|
* @param {String} key key name
|
||||||
|
* @returns {Boolean} true/false if key exists in object
|
||||||
|
*/
|
||||||
|
function objectKeyExists(object, key)
|
||||||
{
|
{
|
||||||
return Object.prototype.hasOwnProperty.call(object, key) ? true : false;
|
return Object.prototype.hasOwnProperty.call(object, key) ? true : false;
|
||||||
}
|
}
|
||||||
@@ -124,8 +142,20 @@ function getKeyByValue(object, value)
|
|||||||
* @param {Object} object object to search value in
|
* @param {Object} object object to search value in
|
||||||
* @param {any} value any value (String, Number, etc)
|
* @param {any} value any value (String, Number, etc)
|
||||||
* @return {Boolean} true on value found, false on not found
|
* @return {Boolean} true on value found, false on not found
|
||||||
|
* @deprecated use objectValueExists
|
||||||
*/
|
*/
|
||||||
function valueInObject(object, value)
|
function valueInObject(object, value)
|
||||||
|
{
|
||||||
|
return objectValueExists(object, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns true if value is found in object with a key
|
||||||
|
* @param {Object} object object to search value in
|
||||||
|
* @param {any} value any value (String, Number, etc)
|
||||||
|
* @return {Boolean} true on value found, false on not found
|
||||||
|
*/
|
||||||
|
function objectValueExists(object, value)
|
||||||
{
|
{
|
||||||
return Object.keys(object).find(key => object[key] === value) ? true : false;
|
return Object.keys(object).find(key => object[key] === value) ? true : false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,9 +25,10 @@ function generateId(len)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* creates a pseudo random string of 11 characters
|
* creates a pseudo random string of 10 or 11 characters
|
||||||
* works on all browsers
|
* works on all browsers
|
||||||
* after many runs it will create duplicates
|
* after many runs it will create duplicates
|
||||||
|
* NOTE: no idea why this sometimes returns 10 or 11
|
||||||
* @return {String} not true random string
|
* @return {String} not true random string
|
||||||
*/
|
*/
|
||||||
function randomIdF()
|
function randomIdF()
|
||||||
|
|||||||
123
tests/JavaScriptHelpers.test.js
Normal file
123
tests/JavaScriptHelpers.test.js
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
|
||||||
|
import {
|
||||||
|
// errorCatch,
|
||||||
|
// isFunction,
|
||||||
|
// executeFunctionByName,
|
||||||
|
isObject,
|
||||||
|
getObjectCount,
|
||||||
|
keyInObject,
|
||||||
|
objectKeyExists,
|
||||||
|
getKeyByValue,
|
||||||
|
valueInObject,
|
||||||
|
objectValueExists,
|
||||||
|
deepCopyFunction
|
||||||
|
} from '../src/utils/JavaScriptHelpers.mjs';
|
||||||
|
|
||||||
|
|
||||||
|
// needs window class
|
||||||
|
// function test() {}
|
||||||
|
/* describe("isFunction", () => {
|
||||||
|
it('Should return brack if function', () => {
|
||||||
|
expect(isFunction("test")).toEqual(true);
|
||||||
|
expect(isFunction("testFalse")).toEqual(false);
|
||||||
|
});
|
||||||
|
}); */
|
||||||
|
|
||||||
|
describe("isObject", () => {
|
||||||
|
it('should return bool if object', () => {
|
||||||
|
let empty_object = {};
|
||||||
|
let is_object = {
|
||||||
|
"a": 1
|
||||||
|
};
|
||||||
|
let is_string = "";
|
||||||
|
let is_number = 1;
|
||||||
|
expect(isObject(empty_object)).toEqual(true);
|
||||||
|
expect(isObject(is_object)).toEqual(true);
|
||||||
|
expect(isObject(is_string)).toEqual(false);
|
||||||
|
expect(isObject(is_number)).toEqual(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("getObjectCount", () => {
|
||||||
|
it('should return count of objects', () => {
|
||||||
|
let zero = {};
|
||||||
|
let one = {
|
||||||
|
"a": 1
|
||||||
|
};
|
||||||
|
let notset = "";
|
||||||
|
expect(getObjectCount(zero)).toEqual(0);
|
||||||
|
expect(getObjectCount(one)).toEqual(1);
|
||||||
|
expect(getObjectCount(notset)).toEqual(-1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("objectKeyExists", () => {
|
||||||
|
it('should return true if key exists in object', () => {
|
||||||
|
let zero = {};
|
||||||
|
let one = {
|
||||||
|
"a": 1,
|
||||||
|
"b": "foo"
|
||||||
|
};
|
||||||
|
let notset = "";
|
||||||
|
expect(keyInObject("", zero)).toEqual(false);
|
||||||
|
expect(objectKeyExists(zero, "")).toEqual(false);
|
||||||
|
expect(keyInObject("a", one)).toEqual(true);
|
||||||
|
expect(objectKeyExists(one, "a")).toEqual(true);
|
||||||
|
expect(keyInObject("c", one)).toEqual(false);
|
||||||
|
expect(objectKeyExists(one, "c")).toEqual(false);
|
||||||
|
expect(keyInObject("", notset)).toEqual(false);
|
||||||
|
expect(objectKeyExists(notset, "")).toEqual(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("getKeyByValue", () => {
|
||||||
|
it('should return the key for matching volume, first serve', () => {
|
||||||
|
let zero = {};
|
||||||
|
let one = {
|
||||||
|
"a": 1,
|
||||||
|
"b": "foo"
|
||||||
|
};
|
||||||
|
let notset = "";
|
||||||
|
expect(getKeyByValue(zero, "")).toEqual("");
|
||||||
|
expect(getKeyByValue(one, 1)).toEqual("a");
|
||||||
|
expect(getKeyByValue(one, "foo")).toEqual("b");
|
||||||
|
expect(getKeyByValue(one, "bar")).toEqual("");
|
||||||
|
expect(getKeyByValue(notset, "")).toEqual("");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("objectValueExists", () => {
|
||||||
|
it('should return true if key exists in object', () => {
|
||||||
|
let zero = {};
|
||||||
|
let one = {
|
||||||
|
"a": 1,
|
||||||
|
"b": "foo"
|
||||||
|
};
|
||||||
|
let notset = "";
|
||||||
|
expect(valueInObject(zero, "")).toEqual(false);
|
||||||
|
expect(objectValueExists(zero, "")).toEqual(false);
|
||||||
|
expect(valueInObject(one, 1)).toEqual(true);
|
||||||
|
expect(objectValueExists(one, 1)).toEqual(true);
|
||||||
|
expect(valueInObject(one, "foo")).toEqual(true);
|
||||||
|
expect(objectValueExists(one, "foo")).toEqual(true);
|
||||||
|
expect(valueInObject(one, "bar")).toEqual(false);
|
||||||
|
expect(objectValueExists(one, "bar")).toEqual(false);
|
||||||
|
expect(valueInObject(notset, "")).toEqual(false);
|
||||||
|
expect(objectValueExists(notset, "")).toEqual(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// deepCopyFunction
|
||||||
|
describe("deepCopyFunction", () => {
|
||||||
|
it('should deep copy an object', () => {
|
||||||
|
let one = {
|
||||||
|
"a": 1,
|
||||||
|
"b": "foo"
|
||||||
|
};
|
||||||
|
let new_object = deepCopyFunction(one);
|
||||||
|
expect(new_object).toStrictEqual(one);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// __END__
|
||||||
@@ -19,10 +19,11 @@ import {
|
|||||||
}); */
|
}); */
|
||||||
|
|
||||||
describe("randomIdF", () => {
|
describe("randomIdF", () => {
|
||||||
it('Should create a 10 character long random id', () => {
|
it('Should create a 10 or 11 character long random id', () => {
|
||||||
let rand_id = randomIdF();
|
let rand_id = randomIdF();
|
||||||
expect(rand_id).lengthOf(11);
|
expect(rand_id.length).toBeGreaterThanOrEqual(10);
|
||||||
expect(rand_id).match(/^[a-z0-9]{11}$/);
|
expect(rand_id.length).toBeLessThanOrEqual(11);
|
||||||
|
expect(rand_id).match(/^[a-z0-9]{10,11}$/);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user