fix(email-renderer): preserve section ancestry in parsed HTML
This commit is contained in:
1 parent
d8b9c2dcb5
commit
2cd8f8ea14
4 files changed
+24
-6
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { parse, type DefaultTreeAdapterMap } from "parse5";
|
||||
import {
|
||||
emailCoreBlocks,
|
||||
renderEmail,
|
||||
@@ -55,6 +56,21 @@ const document: BlockDocument = {
|
||||
};
|
||||
|
||||
describe("email renderer", () => {
|
||||
test("browser HTML parsing preserves every block inside its section", async () => {
|
||||
const rendered = await renderEmail(document);
|
||||
expect(rendered.ok).toBe(true);
|
||||
if (!rendered.ok) return;
|
||||
const tree = parse(rendered.value.parts.html.content);
|
||||
const ancestry = new Map<string, string[]>();
|
||||
const walk = (node: DefaultTreeAdapterMap["node"], parents: string[]) => {
|
||||
const id = "attrs" in node ? node.attrs.find(attr => attr.name === "data-block-id")?.value : undefined;
|
||||
if (id) ancestry.set(id, parents);
|
||||
if ("childNodes" in node) for (const child of node.childNodes) walk(child, id ? [...parents, id] : parents);
|
||||
};
|
||||
walk(tree, []);
|
||||
expect(ancestry.get("heading")).toEqual(["root", "section"]);
|
||||
expect(ancestry.get("button")).toEqual(["root", "section"]);
|
||||
});
|
||||
test("renders deterministic HTML, text, AMP, and MJML", async () => {
|
||||
const first = await renderEmail(document, { amp: true, mjml: true });
|
||||
const second = await renderEmail(document, { amp: true, mjml: true });
|
||||
|
||||
Reference in new issue
Block a user