import { describe, expect, it } from "vitest"; import { createElement } from "react"; import { renderToStaticMarkup } from "react-dom/server"; import { RichTextView } from "@/components/frontend/RichTextView"; /** * Guards the shared Lexical front-end render path: the .lexical-content typography hook, * every structural node type, and author-chosen colours (inline `style`, top-level `color`, * or TextStateFeature `$` state) must survive into rendered HTML. Payload's default JSX * converters drop the colour keys, so this component exists only for that; if it ever regresses * to plain , colours silently disappear on every field again. */ const richText = (nodes: unknown[]) => ({ root: { type: "root", children: nodes } }); const paragraph = (...children: unknown[]) => ({ type: "paragraph", children }); const heading = (tag: string, text: string) => ({ type: "heading", tag, children: [{ type: "text", text }] }); // Payload's default JSX converter derives the marker class from `listType` const bullet = (tag: "ul" | "ol", listType: string, ...items: string[]) => ({ type: "list", tag, listType, children: items.map((t) => ({ type: "listitem", value: 1, children: [{ type: "text", text: t }] })), }); const quote = (...children: unknown[]) => ({ type: "quote", children }); const describeRender = (doc: unknown) => renderToStaticMarkup(createElement(RichTextView, { data: doc as never })); describe("RichTextView shared render path", () => { it("applies .lexical-content exactly once and drops the default payload-richtext wrapper", () => { const html = describeRender(richText([paragraph({ type: "text", text: "hi" })])); expect(html).toContain('class="lexical-content"'); expect(html.match(/lexical-content/g)).toHaveLength(1); expect(html).not.toContain("payload-richtext"); }); it("keeps structural elements intact instead of flattening them to spans", () => { const html = describeRender( richText([ heading("h2", "Title"), paragraph( { type: "text", format: 1, text: "strong bit" }, { type: "text", format: 2, text: " italic" }, ), bullet("ul", "bullet", "alpha", "beta"), bullet("ol", "number", "one"), quote(paragraph({ type: "text", text: "cited" })), { type: "horizontalrule" }, ]), ); for (const fragment of [ "

Title

", "strong bit", " italic", "", "
", ]) { expect(html).toContain(fragment); } }); it("renders the colour stored as an inline style string on a text node", () => { const html = describeRender(richText([paragraph({ type: "text", style: "color:#4ade80;", text: "green" })])); expect(html).toContain('style="color:#4ade80"'); }); it("maps palette keys from `$` state and `color` to the shared briefing hexes", () => { for (const node of [ { type: "text", $: { color: "blue" }, text: "s" }, { type: "text", color: "blue", text: "c" }, ]) { const html = describeRender(richText([paragraph(node)])); expect(html).toContain('style="color:#60a5fa"'); } }); it("leaves uncoloured and unknown-colour nodes without an injected style span", () => { const html = describeRender( richText([ paragraph( { type: "text", format: 1, text: "plain strong" }, { type: "text", style: "color:magenta;", text: " not-in-palette" }, ), ]), ); expect(html).not.toMatch(/style=/); // magenta is a named colour outside our palette and must be dropped rather than emitted raw expect(html).toContain("not-in-palette"); }); });