build: add jest, tests
This commit is contained in:
6
tests/capitalizeFirstChar.test.ts
Normal file
6
tests/capitalizeFirstChar.test.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { capitalizeFirstChar } from "../src";
|
||||
import { expect, test } from "@jest/globals";
|
||||
|
||||
test("capitalizes first character of the string", () => {
|
||||
expect(capitalizeFirstChar("lorem ipsum")).toBe("Lorem ipsum");
|
||||
});
|
||||
10
tests/classNames.test.ts
Normal file
10
tests/classNames.test.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { expect, test } from "@jest/globals";
|
||||
import { classNames } from "../src";
|
||||
|
||||
test("joins html class strings", () => {
|
||||
let isVisible = false;
|
||||
|
||||
expect(classNames("bg-blue-300", isVisible ? "block" : "hidden")).toBe(
|
||||
"bg-blue-300 hidden",
|
||||
);
|
||||
});
|
||||
10
tests/conditionalJoin.test.ts
Normal file
10
tests/conditionalJoin.test.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { expect, test } from "@jest/globals";
|
||||
import { conditionalJoin } from "../src";
|
||||
|
||||
test("conditionally joins strings", () => {
|
||||
let isAdmin = true;
|
||||
|
||||
expect(
|
||||
conditionalJoin(["Hello,", isAdmin ? "Administrator!" : "User!"], " "),
|
||||
).toBe("Hello, Administrator!");
|
||||
});
|
||||
15
tests/isDeepWeakEqual.test.ts
Normal file
15
tests/isDeepWeakEqual.test.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { isDeepWeakEqual } from "../src";
|
||||
import { expect, test } from "@jest/globals";
|
||||
|
||||
test("weakly compares objects", () => {
|
||||
expect(isDeepWeakEqual({ a: 1 }, ["lorem"])).toBe(false);
|
||||
|
||||
expect(isDeepWeakEqual({ a: 1 }, { a: 1 })).toBe(true);
|
||||
|
||||
expect(
|
||||
isDeepWeakEqual(
|
||||
{ date: "2024-03-12T18:19:50.548Z" },
|
||||
{ date: new Date(1710267590548) },
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
7
tests/randomInRange.test.ts
Normal file
7
tests/randomInRange.test.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { expect, test } from "@jest/globals";
|
||||
import { randomInRange } from "../src";
|
||||
|
||||
test("generates a random number in range", () => {
|
||||
const randomNumber = randomInRange(1, 100);
|
||||
expect(randomNumber >= 1 && randomNumber <= 100).toBeTruthy();
|
||||
});
|
||||
29
tests/removeFromArrayByKeyValue.test.ts
Normal file
29
tests/removeFromArrayByKeyValue.test.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { expect, test } from "@jest/globals";
|
||||
import { removeFromArrayByKeyValue } from "../src";
|
||||
|
||||
test("removes an object from an array by key-value object", () => {
|
||||
expect(
|
||||
removeFromArrayByKeyValue(
|
||||
[
|
||||
{ a: "1", b: "0" },
|
||||
{ a: "2", b: "0" },
|
||||
{ a: "3", b: "0" },
|
||||
],
|
||||
{ a: "3" },
|
||||
),
|
||||
).toMatchObject([
|
||||
{ a: "1", b: "0" },
|
||||
{ a: "2", b: "0" },
|
||||
]);
|
||||
|
||||
expect(
|
||||
removeFromArrayByKeyValue(
|
||||
[
|
||||
{ a: "1", b: "0" },
|
||||
{ a: "2", b: "0" },
|
||||
{ a: "3", b: "0" },
|
||||
],
|
||||
{ b: "0" },
|
||||
),
|
||||
).toMatchObject([]);
|
||||
});
|
||||
9
tests/removeObjectProperty.test.ts
Normal file
9
tests/removeObjectProperty.test.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { expect, test } from "@jest/globals";
|
||||
import { removeObjectProperty } from "../src";
|
||||
|
||||
test("remove object property by provided key", () => {
|
||||
expect(removeObjectProperty({ a: "1", b: "2", c: "3" }, "c")).toMatchObject({
|
||||
a: "1",
|
||||
b: "2",
|
||||
});
|
||||
});
|
||||
36
tests/replaceFromArrayByKeyValue.test.ts
Normal file
36
tests/replaceFromArrayByKeyValue.test.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { expect, test } from "@jest/globals";
|
||||
import { replaceFromArrayByKeyValue } from "../src";
|
||||
|
||||
test("replace from an array by key-value", () => {
|
||||
expect(
|
||||
replaceFromArrayByKeyValue(
|
||||
[
|
||||
{ a: "1", b: "0" },
|
||||
{ a: "2", b: "0" },
|
||||
{ a: "3", b: "0" },
|
||||
],
|
||||
{ a: "3" },
|
||||
{ b: "lorem" },
|
||||
),
|
||||
).toMatchObject([
|
||||
{ a: "1", b: "0" },
|
||||
{ a: "2", b: "0" },
|
||||
{ a: "3", b: "lorem" },
|
||||
]);
|
||||
|
||||
expect(
|
||||
replaceFromArrayByKeyValue(
|
||||
[
|
||||
{ a: "1", b: "0" },
|
||||
{ a: "2", b: "0" },
|
||||
{ a: "3", b: "0" },
|
||||
],
|
||||
{ b: "0" },
|
||||
{ a: "lorem" },
|
||||
),
|
||||
).toMatchObject([
|
||||
{ a: "lorem", b: "0" },
|
||||
{ a: "2", b: "0" },
|
||||
{ a: "3", b: "0" },
|
||||
]);
|
||||
});
|
||||
14
tests/splitIntoChunks.test.ts
Normal file
14
tests/splitIntoChunks.test.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { expect, test } from "@jest/globals";
|
||||
import { splitIntoChunks } from "../src";
|
||||
|
||||
test("splits an array into chunks", () => {
|
||||
// JSON.stringify is being used because there is no way to compare nested arrays in jest
|
||||
expect(
|
||||
JSON.stringify(splitIntoChunks(["a", "b", "c", "d", "e", "f"], 3)),
|
||||
).toBe(
|
||||
JSON.stringify([
|
||||
["a", "b", "c"],
|
||||
["d", "e", "f"],
|
||||
]),
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user