99 lines
3.5 KiB
TypeScript
99 lines
3.5 KiB
TypeScript
import { describe, expect, test } from "vitest";
|
|
import { createBrandTokens } from "@hivegrid/brand";
|
|
import { buildBrand, reactTransactionalTemplateNames, renderTransactionalEmail, withBrandDefaults } from "./render.js";
|
|
|
|
const brand = withBrandDefaults();
|
|
|
|
describe("renderTransactionalEmail", () => {
|
|
test("maps nested brand tokens and locks the HiveGrid platform identity", () => {
|
|
const mapped = buildBrand(
|
|
createBrandTokens({
|
|
productName: "Tenant Portal",
|
|
siteUrl: "https://tenant.example.test",
|
|
legal: {
|
|
privacyUrl: "https://tenant.example.test/privacy",
|
|
termsUrl: "https://tenant.example.test/terms",
|
|
imprintUrl: "https://tenant.example.test/imprint",
|
|
},
|
|
assets: { logoUrl: "https://tenant.example.test/logo.png" },
|
|
mail: { colors: { primary: "#123456" } },
|
|
})
|
|
);
|
|
|
|
expect(mapped.productName).toBe("Tenant Portal");
|
|
expect(mapped.primaryColor).toBe("#123456");
|
|
expect(mapped.logoUrl).toBe("https://hivegrid.de/hivegrid-logo.png");
|
|
expect(mapped.siteUrl).toBe("https://hivegrid.de");
|
|
expect(mapped.privacyUrl).toBe("https://hivegrid.de/datenschutz");
|
|
expect(mapped.termsUrl).toBe("https://hivegrid.de/agb");
|
|
expect(mapped.imprintUrl).toBe("https://hivegrid.de/impressum");
|
|
});
|
|
|
|
test("exposes all 9 template names", () => {
|
|
const names = reactTransactionalTemplateNames();
|
|
expect(names).toEqual([
|
|
"welcome",
|
|
"magic-link",
|
|
"password-reset",
|
|
"invitation",
|
|
"subscription-success",
|
|
"subscription-failed",
|
|
"invoice-sent",
|
|
"system-message",
|
|
"tenant-notification",
|
|
]);
|
|
});
|
|
|
|
test("renders welcome via dispatch", () => {
|
|
const result = renderTransactionalEmail("welcome", {
|
|
brand,
|
|
recipientName: "Ada",
|
|
actionUrl: "https://app.example.com/login",
|
|
});
|
|
expect(result.html).toContain("Welcome to HiveGrid, Ada!");
|
|
expect(result.text).toContain("Welcome to HiveGrid, Ada!");
|
|
});
|
|
|
|
test("renders invitation via dispatch", () => {
|
|
const result = renderTransactionalEmail("invitation", {
|
|
brand,
|
|
senderName: "Grace",
|
|
senderOrganization: "Example Org",
|
|
actionUrl: "https://app.example.com/invite",
|
|
});
|
|
expect(result.html).toContain("Grace invited you to HiveGrid");
|
|
expect(result.text).toContain("Join Example Org");
|
|
});
|
|
|
|
test("renders a structured system message in HTML and plaintext", () => {
|
|
const result = renderTransactionalEmail("system-message", {
|
|
brand,
|
|
subject: "Appointment confirmed",
|
|
eyebrow: "Appointment",
|
|
title: "We look forward to seeing you",
|
|
intro: "Your appointment is confirmed.",
|
|
actionUrl: "https://app.example.com/bookings/42",
|
|
actionLabel: "View appointment",
|
|
details: [{ label: "Date", value: "17 June 2026, 10:00" }],
|
|
});
|
|
expect(result.html).toContain("https://hivegrid.de/hivegrid-logo.png");
|
|
expect(result.html).toContain("View appointment");
|
|
expect(result.text).toContain("Date");
|
|
expect(result.text).toContain("17 June 2026, 10:00");
|
|
});
|
|
|
|
test("throws on unknown template", () => {
|
|
expect(() => renderTransactionalEmail("unknown" as Parameters<typeof renderTransactionalEmail>[0], {} as never)).toThrow(
|
|
/Unknown React transactional template/
|
|
);
|
|
});
|
|
|
|
test("returns html with <!DOCTYPE html> prefix for SSR mode", () => {
|
|
const { html } = renderTransactionalEmail("welcome", {
|
|
brand,
|
|
actionUrl: "https://app.example.com/login",
|
|
});
|
|
expect(html.startsWith("<!DOCTYPE html>")).toBe(true);
|
|
});
|
|
});
|