55 lines
2.2 KiB
TypeScript
55 lines
2.2 KiB
TypeScript
import { campaignJsonForAttachmentPreview } from "../src/features/campaigns/utils/templatePreviewDraft";
|
|
|
|
function assert(condition: unknown, message = "assertion failed"): void {
|
|
if (!condition) throw new Error(message);
|
|
}
|
|
|
|
function asRecord(value: unknown): Record<string, unknown> {
|
|
return value && typeof value === "object" && !Array.isArray(value) ? value as Record<string, unknown> : {};
|
|
}
|
|
|
|
const draft = {
|
|
recipients: {
|
|
from: { name: "", email: "" },
|
|
to: [{ name: "Bob", email: " bob@example.org " }, { name: "", email: "" }]
|
|
},
|
|
entries: {
|
|
inline: [{
|
|
id: "recipient-1",
|
|
active: true,
|
|
name: "",
|
|
email: "",
|
|
from: [],
|
|
to: [{ name: "", email: "" }, { name: "Alice", email: " alice@example.org " }],
|
|
cc: [],
|
|
bcc: [],
|
|
merge_to: false
|
|
}],
|
|
defaults: {
|
|
name: "",
|
|
email: "",
|
|
to: [{ name: "", email: "" }],
|
|
attachments: []
|
|
}
|
|
}
|
|
};
|
|
|
|
const normalized = campaignJsonForAttachmentPreview(draft);
|
|
const entries = asRecord(normalized.entries);
|
|
const inline = entries.inline as Record<string, unknown>[];
|
|
const entry = inline[0];
|
|
const to = entry.to as Record<string, unknown>[];
|
|
const defaults = asRecord(entries.defaults);
|
|
const recipients = asRecord(normalized.recipients);
|
|
const globalTo = recipients.to as Record<string, unknown>[];
|
|
|
|
assert(recipients.from === undefined, "empty global from object is stripped");
|
|
assert(globalTo.length === 1 && globalTo[0].email === "bob@example.org", "global recipient arrays are sanitized");
|
|
assert(entry.email === undefined, "empty legacy entry email is stripped");
|
|
assert(entry.name === undefined, "empty legacy entry name is stripped");
|
|
assert(Array.isArray(entry.from) && (entry.from as unknown[]).length === 0, "empty address arrays remain arrays");
|
|
assert(to.length === 1 && to[0].email === "alice@example.org", "empty recipient objects are removed and valid addresses are trimmed");
|
|
assert(defaults.email === undefined, "empty defaults email is stripped");
|
|
assert(Array.isArray(defaults.to) && (defaults.to as unknown[]).length === 0, "empty defaults recipients are removed");
|
|
assert((asRecord(draft.entries).inline as Record<string, unknown>[])[0].email === "", "original draft is not mutated");
|