Skip to content

Commit 1473ad2

Browse files
worked on logging function
1 parent df8326e commit 1473ad2

29 files changed

+1007
-132
lines changed

.yarn/versions/3788a52d.yml

Whitespace-only changes.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@devlander/utils",
33
"description": "Utils shared between projects that are plain javascript",
4-
"version": "0.0.25",
4+
"version": "0.0.26",
55
"browser": "dist/umd/index.js",
66
"main": "dist/cjs/index.js",
77
"types": "typings/index.d.ts",
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { getRandomValFromArray } from "../getRandomValFromArray";
2+
3+
describe("getRandomValFromArray", () => {
4+
it("should return the fallback value if the array is empty", () => {
5+
const arr: number[] = [];
6+
const fallbackValue = 0;
7+
8+
const result = getRandomValFromArray(arr, fallbackValue);
9+
10+
expect(result).toBe(fallbackValue);
11+
});
12+
13+
it("should return a random value from the array", () => {
14+
const arr = [1, 2, 3, 4, 5];
15+
const fallbackValue = 0;
16+
17+
const result = getRandomValFromArray(arr, fallbackValue);
18+
19+
expect(arr).toContain(result);
20+
});
21+
22+
it("should return the fallback value if the array contains only undefined values", () => {
23+
const arr: (number | undefined)[] = [undefined, undefined, undefined];
24+
const fallbackValue = 0;
25+
26+
const result = getRandomValFromArray(arr, fallbackValue);
27+
28+
expect(result).toBe(fallbackValue);
29+
});
30+
});

