add Test
This commit is contained in:
85
src/utils/Text.test.ts
Normal file
85
src/utils/Text.test.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import Text from "./Text";
|
||||
|
||||
describe("Text", () => {
|
||||
describe("keyValue", () => {
|
||||
it("should join key value pairs", () => {
|
||||
expect(Text.keyValue({ a: 1, b: "x" }, "=", "&")).toBe("a=1&b=x");
|
||||
});
|
||||
|
||||
it("should return empty string for empty object", () => {
|
||||
expect(Text.keyValue({}, "=", "&")).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("urlArgs", () => {
|
||||
it("should return empty string when input is undefined", () => {
|
||||
expect(Text.urlArgs()).toBe("");
|
||||
});
|
||||
|
||||
it("should encode primitive and array values", () => {
|
||||
const query = Text.urlArgs({
|
||||
name: "A B",
|
||||
tags: ["x/y", "z"],
|
||||
age: 18
|
||||
});
|
||||
expect(query).toBe("name=A%20B&tags=x%2Fy&tags=z&age=18");
|
||||
});
|
||||
|
||||
it("should ignore inherited properties", () => {
|
||||
const parent = { hidden: "parent" };
|
||||
const obj = Object.create(parent) as { own: string };
|
||||
obj.own = "child";
|
||||
expect(Text.urlArgs(obj)).toBe("own=child");
|
||||
});
|
||||
});
|
||||
|
||||
describe("template", () => {
|
||||
it("should replace template placeholders", () => {
|
||||
expect(Text.template("Hi ${name}", { name: "Timi" })).toBe("Hi Timi");
|
||||
});
|
||||
|
||||
it("should output undefined text when key is missing", () => {
|
||||
expect(Text.template("Hi ${name} ${miss}", { name: "Timi" })).toBe("Hi Timi undefined");
|
||||
});
|
||||
});
|
||||
|
||||
describe("unit", () => {
|
||||
it("should format number with fixed and unit", () => {
|
||||
expect(Text.unitSpace(1.236, 2, "rem")).toBe("1.24 rem");
|
||||
});
|
||||
it("should format number with fixed and unit not space", () => {
|
||||
expect(Text.unit(1.236, "rem", 2)).toBe("1.24rem");
|
||||
});
|
||||
|
||||
it("should trim string and append unit", () => {
|
||||
expect(Text.unit(" 12 ", "px")).toBe("12px");
|
||||
});
|
||||
|
||||
it("should return empty string for nullish zero and NaN", () => {
|
||||
expect(Text.unit(null, "px")).toBe("");
|
||||
expect(Text.unit(undefined, "px")).toBe("");
|
||||
expect(Text.unit(0, "px")).toBe("");
|
||||
expect(Text.unit(Number.NaN, "px")).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("display", () => {
|
||||
it("should trim string values", () => {
|
||||
expect(Text.display(" abc ")).toBe("abc");
|
||||
expect(Text.display(" ")).toBe("");
|
||||
});
|
||||
|
||||
it("should stringify finite numbers", () => {
|
||||
expect(Text.display(0)).toBe("0");
|
||||
expect(Text.display(12.3)).toBe("12.3");
|
||||
});
|
||||
|
||||
it("should return empty string for invalid numbers and nullish", () => {
|
||||
expect(Text.display(Number.NaN)).toBe("");
|
||||
expect(Text.display(Number.POSITIVE_INFINITY)).toBe("");
|
||||
expect(Text.display(null)).toBe("");
|
||||
expect(Text.display(undefined)).toBe("");
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user