安全/鉴权:登出清服务端 httpOnly 令牌、登录 IP 限流前置 bcrypt、停用账号 令牌在 6 个内联 role handler 失效、admin/items 存在性 oracle 认证前置、 同意偏好 sanitize fail-closed、api-crypto 按 HTTP 方法判体。 CMS:workflow 状态更新+审计收进交互式 $transaction(失败即回滚)、编辑器 richtext 走 textarea 防换行损毁、array/object 子字段按作用域读写防污染、 about/contact/erp-upgrade 补 ISR revalidate 与路由映射。 UI/a11y:AnimatedCounter 入视口前显起点、CLS 累计上报、MetricCard 动画入 effect 并卸载取消 rAF、后台状态四态映射/搜索防抖+序号守卫/媒体单 input/ 吞错改横幅/表单 label-aria 关联。 新增对应判别性单测与 pagination/rate-limit/client-ip/sanitize/validate-content-data 等模块及 tests-integration 真库集成层。
530 lines
20 KiB
TypeScript
530 lines
20 KiB
TypeScript
import { describe, it, expect, jest, beforeEach } from "@jest/globals";
|
||
import { createElement } from "react";
|
||
import {
|
||
render,
|
||
screen,
|
||
within,
|
||
act,
|
||
type RenderResult,
|
||
} from "@testing-library/react";
|
||
import "@testing-library/jest-dom";
|
||
import CaseDetailPage, { type CaseDetailData } from "./case-detail-page";
|
||
|
||
// ─── Mocks ───────────────────────────────────────────────────────────────
|
||
// motion 的 style 上挂的是 MotionValue,jsdom 无法求值:useTransform 降级为普通数值,
|
||
// 断言只落在「解析出的数据」与「条件渲染结果」上,不依赖动画实现。
|
||
jest.mock("framer-motion", () => {
|
||
// 动画专属 props 落在 DOM 上只会让 React 报未知属性,且 jsdom 无法求值:统一剔除,只保留结构相关属性
|
||
const animationOnly = [
|
||
"initial",
|
||
"animate",
|
||
"exit",
|
||
"variants",
|
||
"whileInView",
|
||
"viewport",
|
||
"transition",
|
||
"whileHover",
|
||
"whileTap",
|
||
"layout",
|
||
];
|
||
const motionTag =
|
||
(tag: string, testId: string) =>
|
||
({ children, ...rest }: any) => {
|
||
for (const key of animationOnly) delete rest[key];
|
||
return createElement(tag, { "data-testid": testId, ...rest }, children);
|
||
};
|
||
return {
|
||
motion: {
|
||
div: motionTag("div", "motion-div"),
|
||
h1: motionTag("h1", "motion-h1"),
|
||
h2: motionTag("h2", "motion-h2"),
|
||
p: motionTag("p", "motion-p"),
|
||
span: motionTag("span", "motion-span"),
|
||
},
|
||
useInView: jest.fn(() => true),
|
||
useScroll: jest.fn(() => ({ scrollYProgress: { get: () => 0 } })),
|
||
useTransform: jest.fn(() => 1),
|
||
AnimatePresence: ({ children }: any) => <>{children}</>,
|
||
};
|
||
});
|
||
|
||
jest.mock("@/hooks/use-reduced-motion", () => ({
|
||
useReducedMotion: jest.fn(() => false),
|
||
}));
|
||
|
||
jest.mock("@/components/ui/scroll-reveal", () => ({
|
||
ScrollReveal: ({ children, className }: any) => (
|
||
<div data-testid="scroll-reveal" className={className}>
|
||
{children}
|
||
</div>
|
||
),
|
||
StaggerReveal: ({ children, className }: any) => (
|
||
<div data-testid="stagger-reveal" className={className}>
|
||
{children}
|
||
</div>
|
||
),
|
||
}));
|
||
|
||
jest.mock("@/components/ui/static-link", () => ({
|
||
StaticLink: ({ children, href, className, onClick, ...props }: any) => (
|
||
<a
|
||
href={href}
|
||
className={className}
|
||
onClick={onClick}
|
||
data-testid="static-link"
|
||
{...props}
|
||
>
|
||
{children}
|
||
</a>
|
||
),
|
||
}));
|
||
|
||
jest.mock("@/components/ui/button", () => ({
|
||
Button: ({ children, size, variant }: any) => (
|
||
<span data-testid="mock-button" data-size={size} data-variant={variant}>
|
||
{children}
|
||
</span>
|
||
),
|
||
}));
|
||
|
||
jest.mock("next/link", () => ({
|
||
__esModule: true,
|
||
default: ({ children, href, className, ...props }: any) => (
|
||
<a href={href} className={className} data-testid="next-link" {...props}>
|
||
{children}
|
||
</a>
|
||
),
|
||
}));
|
||
|
||
// 计数器在这里只需要证明「数字被正确拆成 value + suffix + 小数位」,动画本身归 ui 层用例
|
||
jest.mock("@/components/ui/animated-counter", () => ({
|
||
AnimatedCounter: ({ value, decimals, suffix }: any) => (
|
||
<span
|
||
data-testid="animated-counter"
|
||
data-value={value}
|
||
data-decimals={decimals}
|
||
data-suffix={suffix}
|
||
>
|
||
{`${value}${suffix}`}
|
||
</span>
|
||
),
|
||
}));
|
||
|
||
// ─── Test Data ───────────────────────────────────────────────────────────
|
||
|
||
const baseData: CaseDetailData = {
|
||
id: "c1",
|
||
client: "华东某连锁零售",
|
||
industry: "零售",
|
||
title: "全渠道库存中台重构",
|
||
subtitle: "把 12 个系统的库存数据收敛成一份真相",
|
||
challenge: "多仓多店库存不互通,超卖频发",
|
||
solution: "统一库存域模型 + 实时对账链路",
|
||
result: "上线一个季度后超卖归零",
|
||
metrics: [
|
||
{ value: "40%", label: "库存周转提升" },
|
||
{ value: "99.9%", label: "系统可用率", highlight: true },
|
||
{ value: "12", label: "接入系统数" },
|
||
{ value: "3 座", label: "覆盖仓数" },
|
||
{ value: "8 步", label: "未被选中的第五项" },
|
||
],
|
||
timeline: [
|
||
{
|
||
phase: "现状盘点",
|
||
duration: "2 周",
|
||
description: "梳理库存口径与主数据来源",
|
||
},
|
||
{ phase: "模型重构", duration: "6 周", description: "落地统一库存域模型" },
|
||
],
|
||
services: [
|
||
{ id: "consulting", title: "战略咨询", description: "业务诊断" },
|
||
{ id: "data", title: "数据治理", description: "口径统一" },
|
||
{ id: "delivery", title: "敏捷交付", description: "系统建设" },
|
||
{ id: "support", title: "长期陪跑", description: "运营支持" },
|
||
],
|
||
testimonial: {
|
||
quote: "超卖问题彻底消失了",
|
||
author: "李总",
|
||
role: "供应链负责人",
|
||
},
|
||
projectDuration: "16 周",
|
||
departments: ["供应链", "电商", "财务"],
|
||
dataScale: "日均 200 万行",
|
||
businessProblem: "库存真相分散在 12 个系统里",
|
||
verified: true,
|
||
};
|
||
|
||
// 页面结构固定为 6 个并列 section,按文档顺序取用即可隔离同名文案
|
||
const SECTION = {
|
||
hero: 0,
|
||
challenge: 1,
|
||
solution: 2,
|
||
results: 3,
|
||
related: 4,
|
||
cta: 5,
|
||
} as const;
|
||
|
||
function section(view: RenderResult, which: number): HTMLElement {
|
||
const el = Array.from(view.container.querySelectorAll("section"))[which];
|
||
if (!el) throw new Error(`第 ${which} 个 section 未渲染`);
|
||
return el;
|
||
}
|
||
|
||
function reduced(v: boolean) {
|
||
const { useReducedMotion } = require("@/hooks/use-reduced-motion");
|
||
(useReducedMotion as jest.Mock).mockReturnValue(v);
|
||
}
|
||
|
||
beforeEach(() => {
|
||
reduced(false);
|
||
});
|
||
|
||
// ─── Hero ────────────────────────────────────────────────────────────────
|
||
|
||
describe("CaseDetailPage · Hero", () => {
|
||
it("leads with industry, client, the first metric and the H1 title", () => {
|
||
const view = render(<CaseDetailPage data={baseData} />);
|
||
const hero = within(section(view, SECTION.hero));
|
||
expect(hero.getByText("零售")).toBeInTheDocument();
|
||
expect(hero.getByText("华东某连锁零售")).toBeInTheDocument();
|
||
expect(hero.getByText("40%")).toBeInTheDocument();
|
||
expect(
|
||
hero.getByText("把 12 个系统的库存数据收敛成一份真相"),
|
||
).toBeInTheDocument();
|
||
expect(hero.getByRole("heading", { level: 1 })).toHaveTextContent(
|
||
"全渠道库存中台重构",
|
||
);
|
||
});
|
||
|
||
it("shows only metrics 2-4 in the quick stats row and brand-reds the highlighted one", () => {
|
||
const view = render(<CaseDetailPage data={baseData} />);
|
||
const hero = within(section(view, SECTION.hero));
|
||
expect(hero.getByText("99.9%")).toBeInTheDocument();
|
||
expect(hero.getByText("12")).toBeInTheDocument();
|
||
expect(hero.getByText("3 座")).toBeInTheDocument();
|
||
expect(hero.queryByText("8 步")).not.toBeInTheDocument();
|
||
// highlight 决定数字用品牌红还是正文色(CSS 变量原样透传,不做解析)
|
||
const brand = Array.from(
|
||
section(view, SECTION.hero).querySelectorAll(
|
||
'[style*="var(--color-brand)"]',
|
||
),
|
||
);
|
||
expect(brand.filter((n) => n.textContent === "99.9%")).toHaveLength(1);
|
||
expect(brand.some((n) => n.textContent === "3 座")).toBe(false);
|
||
});
|
||
|
||
it("无 metrics 时不编造成果数字(N-30:原实现兜底渲染 40% / 核心成果提升)", () => {
|
||
const view = render(<CaseDetailPage data={{ ...baseData, metrics: [] }} />);
|
||
const hero = within(section(view, SECTION.hero));
|
||
// AGENTS.md §3「零编造」:页面上出现的具体百分比必须来自数据本身。
|
||
expect(hero.queryByText("40%")).toBeNull();
|
||
expect(hero.queryByText("核心成果提升")).toBeNull();
|
||
// 快照区仍在结构里,但没有一张数据卡
|
||
expect(
|
||
section(view, SECTION.hero).querySelector('[class*="grid-cols-3"]')
|
||
?.children,
|
||
).toHaveLength(0);
|
||
});
|
||
|
||
it("renders every verifiable dimension with departments joined by 、", () => {
|
||
const view = render(<CaseDetailPage data={baseData} />);
|
||
const hero = within(section(view, SECTION.hero));
|
||
expect(hero.getByText("项目周期")).toBeInTheDocument();
|
||
expect(hero.getByText("16 周")).toBeInTheDocument();
|
||
expect(hero.getByText("涉及部门")).toBeInTheDocument();
|
||
expect(hero.getByText("供应链、电商、财务")).toBeInTheDocument();
|
||
expect(hero.getByText("数据规模")).toBeInTheDocument();
|
||
expect(hero.getByText("日均 200 万行")).toBeInTheDocument();
|
||
expect(hero.getByText("✓ 客户授权公开")).toBeInTheDocument();
|
||
});
|
||
|
||
it("hides each dimension whose data is missing, empty or false", () => {
|
||
const view = render(
|
||
<CaseDetailPage
|
||
data={{
|
||
...baseData,
|
||
projectDuration: undefined,
|
||
departments: [],
|
||
dataScale: undefined,
|
||
verified: false,
|
||
}}
|
||
/>,
|
||
);
|
||
const hero = within(section(view, SECTION.hero));
|
||
expect(hero.queryByText("项目周期")).not.toBeInTheDocument();
|
||
expect(hero.queryByText("涉及部门")).not.toBeInTheDocument();
|
||
expect(hero.queryByText("数据规模")).not.toBeInTheDocument();
|
||
expect(hero.queryByText("✓ 客户授权公开")).not.toBeInTheDocument();
|
||
});
|
||
|
||
it("drops only the dimensions that are absent instead of the whole row", () => {
|
||
const view = render(
|
||
<CaseDetailPage
|
||
data={{
|
||
...baseData,
|
||
departments: ["供应链"],
|
||
projectDuration: undefined,
|
||
}}
|
||
/>,
|
||
);
|
||
const hero = within(section(view, SECTION.hero));
|
||
expect(hero.queryByText("项目周期")).not.toBeInTheDocument();
|
||
expect(hero.getByText("供应链")).toBeInTheDocument();
|
||
expect(hero.getByText("数据规模")).toBeInTheDocument();
|
||
});
|
||
|
||
it("offers the consult CTA and an in-page anchor to the solution", () => {
|
||
const view = render(<CaseDetailPage data={baseData} />);
|
||
const hero = within(section(view, SECTION.hero));
|
||
expect(hero.getByText("预约咨询")).toBeInTheDocument();
|
||
expect(hero.getByRole("link", { name: "查看方案" })).toHaveAttribute(
|
||
"href",
|
||
"#solution",
|
||
);
|
||
});
|
||
});
|
||
|
||
// ─── 挑战 / 方案 ─────────────────────────────────────────────────────────
|
||
|
||
describe("CaseDetailPage · 挑战与方案", () => {
|
||
it("puts the concrete business problem above the challenge statement", () => {
|
||
const view = render(<CaseDetailPage data={baseData} />);
|
||
const challenge = within(section(view, SECTION.challenge));
|
||
expect(challenge.getByText("具体业务问题")).toBeInTheDocument();
|
||
expect(
|
||
challenge.getByText("库存真相分散在 12 个系统里"),
|
||
).toBeInTheDocument();
|
||
expect(challenge.getByText("面临的挑战")).toBeInTheDocument();
|
||
expect(
|
||
challenge.getByText("多仓多店库存不互通,超卖频发"),
|
||
).toBeInTheDocument();
|
||
});
|
||
|
||
it("drops the business problem block when the case has none", () => {
|
||
const view = render(
|
||
<CaseDetailPage data={{ ...baseData, businessProblem: undefined }} />,
|
||
);
|
||
const challenge = within(section(view, SECTION.challenge));
|
||
expect(challenge.queryByText("具体业务问题")).not.toBeInTheDocument();
|
||
expect(
|
||
challenge.getByText("多仓多店库存不互通,超卖频发"),
|
||
).toBeInTheDocument();
|
||
});
|
||
|
||
it("numbers the implementation path steps in order", () => {
|
||
const view = render(<CaseDetailPage data={baseData} />);
|
||
const solution = within(section(view, SECTION.solution));
|
||
expect(
|
||
solution.getByText("统一库存域模型 + 实时对账链路"),
|
||
).toBeInTheDocument();
|
||
expect(solution.getByText("现状盘点")).toBeInTheDocument();
|
||
expect(solution.getByText("梳理库存口径与主数据来源")).toBeInTheDocument();
|
||
expect(solution.getByText("落地统一库存域模型")).toBeInTheDocument();
|
||
expect(solution.getByText("2 周")).toBeInTheDocument();
|
||
expect(solution.getByText("6 周")).toBeInTheDocument();
|
||
expect(solution.getAllByText(/^[12]$/).map((n) => n.textContent)).toEqual([
|
||
"1",
|
||
"2",
|
||
]);
|
||
});
|
||
|
||
it("keeps the 实施路径 heading when the timeline is empty", () => {
|
||
const view = render(
|
||
<CaseDetailPage data={{ ...baseData, timeline: [] }} />,
|
||
);
|
||
const solution = within(section(view, SECTION.solution));
|
||
expect(solution.getByText("实施路径")).toBeInTheDocument();
|
||
expect(solution.queryByText("现状盘点")).not.toBeInTheDocument();
|
||
});
|
||
});
|
||
|
||
// ─── 成果 ────────────────────────────────────────────────────────────────
|
||
|
||
describe("CaseDetailPage · 成果", () => {
|
||
it("counts up the first four metrics, splitting value into number and suffix", () => {
|
||
const view = render(<CaseDetailPage data={baseData} />);
|
||
const results = within(section(view, SECTION.results));
|
||
const counters = results.getAllByTestId("animated-counter");
|
||
expect(counters).toHaveLength(4); // 第五项被 slice(0, 4) 截断
|
||
expect(counters.some((c) => c.textContent === "8 步")).toBe(false);
|
||
const ninetyNine = counters.find((c) => c.textContent === "99.9%");
|
||
expect(ninetyNine).toHaveAttribute("data-value", "99.9");
|
||
expect(ninetyNine).toHaveAttribute("data-decimals", "1");
|
||
const forty = counters.find((c) => c.textContent === "40%");
|
||
expect(forty).toHaveAttribute("data-value", "40");
|
||
expect(forty).toHaveAttribute("data-decimals", "0");
|
||
const plain = counters.find((c) => c.textContent === "12");
|
||
expect(plain).toHaveAttribute("data-suffix", "");
|
||
expect(counters.find((c) => c.textContent === "3 座")).toHaveAttribute(
|
||
"data-suffix",
|
||
" 座",
|
||
);
|
||
expect(results.getByText("可量化的业务成果")).toBeInTheDocument();
|
||
expect(results.getByText("上线一个季度后超卖归零")).toBeInTheDocument();
|
||
});
|
||
|
||
it("keeps a non-numeric metric verbatim instead of animating it", () => {
|
||
const view = render(
|
||
<CaseDetailPage
|
||
data={{
|
||
...baseData,
|
||
metrics: [
|
||
{ value: "N/A", label: "未量化" },
|
||
...baseData.metrics.slice(1),
|
||
],
|
||
}}
|
||
/>,
|
||
);
|
||
const results = within(section(view, SECTION.results));
|
||
expect(results.getByText("N/A")).toBeInTheDocument();
|
||
expect(results.getAllByTestId("animated-counter")).toHaveLength(3);
|
||
});
|
||
|
||
it("badges the highlighted metric as the core indicator", () => {
|
||
const view = render(<CaseDetailPage data={baseData} />);
|
||
const results = within(section(view, SECTION.results));
|
||
expect(results.getAllByText("核心指标")).toHaveLength(1);
|
||
});
|
||
|
||
it("renders no badge when no metric is highlighted", () => {
|
||
const view = render(
|
||
<CaseDetailPage
|
||
data={{
|
||
...baseData,
|
||
metrics: baseData.metrics.map((m) => ({ ...m, highlight: false })),
|
||
}}
|
||
/>,
|
||
);
|
||
expect(
|
||
within(section(view, SECTION.results)).queryByText("核心指标"),
|
||
).not.toBeInTheDocument();
|
||
});
|
||
|
||
it("renders the customer quote with author and role", () => {
|
||
const view = render(<CaseDetailPage data={baseData} />);
|
||
const results = within(section(view, SECTION.results));
|
||
expect(results.getByRole("blockquote")).toHaveTextContent(
|
||
"超卖问题彻底消失了",
|
||
);
|
||
expect(results.getByText("李总")).toBeInTheDocument();
|
||
expect(results.getByText("供应链负责人")).toBeInTheDocument();
|
||
});
|
||
|
||
it("omits the whole testimonial block when the case has none", () => {
|
||
const view = render(
|
||
<CaseDetailPage data={{ ...baseData, testimonial: undefined }} />,
|
||
);
|
||
expect(
|
||
within(section(view, SECTION.results)).queryByRole("blockquote"),
|
||
).not.toBeInTheDocument();
|
||
});
|
||
|
||
it("renders static numbers instead of the counter under reduced motion", () => {
|
||
reduced(true);
|
||
const view = render(<CaseDetailPage data={baseData} />);
|
||
const results = within(section(view, SECTION.results));
|
||
expect(results.queryByTestId("animated-counter")).not.toBeInTheDocument();
|
||
expect(results.getByText("99.9")).toBeInTheDocument();
|
||
expect(results.getByText("40")).toBeInTheDocument();
|
||
// 后缀被拆成独立的小号 span:40% 与 99.9% 各一个,'12' 无后缀不产生
|
||
expect(results.getAllByText("%")).toHaveLength(2);
|
||
expect(results.getByText("3")).toBeInTheDocument();
|
||
expect(results.getByText("座")).toBeInTheDocument();
|
||
});
|
||
|
||
it("starts the counters only when the results block actually enters the viewport", () => {
|
||
// 全局 IntersectionObserver 桩恒定报 isIntersecting: true,这里换成手动触发的桩以覆盖「未进入视口」与「进入后即停止观察」
|
||
const NativeIO = global.IntersectionObserver;
|
||
const observers: StubIO[] = [];
|
||
class StubIO {
|
||
targets: Element[] = [];
|
||
disconnected = false;
|
||
constructor(
|
||
private readonly cb: (
|
||
entries: Array<{ isIntersecting: boolean }>,
|
||
obs: StubIO,
|
||
) => void,
|
||
) {
|
||
observers.push(this);
|
||
}
|
||
observe(el: Element) {
|
||
this.targets.push(el);
|
||
}
|
||
unobserve() {}
|
||
disconnect() {
|
||
this.disconnected = true;
|
||
}
|
||
fire(entries: Array<{ isIntersecting: boolean }>) {
|
||
this.cb(entries, this);
|
||
}
|
||
}
|
||
global.IntersectionObserver =
|
||
StubIO as unknown as typeof IntersectionObserver;
|
||
const view = render(<CaseDetailPage data={baseData} />);
|
||
try {
|
||
const results = within(section(view, SECTION.results));
|
||
expect(observers).toHaveLength(1);
|
||
expect(observers[0]!.targets).toHaveLength(1);
|
||
// 视口外:数字保持解析后的原值,计数器不挂载
|
||
expect(results.queryByTestId("animated-counter")).not.toBeInTheDocument();
|
||
expect(results.getByText("99.9")).toBeInTheDocument();
|
||
|
||
// 回传空列表 / isIntersecting 为 false 都不能提前点亮计数器
|
||
observers[0]!.fire([]);
|
||
act(() => observers[0]!.fire([{ isIntersecting: false }]));
|
||
expect(results.queryByTestId("animated-counter")).not.toBeInTheDocument();
|
||
expect(observers[0]!.disconnected).toBe(false);
|
||
|
||
act(() => observers[0]!.fire([{ isIntersecting: true }]));
|
||
expect(results.getAllByTestId("animated-counter")).toHaveLength(4);
|
||
expect(observers[0]!.disconnected).toBe(true); // 只观察一次
|
||
} finally {
|
||
global.IntersectionObserver = NativeIO; // 卸载留给 RTL 自动 cleanup,effect 清的是实例本身
|
||
}
|
||
});
|
||
});
|
||
|
||
// ─── 相关服务 / CTA ──────────────────────────────────────────────────────
|
||
|
||
describe("CaseDetailPage · 相关服务与 CTA", () => {
|
||
it("lists at most three related services linked by id", () => {
|
||
const view = render(<CaseDetailPage data={baseData} />);
|
||
const related = within(section(view, SECTION.related));
|
||
const links = related.getAllByTestId("next-link");
|
||
expect(links).toHaveLength(3);
|
||
expect(links.map((l) => l.getAttribute("href"))).toEqual([
|
||
"/services/consulting",
|
||
"/services/data",
|
||
"/services/delivery",
|
||
]);
|
||
expect(related.getByText("战略咨询")).toBeInTheDocument();
|
||
expect(related.queryByText("长期陪跑")).not.toBeInTheDocument();
|
||
});
|
||
|
||
it("renders an empty service grid when the case declares no services", () => {
|
||
const view = render(
|
||
<CaseDetailPage data={{ ...baseData, services: [] }} />,
|
||
);
|
||
const related = within(section(view, SECTION.related));
|
||
expect(related.getByText("相关")).toBeInTheDocument();
|
||
expect(related.queryAllByTestId("next-link")).toHaveLength(0);
|
||
});
|
||
|
||
it("closes with the two conversion links and the shared CTA label", () => {
|
||
const view = render(<CaseDetailPage data={baseData} />);
|
||
const cta = within(section(view, SECTION.cta));
|
||
const links = cta.getAllByTestId("static-link");
|
||
expect(links.map((a) => a.getAttribute("href"))).toEqual([
|
||
"/contact",
|
||
"/cases",
|
||
]);
|
||
expect(cta.getByText("预约咨询")).toBeInTheDocument();
|
||
expect(cta.getByText("查看更多案例")).toBeInTheDocument();
|
||
});
|
||
|
||
it("does not mount its own reading-progress bar(P-4:全站唯一实例归 layout)", () => {
|
||
render(<CaseDetailPage data={baseData} />);
|
||
expect(screen.queryByTestId("scroll-progress")).not.toBeInTheDocument();
|
||
});
|
||
});
|