Files
persionalBrand/apps/admin/test/presentation-bindings.test.mjs
T

127 lines
3.8 KiB
JavaScript

import assert from "node:assert/strict";
import {
applyPresentationValues,
filterSectionsForPreviewTarget,
presentationFieldName,
readPresentationValue,
} from "../src/features/brand/presentation-bindings.ts";
const config = {
founder: "阿成",
visualPresentation: {
home: { worksTitle: "原标题", preserveMe: "保留" },
inquiry: { subtitle: "原说明" },
},
unrelatedSetting: true,
};
const sections = [
{
key: "home",
fields: [
{ key: "founder", binding: "brand.config.founder" },
{ key: "worksTitle", binding: "brand.config.visualPresentation.home.worksTitle" },
],
},
];
const next = applyPresentationValues(config, sections, {
[presentationFieldName("home", "founder")]: "阿成主理人",
[presentationFieldName("home", "worksTitle")]: "今日套餐",
});
assert.equal(readPresentationValue(next, "brand.config.founder"), "阿成主理人");
assert.equal(
readPresentationValue(next, "brand.config.visualPresentation.home.worksTitle"),
"今日套餐",
);
assert.equal(next.visualPresentation.home.preserveMe, "保留");
assert.equal(next.visualPresentation.inquiry.subtitle, "原说明");
assert.equal(next.unrelatedSetting, true);
assert.equal(config.founder, "阿成");
const previewSections = [
{
key: "home",
fields: [
{ key: "location", previewTarget: "home.location" },
{ key: "hero", previewTarget: "home.hero" },
],
},
{ key: "profile", fields: [{ key: "bio", previewTarget: "profile.bio" }] },
];
assert.deepEqual(filterSectionsForPreviewTarget(previewSections, "home.hero"), [
{ key: "home", fields: [{ key: "hero", previewTarget: "home.hero" }] },
]);
assert.equal(filterSectionsForPreviewTarget(previewSections).length, 2);
const unsafe = applyPresentationValues(
{ founder: "原主理人" },
[
{
key: "unsafe",
fields: [
{ key: "tenant", binding: "brand.id" },
{ key: "prototype", binding: "brand.config.__proto__.polluted" },
],
},
],
{
[presentationFieldName("unsafe", "tenant")]: "other-brand",
[presentationFieldName("unsafe", "prototype")]: "yes",
},
);
assert.deepEqual(unsafe, { founder: "原主理人" });
assert.equal({}.polluted, undefined);
const listFields = [
{
key: "details",
fields: [
{ key: "coverage", binding: "brand.config.profilePresentation.coverage", input: "list" },
{
key: "warranty",
binding: "brand.config.profilePresentation.warranty",
input: "list",
itemValueKey: "value",
},
],
},
];
const existingLists = {
profilePresentation: {
coverage: [{ title: "原区域", content: "原说明" }],
warranty: ["原承诺"],
other: "保留",
},
};
assert.deepEqual(
readPresentationValue(
existingLists,
"brand.config.profilePresentation.warranty",
listFields[0].fields[1],
),
[{ value: "原承诺" }],
);
const updatedLists = applyPresentationValues(existingLists, listFields, {
[presentationFieldName("details", "coverage")]: [{ title: "新区域", content: "新说明" }],
[presentationFieldName("details", "warranty")]: [{ value: "新承诺" }, { value: "" }],
});
assert.deepEqual(updatedLists.profilePresentation.coverage, [
{ title: "新区域", content: "新说明" },
]);
assert.deepEqual(updatedLists.profilePresentation.warranty, ["新承诺"]);
assert.equal(updatedLists.profilePresentation.other, "保留");
assert.deepEqual(existingLists.profilePresentation.warranty, ["原承诺"]);
const offering = applyPresentationValues(
{ details: { background: "旧背景", preserve: "保留" } },
[
{
key: "offering",
fields: [{ key: "background", binding: "offering.config.details.background" }],
},
],
{ [presentationFieldName("offering", "background")]: "新背景" },
);
assert.deepEqual(offering.details, { background: "新背景", preserve: "保留" });