Files
novalon-website/src/components/sections/sections.test.tsx
T
zhangxiang 81daedbea1 feat(tokens): 视觉语法收口到对标实测——圆角 0–4 记档、彩色阴影退役、卡片去 lift
依据是实测而非口味。Bain & Company(archive 快照 2026-08-31, 1600×846)与
Porsche Consulting(live 国际站)的 getComputedStyle 抽样收敛到同一条:
**两家全站 border-radius 均为 0**,且面/字色近乎单色
(Bain 字色仅 #000/#FFF/#CC0000/#767676;Porsche 面色 #FFF/#000/#D6001D/#F2F2F2,
红出现 4 次且全部是按钮底、无一次是阴影或背景)。本仓此前的「中度软化」
3→32px 八档 + 三枚品牌红阴影 + 卡片 hover 抬升,三件全是对标没有的语法。

本轮按「借语法、不借色相」执行:保留 #C41E3A 色相与朱砂条,只收口形状与层次。

圆角
- --radius-xs/sm 3/6→0、md/lg 8/12→2、xl/2xl/3xl 18/24/32→4,新增 --radius-cta: 0
- **--radius-full 保持 9999 不动**:它承载真圆(avatar/switch/slider/radio-group/
  progress/scroll-area/skeleton),一并归零会把所有旋钮与头像方角化。这条是
  check:grammar R2/R9 的正向守卫 —— 刻意做成「仪器坏了它仍要失败」的那一维
- --badge-radius 由 var(--radius-full) 改指 var(--radius-cta)(实测该令牌零消费者,
  留档不复用,删除动作留给死码族 DEC-P-2 统一处理)
- button.tsx 的 sm/default/lg/xl 走 rounded-cta,icon 档保持 rounded-full(功能形状)

彩色阴影
- --shadow-brand / --shadow-brand-hover / --shadow-glow 三枚退役(实测前两者仅 2 处、
  后者 0 处消费者),消费方回落中性档
- 同批发现并修掉一处暖化漏网:阴影基色仍是冷墨 rgba(10,14,20,α)=#0A0E14,
  而暖墨 #141210 早已落地 —— globals.css 16 处 + 业务 2 处。check:token-twins 抓不到它,
  因为阴影是字面量 rgba、没有 -rgb 孪生兄弟可对照

卡片
- .bain-card hover 去 translateY(-4px) 与阴影增长,改为 hairline→rule + 底色微移
- --card-shadow-hover / --card-transition 随之退役(前者仅 :hover 消费、后者零消费者)

新增信息线令牌 --color-border-rule #8A8377
hover 边框是「告诉用户这个可交互」的信息,按品牌 P4 三档拆分不能用界灰 #C3BDB1
(实测对卡底仅 1.78,不达非文本 3:1)。#8A8377 由我自己算:对白 3.75、对纸 #FBF9F5
3.57、对暗底 #141210 4.89 —— 两主题都过 3:1,且 -rgb 孪生已同步(44/44)。

动效
- 补 --transition-hover: 150ms 并接进 Tailwind duration 档。动效四原则 CONTEXT.md:76
  早就写了 hover 150ms,仓库从来没有这个档(BRAND-MANUAL §6 冲突 C-1)。
  抬升与光晕去掉后 hover 成了唯一的可交互提示,没档可用等于语法自相矛盾
- cta-button 的 transition-all duration-200(裸字面量)一并改为 transition-colors duration-hover

CTA 两轨(实测尺寸之间取值:Bain 20/32+14px、Porsche 13/24+16px → px-7 py-3.5 + text-sm/500)
- 实心/描边两轨都直角;不做 text-transform(中文无大小写)、不补字距(Bain 实测
  letterSpacing: normal,它的大写感来自全大写本身)
- 箭头只保留在 inline 文字链轨:两家按钮实测都是纯文字
- 描边轨静止边框用 border-border-rule,不用 ink/20(≈1.54 不达 3:1)
- 改动半径实测:63 处 <CTAButton> 调用,其中 36 处不写 variant ⇒ 落默认 primary,
  24 处 secondary、3 处 dark、0 处 inline

新闸 npm run check:grammar(9 条规则)+ check:grammar:selftest(9/9 双向自证)
- 闸只看代码:扫描前统一剥注释。不剥的话,解释「为什么退出 rounded-full」的文档注释
  本身会被判红,于是只剩两条出路 —— 改措辞去绕闸(应试,闸就此失去解释力)或把闸调瞎
- 每条规则报分母;分母为 0 ⇒ exit 2 判「未能度量」,不算通过
- 退出码分档 0/1/2,避免闸自身崩溃与判红共用 exit 1 而吞掉同批已算好的打印

顺带删死码 hero-section-v2.tsx(200 行、单一导出、全站零生产引用,仅被自己的 3 个测试
dynamic import)+ 那 3 个测试。留测试会让死码继续占覆盖率分母,并被误读成「有活在跑」。

