Release v0.1.2
This commit is contained in:
54
webui/tests/template-preview-draft.test.ts
Normal file
54
webui/tests/template-preview-draft.test.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
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");
|
||||
Reference in New Issue
Block a user