feat: render CMS content fields and footer settings
This commit is contained in:
+116
-10
@@ -12,7 +12,7 @@ function loadModule(relativePath, mocks = {}, globals = {}) {
|
||||
}).outputText;
|
||||
const loadedModule = { exports: {} };
|
||||
vm.runInNewContext(compiled, {
|
||||
module: loadedModule, exports: loadedModule.exports, process, URLSearchParams,
|
||||
module: loadedModule, exports: loadedModule.exports, process, URLSearchParams, URL,
|
||||
require: (name) => name in mocks ? mocks[name] : require(name),
|
||||
...globals,
|
||||
});
|
||||
@@ -21,7 +21,16 @@ function loadModule(relativePath, mocks = {}, globals = {}) {
|
||||
|
||||
const page = {
|
||||
title: "Page title",
|
||||
meta: { seo_title: "Standard title", search_description: "Standard description" },
|
||||
id: 1,
|
||||
meta: { slug: "test-slug", seo_title: "Standard title", search_description: "Standard description" },
|
||||
cover_image: { url: "https://cms.shiytech.cn/media/cover.png", title: "Cover" },
|
||||
summary: "Detail summary",
|
||||
intro: "Blog introduction",
|
||||
published_at: "2026-10-10T23:00:00Z",
|
||||
client_name: "Client",
|
||||
industry: "Manufacturing",
|
||||
tags: ["Django"],
|
||||
body: [],
|
||||
seo_title_override: "Custom title | Shiytech",
|
||||
seo_description_override: "Custom description",
|
||||
canonical_url: "https://www.shiytech.cn/products",
|
||||
@@ -29,6 +38,46 @@ const page = {
|
||||
schema_json: { "@type": "CollectionPage" },
|
||||
};
|
||||
|
||||
test("content services request covers, tags and column intros", async () => {
|
||||
for (const [name, list, detail] of [
|
||||
["product", "getProductList", "getProductBySlug"],
|
||||
["solution", "getSolutionList", "getSolutionBySlug"],
|
||||
["case", "getCaseStudyList", "getCaseStudyBySlug"],
|
||||
["blog", "getBlogList", "getBlogBySlug"],
|
||||
]) {
|
||||
const calls = [];
|
||||
const service = loadModule(`services/${name}.service.ts`, { "./api-client": { fetchAPI: async (url) => { calls.push(url); return { items: [page] }; } } });
|
||||
await service[list]();
|
||||
await service[detail]("a&b");
|
||||
for (const url of calls) {
|
||||
const query = new URLSearchParams(url.split("?")[1]);
|
||||
assert.ok(query.get("fields").split(",").includes("cover_image"));
|
||||
if (name === "blog") assert.ok(query.get("fields").split(",").includes("tags"));
|
||||
}
|
||||
assert.equal(new URLSearchParams(calls[1].split("?")[1]).get("slug"), "a&b");
|
||||
}
|
||||
let query;
|
||||
const { getColumnPage } = loadModule("services/page.service.ts", { "./api-client": { fetchAPI: async (url) => { query = new URLSearchParams(url.split("?")[1]); return { items: [] }; } } });
|
||||
assert.equal(await getColumnPage("blog.BlogIndexPage", "a&b"), null);
|
||||
assert.equal(query.get("fields"), "intro");
|
||||
assert.equal(query.get("slug"), "a&b");
|
||||
});
|
||||
|
||||
test("content presentation handles covers, dates, tags and column fallback", async () => {
|
||||
const content = loadModule("components/layout/PageContent.tsx", { "@/services/page.service": { getColumnPage: async () => ({ title: "Column", intro: "Introduction" }) } });
|
||||
assert.equal(content.PageCover({ image: null }), null);
|
||||
assert.equal(content.PageCover({ image: { url: "/cover.png", title: "Cover" } }).props.children.props.alt, "Cover");
|
||||
assert.equal(content.PublicationDate({ value: "invalid" }), null);
|
||||
assert.equal(content.PublicationDate({ value: "2026-10-10T23:00:00Z" }).props.children, "2026年10月11日");
|
||||
assert.equal(content.BlogTags({ tags: [] }), null);
|
||||
assert.equal(content.BlogTags({ tags: ["Django"] }).props.children[0].props.children[1], "Django");
|
||||
const header = await content.ColumnHeading({ type: "blog.BlogIndexPage", slug: "blog", fallback: "Fallback" });
|
||||
assert.equal(header.props.children[0].props.children, "Column");
|
||||
assert.equal(header.props.children[1].props.children, "Introduction");
|
||||
const empty = loadModule("components/layout/PageContent.tsx", { "@/services/page.service": { getColumnPage: async () => { throw new Error("offline"); } } });
|
||||
assert.equal((await empty.ColumnHeading({ fallback: "Fallback" })).props.children[0].props.children, "Fallback");
|
||||
});
|
||||
|
||||
test("SEO metadata includes absolute title, canonical and social image", () => {
|
||||
const { createPageMetadata } = loadModule("services/seo.service.ts", { "./api-client": {} });
|
||||
const metadata = createPageMetadata(page, "Fallback");
|
||||
@@ -120,28 +169,31 @@ test("all CMS page routes wire metadata and JSON-LD to the correct page model",
|
||||
];
|
||||
const { createPageMetadata } = loadModule("services/seo.service.ts", { "./api-client": {} });
|
||||
const PageSEO = () => null;
|
||||
const content = { ColumnHeading: () => null, PageCover: () => null, PublicationDate: () => null, BlogTags: () => null };
|
||||
const previews = [];
|
||||
const service = {
|
||||
getHomePage: async () => page, getContactPage: async () => page,
|
||||
getProductList: async () => [], getSolutionList: async () => [],
|
||||
getCaseStudyList: async () => [], getBlogList: async () => [],
|
||||
getProductList: async () => [page], getSolutionList: async () => [page],
|
||||
getCaseStudyList: async () => [page], getBlogList: async () => [page],
|
||||
getProductBySlug: async () => page, getSolutionBySlug: async () => page,
|
||||
getCaseStudyBySlug: async () => page, getBlogBySlug: async () => page,
|
||||
getSimpleContentPageBySlug: async () => page,
|
||||
};
|
||||
const mocks = {
|
||||
"@/components/layout/PageSEO": { default: PageSEO },
|
||||
"@/components/layout/PageContent": content,
|
||||
"@/blocks/BlockRenderer": { BlockRenderer: () => null },
|
||||
"@/services/preview.service": { getPreviewOrFallback: async (type, fields, fallback) => fallback() },
|
||||
"@/services/preview.service": { getPreviewOrFallback: async (type, fields, fallback) => { previews.push(fields); return fallback(); } },
|
||||
};
|
||||
for (const name of ["page", "product", "solution", "case", "blog", "legal"]) {
|
||||
mocks[`@/services/${name}.service`] = service;
|
||||
}
|
||||
function findSEO(element) {
|
||||
function findElement(element, type) {
|
||||
if (!element || typeof element !== "object") return null;
|
||||
if (element.type === PageSEO) return element;
|
||||
const children = element.props?.children;
|
||||
if (element.type === type) return element;
|
||||
const children = Array.isArray(element) ? element : element.props?.children;
|
||||
for (const child of Array.isArray(children) ? children : [children]) {
|
||||
const found = findSEO(child);
|
||||
const found = findElement(child, type);
|
||||
if (found) return found;
|
||||
}
|
||||
return null;
|
||||
@@ -159,9 +211,63 @@ test("all CMS page routes wire metadata and JSON-LD to the correct page model",
|
||||
assert.equal((await route.generateMetadata(props)).title.absolute, page.seo_title_override, file);
|
||||
assert.equal(calls[0].type, expectedType, file);
|
||||
assert.equal(calls[0].slug, expectedSlug, file);
|
||||
const element = findSEO(await route.default(props));
|
||||
const tree = await route.default(props);
|
||||
const element = findElement(tree, PageSEO);
|
||||
assert.ok(element, file);
|
||||
assert.equal(element.props.type, expectedType, file);
|
||||
assert.equal(element.props.slug, expectedSlug, file);
|
||||
if (/^(products|solutions|cases|blog)\./.test(expectedType)) {
|
||||
assert.equal(findElement(tree, content.PageCover).props.image, page.cover_image, file);
|
||||
if (expectedType.endsWith("IndexPage")) {
|
||||
const heading = findElement(tree, content.ColumnHeading);
|
||||
assert.equal(heading.props.type, expectedType, file);
|
||||
assert.equal(heading.props.slug, expectedSlug, file);
|
||||
} else {
|
||||
assert.ok(previews.at(-1).split(",").includes("cover_image"), file);
|
||||
}
|
||||
if (/^(blog|cases)\./.test(expectedType)) assert.equal(findElement(tree, content.PublicationDate).props.value, page.published_at, file);
|
||||
if (expectedType.startsWith("blog.")) {
|
||||
assert.equal(findElement(tree, content.BlogTags).props.tags, page.tags, file);
|
||||
if (!expectedType.endsWith("IndexPage")) assert.ok(previews.at(-1).split(",").includes("tags"), file);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("feature grid displays configured icon text", () => {
|
||||
const { default: FeatureGrid } = loadModule("blocks/feature-grid/FeatureGrid.tsx");
|
||||
const tree = FeatureGrid({ value: { items: [{ icon: "S", title: "Speed", description: "Fast" }] } });
|
||||
const item = tree.props.children.props.children[1].props.children[0];
|
||||
assert.equal(item.props.children[0].props.children, "S");
|
||||
});
|
||||
|
||||
test("footer renders configured navigation, contact and social fields with CMS QR URL", async () => {
|
||||
const settings = { company_name: "Company", contact_phone: "123456", contact_email: "hello@example.com", address: "Address", wechat_account: "Account", weibo_url: "https://weibo.com/example", wechat_qrcode: { url: "/media/qr.png", title: "QR" } };
|
||||
const { default: Footer } = loadModule("components/layout/Footer.tsx", {
|
||||
"@/services/site-settings.service": { getSiteSettings: async () => settings },
|
||||
"@/services/navigation.service": { getNavigationMenu: async (name) => { assert.equal(name, "footer"); return { items: [{ label: "Contact", url: "/contact", open_in_new_tab: true }] }; } },
|
||||
}, { process: { env: { NEXT_PUBLIC_API_URL: "https://cms.example.com" } } });
|
||||
const elements = [];
|
||||
function collect(element) {
|
||||
if (Array.isArray(element)) { element.forEach(collect); return; }
|
||||
if (!element || typeof element !== "object") return;
|
||||
elements.push(element);
|
||||
collect(element.props?.children);
|
||||
}
|
||||
collect(await Footer());
|
||||
for (const href of ["/contact", "tel:123456", "mailto:hello@example.com", settings.weibo_url]) assert.ok(elements.some((element) => element.props?.href === href));
|
||||
const contact = elements.find((element) => element.props?.href === "/contact");
|
||||
assert.equal(contact.props.target, "_blank");
|
||||
assert.equal(contact.props.rel, "noreferrer");
|
||||
assert.ok(elements.some((element) => element.props?.src === "https://cms.example.com/media/qr.png"));
|
||||
assert.ok(elements.some((element) => element.props?.children === settings.address));
|
||||
assert.ok(elements.some((element) => element.props?.children === settings.wechat_account));
|
||||
const fallback = loadModule("components/layout/Footer.tsx", {
|
||||
"@/services/site-settings.service": { getSiteSettings: async () => null },
|
||||
"@/services/navigation.service": { getNavigationMenu: async () => null },
|
||||
});
|
||||
elements.length = 0;
|
||||
collect(await fallback.default());
|
||||
assert.ok(elements.some((element) => element.props?.href === "/privacy-policy"));
|
||||
assert.ok(elements.some((element) => element.props?.children === "冀ICP备2025130506号-1"));
|
||||
});
|
||||
Reference in New Issue
Block a user