KNOWN-RED: test:visual —— 全局令牌改动没有"爆炸半径"可言,页面级 130 张基线预期全部churn。
按计划 §10 只在 #3–#9 落地后一次性重捕(需 AUTH-VISUAL 单独授权),不按笔重写以免归因失效。

门禁实测:type-check EXIT=0 · check:grammar 9/9 · check:grammar:selftest 9/9 ·
check:token-twins 44/44 · check:contrast 44/44 · check:brand-token 0 违规 ·
check:motion 23→22(--transition-* 采集数 6→7,假红已消)· 全量 jest 143 套件 / 1769 例通过
2026-10-16 20:02:11 +08:00

311 lines
11 KiB
TypeScript

import { describe, it, expect, jest } from '@jest/globals';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
// ─── Mocks ───────────────────────────────────────────────────────────────
jest.mock('framer-motion', () => ({
motion: {
div: ({ children, initial, animate, variants, whileInView, whileHover, whileTap, viewport, transition, className, onMouseMove, onMouseEnter, onMouseLeave, style, ref, ...props }: any) => (
<div
data-testid="motion-div"
data-initial={JSON.stringify(initial)}
data-animate={JSON.stringify(animate)}
data-variants={JSON.stringify(variants)}
data-while-hover={JSON.stringify(whileHover)}
data-while-tap={JSON.stringify(whileTap)}
data-while-inview={JSON.stringify(whileInView)}
data-viewport={JSON.stringify(viewport)}
data-transition={JSON.stringify(transition)}
className={className}
style={style}
ref={ref}
onMouseMove={onMouseMove}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
{...props}
>
{children}
</div>
),
p: ({ children, className, ...props }: any) => (
<p data-testid="motion-p" className={className} {...props}>{children}</p>
),
h1: ({ children, className, ...props }: any) => (
<h1 data-testid="motion-h1" className={className} {...props}>{children}</h1>
),
span: ({ children, className, ...props }: any) => (
<span data-testid="motion-span" className={className} {...props}>{children}</span>
),
button: ({ children, onClick, className, ...props }: any) => (
<button
data-testid="motion-button"
onClick={onClick}
className={className}
{...props}
>
{children}
</button>
),
},
useInView: jest.fn(() => true),
AnimatePresence: ({ children }: any) => <>{children}</>,
}));
jest.mock('@/hooks/use-reduced-motion', () => ({
useReducedMotion: jest.fn(() => false),
}));
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, className, ...props }: any) => (
<button className={`${className}${size ? ` btn-${size}` : ''}${variant ? ` btn-${variant}` : ''}`} data-testid="mock-button" {...props}>
{children}
</button>
),
}));
jest.mock('@/components/ui/brand-visuals', () => ({
BrandStamp: ({ children }: { children: React.ReactNode }) => (
<span data-testid="brand-stamp">{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>
),
}));
jest.mock('lucide-react', () => {
const mockIcon = (name: string) => {
const Icon = (props: any) => <svg data-testid={`icon-${name.toLowerCase()}`} className={props.className} strokeWidth={props.strokeWidth} />;
Icon.displayName = name;
return Icon;
};
return {
ArrowRight: mockIcon('arrow-right'),
MessageSquare: mockIcon('message-square'),
Search: mockIcon('search'),
Rocket: mockIcon('rocket'),
Handshake: mockIcon('handshake'),
Sparkles: mockIcon('sparkles'),
MessageCircle: mockIcon('message-circle'),
Clock: mockIcon('clock'),
ShieldCheck: mockIcon('shield-check'),
};
});
// ─── Tests ──────────────────────────────────────────────────────────────
describe('SectionHeader', () => {
it('should render label, title, and description', async () => {
const { SectionHeader } = await import('./section-header');
render(<SectionHeader label="TEST" title="Test Title" desc="Test description" />);
expect(screen.getByText('TEST')).toBeInTheDocument();
expect(screen.getByText('Test Title')).toBeInTheDocument();
expect(screen.getByText('Test description')).toBeInTheDocument();
});
it('should render highlighted text', async () => {
const { SectionHeader } = await import('./section-header');
render(<SectionHeader label="LABEL" title="Hello " highlight="World" />);
expect(screen.getByText('World')).toBeInTheDocument();
});
it('should apply light mode', async () => {
const { SectionHeader } = await import('./section-header');
render(<SectionHeader label="TEST" title="Title" light />);
const heading = screen.getByText('Title');
// Light mode: white text (rendered as rgb)
expect(heading.closest('h2')?.style.color).toBe('rgb(255, 255, 255)');
});
it('washes the description white on dark sections and keeps it muted on light ones', async () => {
const { SectionHeader } = await import('./section-header');
const { unmount } = render(<SectionHeader label="TEST" title="Title" desc="深色底的说明文字" light />);
expect(screen.getByText('深色底的说明文字').style.color).toBe('rgba(255, 255, 255, 0.5)');
unmount();
render(<SectionHeader label="TEST" title="Title" desc="浅色底的说明文字" />);
expect(screen.getByText('浅色底的说明文字').style.color).toBe('var(--color-text-muted)');
});
it('should apply custom className', async () => {
const { SectionHeader } = await import('./section-header');
render(<SectionHeader label="T" title="T" className="my-class" />);
// SectionHeader has multiple motion.div elements, find the first/outer one
const elements = screen.getAllByTestId('motion-div');
const outer = elements.find(el => el.className.includes('my-class'));
expect(outer).toBeTruthy();
});
it('should render without description when not provided', async () => {
const { SectionHeader } = await import('./section-header');
const { container } = render(<SectionHeader label="T" title="T" />);
// There should be no <p> element for description
const paragraphs = container.querySelectorAll('p');
expect(paragraphs.length).toBe(0);
});
});
describe('ProductCard', () => {
it('should render title, description and icon', async () => {
const { ProductCard } = await import('./product-card');
render(
<ProductCard
icon={<span data-testid="custom-icon">★</span>}
title="ERP 系统"
description="企业资源管理系统"
href="/products/erp"
/>
);
expect(screen.getByText('ERP 系统')).toBeInTheDocument();
expect(screen.getByText('企业资源管理系统')).toBeInTheDocument();
expect(screen.getByTestId('custom-icon')).toBeInTheDocument();
expect(screen.getByTestId('static-link')).toHaveAttribute('href', '/products/erp');
});
it('should render badge when provided', async () => {
const { ProductCard } = await import('./product-card');
render(
<ProductCard
icon={<span>★</span>}
title="ERP"
description="Desc"
href="/products/erp"
badge="新版本"
/>
);
expect(screen.getByText('新版本')).toBeInTheDocument();
});
it('should render "了解更多" link', async () => {
const { ProductCard } = await import('./product-card');
render(
<ProductCard
icon={<span>★</span>}
title="ERP"
description="Desc"
href="/products/erp"
/>
);
expect(screen.getByText('了解更多')).toBeInTheDocument();
});
it('should apply custom className', async () => {
const { ProductCard } = await import('./product-card');
render(
<ProductCard
icon={<span>★</span>}
title="ERP"
description="Desc"
href="/products/erp"
className="card-class"
/>
);
const link = screen.getByTestId('static-link');
expect(link.className).toContain('card-class');
});
});
describe('ServiceCard', () => {
it('should render title, description and link', async () => {
const { ServiceCard } = await import('./service-card');
render(
<ServiceCard
title="战略咨询"
description="业务诊断与战略规划"
href="/services/consulting"
/>
);
expect(screen.getByText('战略咨询')).toBeInTheDocument();
expect(screen.getByText('业务诊断与战略规划')).toBeInTheDocument();
expect(screen.getByText('了解详情')).toBeInTheDocument();
expect(screen.getByTestId('next-link')).toHaveAttribute('href', '/services/consulting');
});
it('should apply custom className', async () => {
const { ServiceCard } = await import('./service-card');
render(
<ServiceCard
title="Data"
description="Analysis"
href="/services/data"
className="service-class"
/>
);
const elements = screen.getAllByTestId('motion-div');
const target = elements.find(el => el.className.includes('service-class'));
expect(target).toBeTruthy();
});
it('should accept optional number and color props for compatibility', async () => {
const { ServiceCard } = await import('./service-card');
render(
<ServiceCard
number="03"
title="Blue"
description="Service"
href="/services/test"
color="blue"
/>
);
// ServiceCard has multiple motion.div elements, use getAllByTestId
const elements = screen.getAllByTestId('motion-div');
expect(elements.length).toBeGreaterThan(0);
});
});
describe('CTASection', () => {
it('should render default content', async () => {
const { CTASection } = await import('./cta-section');
render(<CTASection />);
expect(screen.getByText('一起聊聊您的数字化需求')).toBeInTheDocument();
expect(screen.getByText('预约咨询')).toBeInTheDocument();
});
it('should render custom title and description', async () => {
const { CTASection } = await import('./cta-section');
render(
<CTASection
title="Custom Title"
description="Custom description"
primaryLabel="Get Started"
primaryHref="/start"
secondaryLabel="Learn More"
secondaryHref="/about"
/>
);
expect(screen.getByText('Custom Title')).toBeInTheDocument();
expect(screen.getByText('Custom description')).toBeInTheDocument();
expect(screen.getByText('Get Started')).toBeInTheDocument();
expect(screen.getByText('Learn More')).toBeInTheDocument();
});
it('should have proper links', async () => {
const { CTASection } = await import('./cta-section');
render(<CTASection primaryHref="/contact" />);
const links = screen.getAllByTestId('static-link');
expect(links.length).toBeGreaterThan(0);
});
it('should render brand stamp', async () => {
const { CTASection } = await import('./cta-section');
render(<CTASection />);
expect(screen.getByTestId('brand-stamp')).toBeInTheDocument();
});
});
// HeroSectionV2 的三个用例随组件本体一同删除(2026-10-16):
// 该组件全站零生产引用(仅被本测试文件 dynamic import),属 qa-tracker DEC-P-2 死码族。
// 保留测试会让死码继续占用覆盖率分母,并在视觉上被误读为「有活在跑」。