fix(email): wrap root blocks in valid MJML layouts
Quality / quality (ubuntu-latest) (push) Failing after 10m0s
Quality / quality (windows-latest) (push) Canceled after 0s

This commit is contained in:
Clintchiz committed 2026-09-05 15:56:23 +05:30
1 parent 7afd60e812
commit 2a93e141aa
4 files changed
+37 -3

No files matched your search

@@ -0,0 +1,21 @@
import { expect, test } from "bun:test";
import type { BlockDocument, BlockNode } from "@wrnexus/blocks";
import { renderEmail, validateMjml } from "../src/index.ts";
const text = (id: string): BlockNode => ({ id, type: "content.text", version: 1, props: { text: id }, styles: {}, slots: {} });
const columns = (id: string): BlockNode => ({ id, type: "layout.columns", version: 1, props: {}, styles: {}, slots: { columns: [0, 1].map(index => ({ id: `${id}-${index}`, type: "layout.column", version: 1, props: { width: "50%" }, styles: {}, slots: { content: [text(`${id}-text-${index}`)] } })) } });
test("root content and separate column rows produce valid ordered MJML without changing the document", async () => {
const document: BlockDocument = { schemaVersion: 1, kind: "email", meta: { subject: "Root blocks", lang: "en" }, theme: { tokens: {} }, root: { id: "root", type: "layout.email-document", version: 1, props: {}, styles: {}, slots: { content: [text("intro"), columns("first"), columns("second"), { id: "cta", type: "content.button", version: 1, props: { label: "Act", href: "https://example.com" }, styles: {}, slots: {} }, text("end")] } } };
const before = structuredClone(document);
const rendered = await renderEmail(document, { mjml: true });
expect(rendered.ok).toBe(true);
if (!rendered.ok) return;
const mjml = rendered.value.exports!.mjml!;
expect(await validateMjml(mjml)).toEqual([]);
expect(mjml.match(/<mj-section\b/g)).toHaveLength(5);
expect(mjml.indexOf("intro")).toBeLessThan(mjml.indexOf("first-text-0"));
expect(mjml.indexOf("first-text-1")).toBeLessThan(mjml.indexOf("second-text-0"));
expect(mjml.indexOf("second-text-1")).toBeLessThan(mjml.indexOf(">Act<"));
expect(document).toEqual(before);
});