From 87342cb2085e69bbf3f296efc1d75c8f33d4763f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=BF=94?= Date: Thu, 9 Apr 2026 13:15:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=9B=86=E6=88=90?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=BB=84=E4=BB=B6=E5=AF=BC=E5=85=A5=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 为 news-section.integration.test.tsx 添加 lucide-react mock - 为 products-section.integration.test.tsx 添加 lucide-react mock - 为 services-section.integration.test.tsx 添加 lucide-react mock - 修复 ESLint 错误:使用具体类型替代 any,添加 displayName,使用 import 替代 require 问题原因:测试文件缺少 lucide-react 图标库的 mock,导致组件渲染失败 解决方案:添加 lucide-react 的 mock,将图标组件替换为简单的 span 元素 测试结果: - news-section.integration.test.tsx: 16 passed - products-section.integration.test.tsx: 16 passed - services-section.integration.test.tsx: 11 passed - 总计: 43 passed --- .../news-section.integration.test.tsx | 14 ++++++++++--- .../products-section.integration.test.tsx | 15 +++++++++++--- .../services-section.integration.test.tsx | 20 ++++++++++++++----- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/components/sections/news-section.integration.test.tsx b/src/components/sections/news-section.integration.test.tsx index d9b9809..50af6c3 100644 --- a/src/components/sections/news-section.integration.test.tsx +++ b/src/components/sections/news-section.integration.test.tsx @@ -2,23 +2,31 @@ import { describe, it, expect, beforeEach, afterEach, jest } from '@jest/globals import { render, screen, waitFor } from '@testing-library/react'; import '@testing-library/jest-dom'; import { NewsSection } from './news-section'; +import type { ReactNode } from 'react'; jest.mock('framer-motion', () => ({ motion: { - div: ({ children, ...props }: any) =>
{children}
, + div: ({ children, ...props }: { children?: ReactNode; [key: string]: unknown }) =>
{children}
, }, useInView: () => true, })); jest.mock('next/link', () => { - return ({ children, href }: any) => {children}; + const MockLink = ({ children, href }: { children?: ReactNode; href: string }) => {children}; + MockLink.displayName = 'MockLink'; + return MockLink; }); +jest.mock('lucide-react', () => ({ + ArrowRight: () => ArrowRight, + Calendar: () => Calendar, +})); + jest.mock('@/hooks/use-news', () => ({ useNews: jest.fn(), })); -const { useNews } = require('@/hooks/use-news'); +import { useNews } from '@/hooks/use-news'; describe('NewsSection Integration', () => { beforeEach(() => { diff --git a/src/components/sections/products-section.integration.test.tsx b/src/components/sections/products-section.integration.test.tsx index 7036149..b06d5a2 100644 --- a/src/components/sections/products-section.integration.test.tsx +++ b/src/components/sections/products-section.integration.test.tsx @@ -2,23 +2,32 @@ import { describe, it, expect, beforeEach, afterEach, jest } from '@jest/globals import { render, screen, waitFor } from '@testing-library/react'; import '@testing-library/jest-dom'; import { ProductsSection } from './products-section'; +import type { ReactNode } from 'react'; jest.mock('framer-motion', () => ({ motion: { - div: ({ children, ...props }: any) =>
{children}
, + div: ({ children, ...props }: { children?: ReactNode; [key: string]: unknown }) =>
{children}
, }, useInView: () => true, })); jest.mock('next/link', () => { - return ({ children, href }: any) => {children}; + const MockLink = ({ children, href }: { children?: ReactNode; href: string }) => {children}; + MockLink.displayName = 'MockLink'; + return MockLink; }); +jest.mock('lucide-react', () => ({ + ArrowRight: () => ArrowRight, + Check: () => Check, + TrendingUp: () => TrendingUp, +})); + jest.mock('@/hooks/use-products', () => ({ useProducts: jest.fn(), })); -const { useProducts } = require('@/hooks/use-products'); +import { useProducts } from '@/hooks/use-products'; describe('ProductsSection Integration', () => { beforeEach(() => { diff --git a/src/components/sections/services-section.integration.test.tsx b/src/components/sections/services-section.integration.test.tsx index 3195dcc..e9dfb68 100644 --- a/src/components/sections/services-section.integration.test.tsx +++ b/src/components/sections/services-section.integration.test.tsx @@ -2,23 +2,34 @@ import { describe, it, expect, beforeEach, afterEach, jest } from '@jest/globals import { render, screen, waitFor } from '@testing-library/react'; import '@testing-library/jest-dom'; import { ServicesSection } from './services-section'; +import type { ReactNode } from 'react'; jest.mock('framer-motion', () => ({ motion: { - div: ({ children, ...props }: any) =>
{children}
, + div: ({ children, ...props }: { children?: ReactNode; [key: string]: unknown }) =>
{children}
, }, useInView: () => true, })); jest.mock('next/link', () => { - return ({ children, href }: any) => {children}; + const MockLink = ({ children, href }: { children?: ReactNode; href: string }) => {children}; + MockLink.displayName = 'MockLink'; + return MockLink; }); +jest.mock('lucide-react', () => ({ + Code: () => Code, + Cloud: () => Cloud, + BarChart3: () => BarChart3, + Shield: () => Shield, + ArrowRight: () => ArrowRight, +})); + jest.mock('@/hooks/use-services', () => ({ useServices: jest.fn(), })); -const { useServices } = require('@/hooks/use-services'); +import { useServices } from '@/hooks/use-services'; describe('ServicesSection Integration', () => { beforeEach(() => { @@ -188,8 +199,7 @@ describe('ServicesSection Integration', () => { render(); await waitFor(() => { - const icons = document.querySelectorAll('svg'); - expect(icons.length).toBeGreaterThan(0); + expect(screen.getByText('Code')).toBeInTheDocument(); }); }); });