+import { hoursToMilliseconds, hoursToSeconds, isValid } from 'date-fns';
import { expect } from 'expect';
import { Constants } from '../../src/utils/Constants';
convertToDate,
convertToFloat,
convertToInt,
+ formatDurationMilliSeconds,
+ formatDurationSeconds,
generateUUID,
getRandomFloat,
getRandomInteger,
hasOwnProp,
+ isArraySorted,
isEmptyArray,
isEmptyObject,
isEmptyString,
isNullOrUndefined,
isObject,
isUndefined,
+ isValidTime,
roundTo,
secureRandom,
sleep,
expect(end - start).toBeGreaterThanOrEqual(1000);
});
+ it('Verify formatDurationMilliSeconds()', () => {
+ expect(formatDurationMilliSeconds(0)).toBe('');
+ expect(formatDurationMilliSeconds(1000)).toBe('1 second');
+ expect(formatDurationMilliSeconds(hoursToMilliseconds(4380))).toBe('182 days 12 hours');
+ });
+
+ it('Verify formatDurationSeconds()', () => {
+ expect(formatDurationSeconds(0)).toBe('');
+ expect(formatDurationSeconds(1)).toBe('1 second');
+ expect(formatDurationSeconds(hoursToSeconds(4380))).toBe('182 days 12 hours');
+ });
+
+ it('Verify isValidTime()', () => {
+ expect(isValidTime(undefined)).toBe(false);
+ expect(isValidTime(null)).toBe(false);
+ expect(isValidTime('')).toBe(false);
+ expect(isValidTime({})).toBe(false);
+ expect(isValidTime([])).toBe(false);
+ expect(isValidTime(new Map())).toBe(false);
+ expect(isValidTime(new Set())).toBe(false);
+ expect(isValidTime(new WeakMap())).toBe(false);
+ expect(isValidTime(new WeakSet())).toBe(false);
+ expect(isValidTime(-1)).toBe(true);
+ expect(isValidTime(0)).toBe(true);
+ expect(isValidTime(1)).toBe(true);
+ expect(isValidTime(-0.5)).toBe(true);
+ expect(isValidTime(0.5)).toBe(true);
+ expect(isValidTime(new Date())).toBe(true);
+ });
+
it('Verify convertToDate()', () => {
expect(convertToDate(undefined)).toBe(undefined);
expect(convertToDate(null)).toBe(null);
- const invalidDate = convertToDate('');
- expect(invalidDate instanceof Date && !isNaN(invalidDate.getTime())).toBe(false);
+ expect(isValid(convertToDate(''))).toBe(false);
expect(convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z'));
const dateStr = '2020-01-01T00:00:00.000Z';
let date = convertToDate(dateStr);
it('Verify isEmptyObject()', () => {
expect(isEmptyObject({})).toBe(true);
expect(isEmptyObject({ 1: 1, 2: 2 })).toBe(false);
- expect(isEmptyObject(undefined)).toBe(false);
- expect(isEmptyObject(null)).toBe(false);
expect(isEmptyObject(new Map())).toBe(false);
expect(isEmptyObject(new Set())).toBe(false);
expect(isEmptyObject(new WeakMap())).toBe(false);
expect(isEmptyObject(new WeakSet())).toBe(false);
});
+
+ it('Verify isArraySorted()', () => {
+ expect(
+ isArraySorted([], (a, b) => {
+ return a - b;
+ }),
+ ).toBe(true);
+ expect(
+ isArraySorted([1], (a, b) => {
+ return a - b;
+ }),
+ ).toBe(true);
+ expect(isArraySorted<number>([1, 2, 3, 4, 5], (a, b) => a - b)).toBe(true);
+ expect(isArraySorted<number>([1, 2, 3, 5, 4], (a, b) => a - b)).toBe(false);
+ expect(isArraySorted<number>([2, 1, 3, 4, 5], (a, b) => a - b)).toBe(false);
+ });
});