import { describe, expect, it } from "vitest"; import { seedForCinemas, seedForFilmLovers } from "./content-seed"; import { sanitizeForCinemas, sanitizeForFilmLovers } from "./content.server"; /** * ทดสอบตัวตรวจข้อมูลของหน้าบทความทั้งสองหน้า * (สำหรับคนทำโรงหนัง / สำหรับคนรักหนัง) * ค่าที่เว้นว่าง/ผิดรูปแบบต้องถูกแทนด้วยค่าเริ่มต้น ไม่ใช่ปล่อยให้หน้าเว็บว่าง */ describe("sanitizeForCinemas", () => { const seed = seedForCinemas(); it("ไม่ส่งข้อมูลมา → คืนค่าเริ่มต้นทั้งสองภาษา", () => { expect(sanitizeForCinemas(undefined)).toEqual(seed); expect(sanitizeForCinemas("ผิดรูปแบบ")).toEqual(seed); }); it("เก็บบล็อกที่ถูกต้องตามลำดับ และตัดบล็อกที่ว่าง", () => { const result = sanitizeForCinemas({ th: { title: "สำหรับคนทำโรงหนัง", lead: "เกริ่น", blocks: [ { type: "paragraph", text: "หนึ่ง" }, { type: "paragraph", text: " " }, { type: "image", src: "/images/a.jpg", alt: "a", caption: "" }, { type: "image", src: "" }, ], contactHeading: "ติดต่อ", contactNote: "โน้ต", }, }); expect(result.th.blocks).toEqual([ { type: "paragraph", text: "หนึ่ง" }, { type: "image", src: "/images/a.jpg", alt: "a", caption: "" }, ]); expect(result.th.title).toBe("สำหรับคนทำโรงหนัง"); }); it("ฟิลด์ที่เว้นว่างถูกแทนด้วยค่าเริ่มต้น", () => { const result = sanitizeForCinemas({ th: { title: " ", lead: "", blocks: [], contactHeading: "", contactNote: "", }, }); expect(result.th.title).toBe(seed.th.title); expect(result.th.lead).toBe(seed.th.lead); expect(result.th.blocks).toEqual(seed.th.blocks); expect(result.th.contactHeading).toBe(seed.th.contactHeading); expect(result.th.contactNote).toBe(seed.th.contactNote); }); it("ภาษา en ที่ไม่ส่งมาจะได้ค่าเริ่มต้นของ en", () => { const result = sanitizeForCinemas({ th: { title: "ไทยเท่านั้น" } }); expect(result.en).toEqual(seed.en); }); }); describe("sanitizeForFilmLovers", () => { const seed = seedForFilmLovers(); it("ไม่ส่งข้อมูลมา → ค่าเริ่มต้นของหน้า 'สำหรับคนรักหนัง'", () => { expect(sanitizeForFilmLovers(undefined)).toEqual(seed); expect(seed.th.title).not.toBe(seedForCinemas().th.title); }); it("เก็บบล็อกและหัวเรื่องที่ส่งมา ตัดบล็อกว่าง", () => { const result = sanitizeForFilmLovers({ th: { title: "สำหรับคนรักหนัง", blocks: [ { type: "paragraph", text: "ย่อหน้า" }, { type: "image", src: "/images/a.jpg", alt: "alt", caption: "" }, { type: "paragraph", text: "" }, ], }, }); expect(result.th.blocks).toHaveLength(2); expect(result.th.title).toBe("สำหรับคนรักหนัง"); expect(result.th.lead).toBe(seed.th.lead); }); });