test(core): 补充单元测试,修复 useCountUp 精度问题,新增项目文档

- 新增 hooks/components/lib 共 9 个测试文件,覆盖边界条件与异常路径
- 补充 animations.test.tsx 用例(RotatingBorder、CounterWithEffect 等)
- 修复 useCountUp 结束时 toFixed 精度问题
- 调整 jest 覆盖率配置为渐进式阈值,收缩收集范围
- 新增 docs/lessons-learned.md(经验教训汇总)与 docs/troubleshooting.md(问题排查索引)
- 更新 README.md 文档索引
This commit is contained in:
张翔 committed 2026-07-07 19:42:50 +08:00
1 parent 55381d7012
commit 38be4a19ef
15 files changed
+2907 -6

No files matched your search

+203
View File
@@ -0,0 +1,203 @@
import { describe, it, expect } from '@jest/globals';
import {
CASE_STUDIES,
CASE_INDUSTRIES,
getCaseBySlug,
getFeaturedCases,
getCasesByIndustry,
} from './cases';
describe('CASE_STUDIES', () => {
it('should have 6 case studies', () => {
expect(CASE_STUDIES.length).toBe(6);
});
it('should have required properties on each case', () => {
CASE_STUDIES.forEach((c) => {
expect(c).toHaveProperty('id');
expect(c).toHaveProperty('slug');
expect(c).toHaveProperty('client');
expect(c).toHaveProperty('industry');
expect(c).toHaveProperty('companySize');
expect(c).toHaveProperty('title');
expect(c).toHaveProperty('subtitle');
expect(c).toHaveProperty('challenge');
expect(c).toHaveProperty('solution');
expect(c).toHaveProperty('result');
expect(c).toHaveProperty('metrics');
expect(c).toHaveProperty('timeline');
expect(c).toHaveProperty('services');
expect(c).toHaveProperty('color');
});
});
it('should have unique slugs', () => {
const slugs = CASE_STUDIES.map((c) => c.slug);
expect(new Set(slugs).size).toBe(slugs.length);
});
it('should have unique ids', () => {
const ids = CASE_STUDIES.map((c) => c.id);
expect(new Set(ids).size).toBe(ids.length);
});
it('should have valid color values', () => {
const validColors = ['brand', 'blue', 'teal', 'amber', 'purple'];
CASE_STUDIES.forEach((c) => {
expect(validColors).toContain(c.color);
});
});
it('should have at least one metric per case', () => {
CASE_STUDIES.forEach((c) => {
expect(c.metrics.length).toBeGreaterThan(0);
});
});
it('should have at least one timeline phase per case', () => {
CASE_STUDIES.forEach((c) => {
expect(c.timeline.length).toBeGreaterThan(0);
});
});
it('should have at least one service per case', () => {
CASE_STUDIES.forEach((c) => {
expect(c.services.length).toBeGreaterThan(0);
});
});
it('should have valid metric structure', () => {
CASE_STUDIES.forEach((c) => {
c.metrics.forEach((m) => {
expect(m).toHaveProperty('value');
expect(m).toHaveProperty('label');
});
});
});
it('should have valid timeline structure', () => {
CASE_STUDIES.forEach((c) => {
c.timeline.forEach((t) => {
expect(t).toHaveProperty('phase');
expect(t).toHaveProperty('duration');
expect(t).toHaveProperty('description');
});
});
});
it('should have valid service structure', () => {
CASE_STUDIES.forEach((c) => {
c.services.forEach((s) => {
expect(s).toHaveProperty('id');
expect(s).toHaveProperty('title');
expect(s).toHaveProperty('description');
});
});
});
it('should have 3 featured cases', () => {
const featured = CASE_STUDIES.filter((c) => c.featured);
expect(featured.length).toBe(3);
});
});
describe('CASE_INDUSTRIES', () => {
it('should have 7 industries', () => {
expect(CASE_INDUSTRIES.length).toBe(7);
});
it('should have required properties', () => {
CASE_INDUSTRIES.forEach((ind) => {
expect(ind).toHaveProperty('id');
expect(ind).toHaveProperty('label');
});
});
it('should have "all" as first industry', () => {
expect(CASE_INDUSTRIES[0]?.id).toBe('all');
expect(CASE_INDUSTRIES[0]?.label).toBe('全部行业');
});
it('should have valid industry ids', () => {
const validIds = ['all', '制造业', '贸易零售', '医疗健康', '教育培训', '金融服务', '物流运输'];
CASE_INDUSTRIES.forEach((ind) => {
expect(validIds).toContain(ind.id);
});
});
});
describe('getCaseBySlug', () => {
it('should return the correct case by slug', () => {
const result = getCaseBySlug('manufacturing-erp-upgrade');
expect(result).toBeDefined();
expect(result?.id).toBe('case-001');
expect(result?.client).toBe('某上市制造企业');
});
it('should return undefined for non-existent slug', () => {
const result = getCaseBySlug('non-existent-slug');
expect(result).toBeUndefined();
});
it('should handle empty string', () => {
const result = getCaseBySlug('');
expect(result).toBeUndefined();
});
});
describe('getFeaturedCases', () => {
it('should return only featured cases', () => {
const result = getFeaturedCases();
expect(result.length).toBe(3);
result.forEach((c) => {
expect(c.featured).toBe(true);
});
});
it('should include manufacturing case', () => {
const result = getFeaturedCases();
const slugs = result.map((c) => c.slug);
expect(slugs).toContain('manufacturing-erp-upgrade');
});
it('should include retail case', () => {
const result = getFeaturedCases();
const slugs = result.map((c) => c.slug);
expect(slugs).toContain('retail-omnichannel');
});
it('should include healthcare case', () => {
const result = getFeaturedCases();
const slugs = result.map((c) => c.slug);
expect(slugs).toContain('healthcare-data-platform');
});
});
describe('getCasesByIndustry', () => {
it('should return all cases when industry is "all"', () => {
const result = getCasesByIndustry('all');
expect(result.length).toBe(6);
});
it('should filter by 制造业', () => {
const result = getCasesByIndustry('制造业');
expect(result.length).toBe(1);
expect(result[0]?.slug).toBe('manufacturing-erp-upgrade');
});
it('should filter by 医疗健康', () => {
const result = getCasesByIndustry('医疗健康');
expect(result.length).toBe(1);
expect(result[0]?.slug).toBe('healthcare-data-platform');
});
it('should return empty array for non-existent industry', () => {
const result = getCasesByIndustry('非存在行业');
expect(result.length).toBe(0);
});
it('should return empty array for empty string', () => {
const result = getCasesByIndustry('');
expect(result.length).toBe(0);
});
});
+126
View File
@@ -0,0 +1,126 @@
import { describe, it, expect } from '@jest/globals';
import {
getProductCrossRefs,
getSolutionCrossRefs,
getServiceCrossRefs,
} from './cross-references';
describe('getProductCrossRefs', () => {
it('should return related solutions for erp', () => {
const result = getProductCrossRefs('erp');
expect(result.length).toBeGreaterThan(0);
const solutionRefs = result.filter((r) => r.type === 'solution');
expect(solutionRefs.length).toBeGreaterThan(0);
solutionRefs.forEach((r) => {
expect(r).toHaveProperty('id');
expect(r).toHaveProperty('title');
expect(r).toHaveProperty('type', 'solution');
expect(r).toHaveProperty('href');
expect(r.href).toMatch(/^\/solutions\//);
expect(r).toHaveProperty('reason');
});
});
it('should return related products for erp', () => {
const result = getProductCrossRefs('erp');
const productRefs = result.filter((r) => r.type === 'product');
productRefs.forEach((r) => {
expect(r).toHaveProperty('id');
expect(r).toHaveProperty('title');
expect(r).toHaveProperty('type', 'product');
expect(r).toHaveProperty('href');
expect(r.href).toMatch(/^\/products\//);
expect(r).toHaveProperty('reason');
});
});
it('should return at most 3 product cross-refs', () => {
const result = getProductCrossRefs('erp');
const productRefs = result.filter((r) => r.type === 'product');
expect(productRefs.length).toBeLessThanOrEqual(3);
});
it('should return empty array for non-existent product', () => {
const result = getProductCrossRefs('non-existent');
expect(result).toEqual([]);
});
it('should return cross-refs for crm', () => {
const result = getProductCrossRefs('crm');
expect(result.length).toBeGreaterThan(0);
const solutionSlugs = result.filter((r) => r.type === 'solution').map((r) => r.id);
expect(solutionSlugs).toContain('retail');
});
});
describe('getSolutionCrossRefs', () => {
it('should return related products for manufacturing', () => {
const result = getSolutionCrossRefs('manufacturing');
expect(result.length).toBeGreaterThan(0);
result.forEach((r) => {
expect(r).toHaveProperty('id');
expect(r).toHaveProperty('title');
expect(r).toHaveProperty('type', 'product');
expect(r).toHaveProperty('href');
expect(r.href).toMatch(/^\/products\//);
expect(r).toHaveProperty('reason');
});
});
it('should include erp and bi for manufacturing', () => {
const result = getSolutionCrossRefs('manufacturing');
const productIds = result.map((r) => r.id);
expect(productIds).toContain('erp');
expect(productIds).toContain('bi');
});
it('should include crm and bi for retail', () => {
const result = getSolutionCrossRefs('retail');
const productIds = result.map((r) => r.id);
expect(productIds).toContain('crm');
expect(productIds).toContain('bi');
});
it('should return empty array for non-existent solution', () => {
const result = getSolutionCrossRefs('non-existent');
expect(result).toEqual([]);
});
});
describe('getServiceCrossRefs', () => {
it('should return related solutions for software service', () => {
const result = getServiceCrossRefs('software');
expect(result.length).toBeGreaterThan(0);
result.forEach((r) => {
expect(r).toHaveProperty('id');
expect(r).toHaveProperty('title');
expect(r).toHaveProperty('type', 'solution');
expect(r).toHaveProperty('href');
expect(r.href).toMatch(/^\/solutions\//);
expect(r).toHaveProperty('reason');
});
});
it('should include manufacturing and retail for software', () => {
const result = getServiceCrossRefs('software');
const solutionIds = result.map((r) => r.id);
expect(solutionIds).toContain('manufacturing');
expect(solutionIds).toContain('retail');
});
it('should return at most 3 solutions', () => {
const result = getServiceCrossRefs('solutions');
expect(result.length).toBeLessThanOrEqual(3);
});
it('should return empty array for non-existent service', () => {
const result = getServiceCrossRefs('non-existent');
expect(result).toEqual([]);
});
it('should return empty array for empty string', () => {
const result = getServiceCrossRefs('');
expect(result).toEqual([]);
});
});