feat: connect company setup data and add about page

This commit is contained in:
2026-10-27 21:36:59 +08:00
parent f990df3023
commit 43edc632a5
8 changed files with 133 additions and 29 deletions
+4 -2
View File
@@ -242,7 +242,7 @@ test("feature grid displays configured icon text", () => {
});
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 settings = { company_name: "Company", contact_phone: "123456", contact_email: "hello@example.com", address: "Address", icp_number: "测试备案号", icp_url: "https://beian.miit.gov.cn/", 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 }] }; } },
@@ -262,6 +262,8 @@ test("footer renders configured navigation, contact and social fields with CMS Q
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));
assert.ok(elements.some((element) => element.props?.href === settings.icp_url));
assert.ok(elements.some((element) => element.props?.children === settings.icp_number));
const fallback = loadModule("components/layout/Footer.tsx", {
"@/services/site-settings.service": { getSiteSettings: async () => null },
"@/services/navigation.service": { getNavigationMenu: async () => null },
@@ -269,5 +271,5 @@ test("footer renders configured navigation, contact and social fields with CMS Q
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"));
assert.ok(!elements.some((element) => element.props?.href === settings.icp_url));
});
+77
View File
@@ -0,0 +1,77 @@
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const vm = require("node:vm");
const { test } = require("node:test");
const ts = require("typescript");
function loadModule(relativePath, mocks = {}) {
const source = fs.readFileSync(path.join(__dirname, "..", relativePath), "utf8");
const compiled = ts.transpileModule(source, {
compilerOptions: { target: ts.ScriptTarget.ES2020, module: ts.ModuleKind.CommonJS, jsx: ts.JsxEmit.ReactJSX },
}).outputText;
const module = { exports: {} };
vm.runInNewContext(compiled, {
module, exports: module.exports, process, URLSearchParams, URL,
require: (name) => name in mocks ? mocks[name] : require(name),
});
return module.exports;
}
function nodes(element) {
if (!element || typeof element !== "object") return [];
const children = element.props?.children;
return [element, ...(Array.isArray(children) ? children.flat(Infinity) : [children]).flatMap(nodes)];
}
test("header reads company name and CMS navigation", async () => {
const { default: Header } = loadModule("components/layout/Header.tsx", {
"@/services/navigation.service": { getNavigationMenu: async () => ({ items: [{ label: "关于我们", url: "/about", open_in_new_tab: false }] }) },
"@/services/site-settings.service": { getSiteSettings: async () => ({ company_name: "测试软件公司" }) },
"./MobileNavigation": { default: () => null },
});
const tree = nodes(await Header());
assert.ok(tree.some((node) => node.props?.["aria-label"] === "测试软件公司首页"));
assert.ok(tree.some((node) => node.props?.href === "/about"));
assert.ok(tree.some((node) => node.props?.children === "测试软件公司"));
assert.ok(!tree.some((node) => node.props?.children === "世耀科技"));
});
test("footer does not invent an ICP number for an unconfigured company", async () => {
const { default: Footer } = loadModule("components/layout/Footer.tsx", {
"@/services/navigation.service": { getNavigationMenu: async () => null },
"@/services/site-settings.service": { getSiteSettings: async () => ({ company_name: "测试软件公司", icp_number: "" }) },
});
const tree = nodes(await Footer());
assert.ok(!tree.some((node) => node.props?.href === "https://beian.miit.gov.cn/"));
assert.ok(!tree.some((node) => typeof node.props?.children === "string" && node.props.children.includes("冀ICP备")));
});
test("about page uses CMS content, supports drafts and returns 404 when missing", async () => {
const page = { title: "关于我们", meta: { slug: "about" }, body: "<p>企业介绍</p>" };
let content = page;
let draft = null;
const { default: About } = loadModule("app/about/page.tsx", {
"next/navigation": { notFound: () => { throw new Error("NOT_FOUND"); } },
"@/services/legal.service": { getSimpleContentPageBySlug: async (slug) => { assert.equal(slug, "about"); return content; } },
"@/services/preview.service": { getPreviewOrFallback: async (type, fields, fallback) => { assert.equal(type, "core.simplecontentpage"); assert.equal(fields, "body"); return draft ?? fallback(); } },
"@/services/seo.service": {},
"@/components/layout/PageSEO": { default: () => null },
});
assert.ok(nodes(await About()).some((node) => node.props?.dangerouslySetInnerHTML?.__html === page.body));
draft = { ...page, body: "<p>未发布的介绍</p>" };
assert.ok(nodes(await About()).some((node) => node.props?.dangerouslySetInnerHTML?.__html === draft.body));
draft = { ...page, meta: { slug: "privacy-policy" }, body: "<p>其他页面的草稿</p>" };
assert.ok(nodes(await About()).some((node) => node.props?.dangerouslySetInnerHTML?.__html === page.body));
draft = null;
content = null;
await assert.rejects(About(), /NOT_FOUND/);
});
test("simple content preview resolves only supported about route", () => {
const { resolvePreviewPath } = loadModule("services/preview.service.ts", {
"next/headers": {}, "./api-client": {},
});
assert.equal(resolvePreviewPath("core.simplecontentpage", { meta: { slug: "about" } }), "/about");
assert.equal(resolvePreviewPath("core.simplecontentpage", { meta: { slug: "unknown" } }), null);
});