1
0
Fork 0
polaris-task-force/tests/int/rich-text-view.int.spec.ts
Z8MB1E 34c0d5681f fix(rich-text): render full lexical formatting incl. colours across all fields
RichText authored in the admin panel was rendered as plain text on the
public site: headings/lists/quotes were flattened by Tailwind preflight
(no @tailwindcss/typography installed), and Payload's default JSX
converter ignored text colours.

- src/lib/lexical: shared editor factory (ptfLexicalEditor) + shared
  colour palette (textState) + colour converter (richTextConverters)
- RichTextView: canonical .lexical-content renderer (colour-aware),
  swapped into every frontend render site
- All rich-text editors route through the factory, so every field
  inherits the coloured editor (incl. the previously-unedited ones)
- .lexical-content CSS extended with checklist/table/heading rules,
  wrapped in @layer components so per-page utilities still override;
  dropped the now-dead per-page prose hacks
- Remove the now-dead briefingConverters in favour of the shared
  converter; add tests/int/rich-text-view.int.spec.ts
2026-11-18 13:10:26 -05:00

93 lines
3.7 KiB
TypeScript

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 <RichText>, 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 [
"<h2>Title</h2>",
"<strong>strong bit</strong>",
"<em> italic</em>",
"<ul",
"alpha",
"beta",
"<ol",
"<blockquote>",
"<hr/>",
]) {
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");
});
});