src/__tests__/hslToRgb.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { hslToRgb } from "../hslToRgb";
2+
3+
describe("hslToRgb", () => {
4+
it("should convert HSL values to RGB correctly", () => {
5+
// Test case 1
6+
let h = 0;
7+
let s = 100;
8+
let l = 50;
9+
let expectedRgb = [255, 0, 0];
10+
let result = hslToRgb(h, s, l);
11+
expect(result).toEqual(expectedRgb);
12+
13+
// Test case 2
14+
h = 120;
15+
s = 100;
16+
l = 50;
17+
expectedRgb = [0, 255, 0];
18+
result = hslToRgb(h, s, l);
19+
expect(result).toEqual(expectedRgb);
20+
21+
// Test case 3
22+
h = 240;
23+
s = 100;
24+
l = 50;
25+
expectedRgb = [0, 0, 255];
26+
result = hslToRgb(h, s, l);
27+
expect(result).toEqual(expectedRgb);
28+
29+
// Add more test cases as needed
30+
});
31+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { isPlainObject } from "../isPlainObject";
2+
3+
describe("isPlainObject", () => {
4+
it("should return true for a plain object", () => {
5+
const obj = { name: "John", age: 30 };
6+
7+
const result = isPlainObject(obj);
8+
9+
expect(result).toBe(true);
10+
});
11+
12+
it("should return false for an array", () => {
13+
const arr = [1, 2, 3];
14+
15+
const result = isPlainObject(arr);
16+
17+
expect(result).toBe(false);
18+
});
19+
20+
it("should return false for a string", () => {
21+
const str = "Hello, world!";
22+
23+
const result = isPlainObject(str);
24+
25+
expect(result).toBe(false);
26+
});
27+
28+
it("should return false for a number", () => {
29+
const num = 42;
30+
31+
const result = isPlainObject(num);
32+
33+
expect(result).toBe(false);
34+
});
35+
36+
it("should return false for null", () => {
37+
const value = null;
38+
39+
const result = isPlainObject(value);
40+
41+
expect(result).toBe(false);
42+
});
43+
44+
it("should return false for undefined", () => {
45+
const value = undefined;
46+
47+
const result = isPlainObject(value);
48+
49+
expect(result).toBe(false);
50+
});
51+
});

src/__tests__/isValidHex.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { isValidHex } from "../isValidHex";
2+
3+
describe("isValidHex", () => {
4+
it("should return true for valid hex colors", () => {
5+
expect(isValidHex("#000")).toBe(true);
6+
expect(isValidHex("#ffffff")).toBe(true);
7+
expect(isValidHex("#12345678")).toBe(true);
8+
});
9+
10+
it("should return false for invalid hex colors", () => {
11+
expect(isValidHex("000")).toBe(false);
12+
expect(isValidHex("#12345")).toBe(false);
13+
expect(isValidHex("#gggggg")).toBe(false);
14+
expect(isValidHex("#123456789")).toBe(false);
15+
});
16+
17+
it("should return false for non-string input", () => {
18+
expect(isValidHex(null)).toBe(false);
19+
expect(isValidHex(undefined)).toBe(false);
20+
expect(isValidHex(123)).toBe(false);
21+
expect(isValidHex({})).toBe(false);
22+
expect(isValidHex([])).toBe(false);
23+
});
24+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { applyFormatters } from "../../logFormattedPhrases/applyFormatters";
2+
import { PhraseItem, PhraseOrderType } from "../../logFormattedPhrases/formattedPhrase.types";
3+
import { defaultPhraseOrder } from "../../logFormattedPhrases/getPhraseStylesFromOrder";
4+
5+
6+
describe("applyFormatters", () => {
7+
it("Expect apply formatters to return correct values", () => {
8+
const phraseItem: PhraseItem = {
9+
background: "red", color: "blue",
10+
phrase: ""
11+
};
12+
const orderItems:PhraseOrderType[] = defaultPhraseOrder
13+
14+
const result = applyFormatters(phraseItem, orderItems);
15+
16+
// console.log(result, 'result in applyFormatters test');
17+
18+
const hasBlueForColor = result.some((formatter) => formatter.value === "blue" && formatter.name === "color");
19+
20+
const hasRedValueForBackground = result.some((formatter) => formatter.value === "red" && formatter.name === "background");
21+
expect(hasRedValueForBackground).toBe(true);
22+
expect(hasBlueForColor).toBe(true);
23+
});
24+
25+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
import { formatPhrase } from '../../logFormattedPhrases/formatPhrase';
3+
import { PhraseItem } from '../../logFormattedPhrases/formattedPhrase.types';
4+
5+
6+
describe("formatPhrase", () => {
7+
it("should be able to log with just text", () => {
8+
const item: PhraseItem = {
9+
phrase: "Hello, world!",
10+
};
11+
12+
const expectedFormattedPhrase = "Hello, world!"; // Update this with the expected formatted phrase
13+
14+
15+
16+
// Call the formatPhrase function
17+
const result = formatPhrase(item);
18+
if(result) {
19+
expect(result.message).toBe(expectedFormattedPhrase);
20+
}
21+
});
22+
23+
it("it should work with color and background", () => {
24+
const item: PhraseItem = {
25+
phrase: "Hello, world!",
26+
color: "blue",
27+
background: "red"
28+
};
29+
30+
31+
32+
33+
// Call the formatPhrase function
34+
const result = formatPhrase(item);
35+
36+
console.log(result, 'result in formatPhrase test');
37+
38+
if(result) {
39+
expect(result.formattersApplied.includes("color")).toBe(true);
40+
expect(result.formattersApplied.includes("background")).toBe(true);
41+
42+
}
43+
});
44+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Mock the module
2+
jest.mock("../../logFormattedPhrases/getPhraseStylesFromOrder", () => ({
3+
getPhraseStylesFromOrder: jest.fn().mockReturnValue([]),
4+
}));
5+
6+
import * as pico from "picocolors";
7+
import { PhraseColors } from "../../logFormattedPhrases/formattedPhrase.types";
8+
import { getPhraseBackground } from "../../logFormattedPhrases/getPhraseBackground";
9+
10+
describe("getPhraseBackground", () => {
11+
it("should return the correct background function for a given color", () => {
12+
const color = "red";
13+
const expectedFunction = pico.bgRed;
14+
15+
const result = getPhraseBackground(color);
16+
17+
expect(result).toBe(expectedFunction);
18+
});
19+
20+
it("should return the default background function when color is not found", () => {
21+
const color = "purple";
22+
const expectedFunction = pico.bgBlack;
23+
24+
const result = getPhraseBackground(color as PhraseColors);
25+
26+
expect(result).toBe(expectedFunction);
27+
});
28+
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Source: do not remove the comment above. It's used to verify the path of the snippet when it's being
2+
// referenced in conversations about the code.
3+
// Path: ./src/__tests__/logFormattedPhrases/getPhraseStylesFromOrder.test.ts
4+
5+
import { AppliedFormatter, PhraseItem } from "../../logFormattedPhrases/formattedPhrase.types";
6+
import { getPhraseStylesFromOrder } from "../../logFormattedPhrases/getPhraseStylesFromOrder";
7+
8+
const hasValueInFormatters = (formatters: AppliedFormatter[], property: string) => {
9+
return formatters.some((formatter) => formatter.name === property);
10+
}
11+
12+
describe("getPhraseStylesFromOrder", () => {
13+
14+
15+
// it("should return an empty array when item.orderToApplyStyles is empty", () => {
16+
// const item: PhraseItem = {
17+
// orderToApplyStyles: [],
18+
// phrase: ""
19+
// };
20+
// const result = getPhraseStylesFromOrder(item);
21+
// expect(result).toEqual([]);
22+
// });
23+
24+
// it("should return an empty array when item.orderToApplyStyles is not an array", () => {
25+
26+
// const item: PhraseItem = {
27+
// orderToApplyStyles: "invalid" as any,
28+
// phrase: ""
29+
// };
30+
// const result = getPhraseStylesFromOrder(item);
31+
// expect(result).toEqual([]);
32+
// });
33+
34+
it("Should change just color and fontWeight to false", () => {
35+
// look at this one RIGHT HERE
36+
37+
const item: PhraseItem = {
38+
phrase: "",
39+
color: "red",
40+
orderToApplyStyles: [
41+
{ property: "color", enabled: false, order: 1 } as any,
42+
{ property: "fontWeight", enabled: false, order: 2 } as any,
43+
],
44+
};
45+
const result: AppliedFormatter[] = getPhraseStylesFromOrder(item);
46+
expect(result).toBeDefined();
47+
48+
});
49+
50+
// Add more test cases for different scenarios
51+
52+
});

0 commit comments

Comments
 (0)