Files
timi-web/src/utils/Text.test.ts
2026-04-12 10:40:35 +08:00

113 lines
3.3 KiB
TypeScript

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("");
});
});
describe("readableColor", () => {
it("should return #FFF for dark backgrounds", () => {
expect(Text.readableColor("#000")).toBe("#FFF");
expect(Text.readableColor("#123456")).toBe("#FFF");
});
it("should return #111 for light backgrounds", () => {
expect(Text.readableColor("#FFF")).toBe("#111");
expect(Text.readableColor("#f5f5f5")).toBe("#111");
});
it("should use threshold at luminance 186", () => {
expect(Text.readableColor("#BABABA")).toBe("#111");
expect(Text.readableColor("#B9B9B9")).toBe("#FFF");
});
it("should trim whitespace and accept mixed case hex", () => {
expect(Text.readableColor(" #AbC ")).toBe("#FFF");
});
it("should return #111 for invalid colors", () => {
expect(Text.readableColor("red")).toBe("#111");
expect(Text.readableColor("#12")).toBe("#111");
expect(Text.readableColor("#GGGGGG")).toBe("#111");
});
});
});