build: add jest, tests

This commit is contained in:
2024-03-12 21:23:19 +02:00
parent fdb28fa612
commit ea3cb13c85
19 changed files with 2173 additions and 13 deletions

View 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
View 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",
);
});

View 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!");
});

View 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);
});

View 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();
});

View 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([]);
});

View 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",
});
});

View 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" },
]);
});

View 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"],
]),
);
});