Skip to content

Commit

Permalink
add unit tests to increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
patinthehat committed Mar 29, 2024
1 parent cad91bd commit fcba31d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 52 deletions.
12 changes: 0 additions & 12 deletions src/Payloads/TracePayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ export class TracePayload extends Payload {
this.frames = RemovesRayFrames.removeRayFrames(frames);
}

public startFromIndex(index: number): this {
this.startFromIndexNum = index;

return this;
}

public limit(limit: number): this {
this.limitNum = limit;

return this;
}

public getType(): string {
return 'trace';
}
Expand Down
40 changes: 0 additions & 40 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,6 @@

import { Ray } from '@/Ray';

/**
* Check type of operand with more specificity than `typeof`.
* Slightly modified version of MDN helper found in `typeof` definition page.
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof#real-world_usage
*
* Modified version of original from:
* @see https://github.com/m1guelpf/ray-js/blob/main/src/utils/type.js
*
* @param {*} item
* @returns {string}
*/
export const getType = (item: any) => {
if (typeof item === 'object') {
//return item.constructor.name.toString();
const deepType = Object.prototype.toString.call(item).slice(8, -1).toLowerCase();

if (deepType === 'generatorfunction') {
return 'function';
}

// Prevent over-specificity (for example, [object HTMLDivElement], etc).
// Account for functionish Regexp (Android <=2.3), functionish <object> element (Chrome <=57, Firefox <=52), etc.
// String.prototype.match is universally supported.
if (deepType.match(/^(array|bigint|date|error|function|generator|regexp|symbol)$/)) {
return deepType;
}
}

return typeof item;
};

export const isHtmlable = (item: any): boolean => {
if (typeof item !== 'object') {
return false;
}

return item instanceof HTMLElement;
};

export enum SendRequestCallbackType {
Sending = 'sending',
Sent = 'sent',
Expand Down
32 changes: 32 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,35 @@ export function formatDateExtended(date: Date, format = 'YYYY-MM-DD HH:mm:ss'):

return formattedDate;
}

/**
* Check type of operand with more specificity than `typeof`.
* Slightly modified version of MDN helper found in `typeof` definition page.
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof#real-world_usage
*
* Modified version of original from:
* @see https://github.com/m1guelpf/ray-js/blob/main/src/utils/type.js
*
* @param {*} item
* @returns {string}
*/
export const getType = (item: any) => {
if (typeof item === 'object') {
//return item.constructor.name.toString();
const deepType = Object.prototype.toString.call(item).slice(8, -1).toLowerCase();

if (deepType === 'generatorfunction') {
return 'function';
}

// Prevent over-specificity (for example, [object HTMLDivElement], etc).
// Account for functionish Regexp (Android <=2.3), functionish <object> element (Chrome <=57, Firefox <=52), etc.
// String.prototype.match is universally supported.
if (deepType.match(/^(array|bigint|date|error|function|generator|regexp|symbol)$/)) {
return deepType;
}
}

return typeof item;
};
7 changes: 7 additions & 0 deletions tests/lib/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
nonCryptoUuidV4,
md5,
formatDateExtended,
getType,
} from '../../src/lib/utils';

it.concurrent('sleeps for 0.1 sec', async () => {
Expand Down Expand Up @@ -88,3 +89,9 @@ it('formats a date', () => {
expect(formatDateExtended(date, 'hh:mm:ss')).toBe('12:34:56');
expect(formatDateExtended(date, 'T').length).greaterThan(1);
});

it('gets the type of a variable', () => {
expect(getType('test')).toBe('string');
expect(getType(123)).toBe('number');
expect(getType(true)).toBe('boolean');
});

0 comments on commit fcba31d

Please sign in to comment.