Files
web-alterflixhub/app/components/admin/admin-helpers.spec.ts
T
2026-11-01 17:24:44 +07:00

84 lines
3.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { parseCommaList, formatList } from "@/components/admin/CommaListInput";
import { normalizeSlug, slugProblem, suggestSlug } from "@/components/admin/SlugInput";
/**
* สเปกของตัวช่วยในหลังบ้าน — กันบั๊กที่เคยเจอจริง
* 1) ช่องคั่นด้วย "," ที่กลืนตัวคั่นระหว่างพิมพ์
* 2) slug ที่พิมพ์ไทย/เว้นวรรคแล้วหาย
*/
describe("parseCommaList", () => {
it("แยกด้วย , และตัดช่องว่างหัวท้าย", () => {
expect(parseCommaList("อาร์ตเฮาส์, สารคดี")).toEqual(["อาร์ตเฮาส์", "สารคดี"]);
});
it("ทิ้งค่าว่าง และรับตัวคั่นเต็มความกว้าง/ขึ้นบรรทัดใหม่", () => {
expect(parseCommaList("เงินสด, , โอน")).toEqual(["เงินสด", "โอน"]);
expect(parseCommaList("เงินสด,โอน")).toEqual(["เงินสด", "โอน"]);
expect(parseCommaList("ก\nข")).toEqual(["ก", "ข"]);
});
it("ค่าว่างได้อาร์เรย์ว่าง", () => {
expect(parseCommaList(" ")).toEqual([]);
});
});
describe("formatList", () => {
it("ต่อด้วย ', ' เสมอ", () => {
expect(formatList(["a", "b"])).toBe("a, b");
expect(formatList([])).toBe("");
});
});
describe("normalizeSlug", () => {
it("ตัวเล็ก + ช่องว่างเป็นขีด", () => {
expect(normalizeSlug("Mecani Cinema")).toBe("mecani-cinema");
});
it("ตัดอักขระที่ไม่ใช่ a-z0-9- และยุบขีดซ้ำ", () => {
expect(normalizeSlug(" HOUSE & Co. ")).toBe("house-co");
expect(normalizeSlug("a__b")).toBe("a-b");
expect(normalizeSlug("--a--b--")).toBe("a-b");
});
it("ภาษาไทยล้วนได้ค่าว่าง (ไม่ยอมให้บันทึก พร้อมเหตุผล)", () => {
expect(normalizeSlug("เมคานี ใหม่")).toBe("");
expect(slugProblem("", { others: [] })).toBe("slug ว่างไม่ได้");
});
});
describe("suggestSlug", () => {
it("แปลงชื่ออังกฤษเป็น slug", () => {
expect(suggestSlug("HOUSE CINEMA SAMYAN")).toBe("house-cinema-samyan");
expect(suggestSlug("MECANI")).toBe("mecani");
});
it("& กลายเป็น 'and' และตัด apostrophe", () => {
expect(suggestSlug("Doc Club & Friends")).toBe("doc-club-and-friends");
expect(suggestSlug("O'Brien Cinema")).toBe("obrien-cinema");
});
it("ตัดเสียง accent", () => {
expect(suggestSlug("Café Lumière")).toBe("cafe-lumiere");
});
});
describe("slugProblem", () => {
it("ซ้ำกับโรงอื่น = ใช้ไม่ได้", () => {
expect(
slugProblem("mecani", { others: ["mecani", "loprama"], self: "loprama" }),
).toBe("slug นี้มีโรงอื่นใช้อยู่");
});
it("ซ้ำกับตัวเอง = ผ่าน (ตอนแก้ฟิลด์อื่น)", () => {
expect(slugProblem("mecani", { others: ["mecani"], self: "mecani" })).toBeNull();
});
it("รูปแบบผิด เช่นขีดหัว/ท้าย = ใช้ไม่ได้", () => {
expect(slugProblem("-mecani", { others: [] })).toContain("a-z");
expect(slugProblem("mecani--x", { others: [] })).toContain("a-z");
});
});