From 9d2b77602dca015446cf9a72e6e9b7b16e9798d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=BF=94?= Date: Tue, 24 Mar 2026 12:36:23 +0800 Subject: [PATCH] =?UTF-8?q?test(contact-section):=20=E4=B8=BA=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=96=87=E4=BB=B6=E6=B7=BB=E5=8A=A0=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=B9=B6=E6=94=B9=E8=BF=9Bmock=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为contact-section测试文件添加了类型定义接口,包括MotionComponentProps、InputComponentProps和ToastComponentProps 替换了原有的any类型,使用更严格的类型定义来改进mock实现 --- .gitignore | 6 +++ .../sections/contact-section.test.tsx | 50 +++++++++++++++---- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 9eda0e5..849674a 100644 --- a/.gitignore +++ b/.gitignore @@ -335,3 +335,9 @@ e2e/storage-state.json *.trace.zip trace.zip network-logs/ + +#trae +.trae/ + +#docs +docs diff --git a/src/components/sections/contact-section.test.tsx b/src/components/sections/contact-section.test.tsx index 47c998f..3040687 100644 --- a/src/components/sections/contact-section.test.tsx +++ b/src/components/sections/contact-section.test.tsx @@ -4,6 +4,33 @@ import { render, screen, fireEvent, waitFor } from '@testing-library/react'; import '@testing-library/jest-dom'; import userEvent from '@testing-library/user-event'; +interface MotionComponentProps { + children?: React.ReactNode; + className?: string; + disabled?: boolean; + [key: string]: unknown; +} + +interface InputComponentProps { + label?: string; + id?: string; + placeholder?: string; + required?: boolean; + value?: string; + onChange?: (e: React.ChangeEvent) => void; + onBlur?: () => void; + error?: string; + rows?: number; + [key: string]: unknown; +} + +interface ToastComponentProps { + message?: string; + type?: string; + onClose?: () => void; + [key: string]: unknown; +} + global.fetch = jest.fn(() => Promise.resolve({ ok: true, @@ -13,18 +40,18 @@ global.fetch = jest.fn(() => jest.mock('framer-motion', () => ({ motion: { - div: ({ children, className, ...props }: any) => ( + div: ({ children, className, ...props }: MotionComponentProps) => (
{children}
), - section: ({ children, className, ...props }: any) => ( + section: ({ children, className, ...props }: MotionComponentProps) => (
{children}
), }, - AnimatePresence: ({ children }: any) => <>{children}, + AnimatePresence: ({ children }: MotionComponentProps) => <>{children}, })); jest.mock('lucide-react', () => ({ @@ -49,7 +76,10 @@ jest.mock('@/lib/csrf', () => ({ getCSRFTokenFromStorage: jest.fn(() => 'test-csrf-token'), })); -const { generateCSRFToken, setCSRFTokenToStorage } = jest.requireMock('@/lib/csrf') as any; +const { generateCSRFToken, setCSRFTokenToStorage } = jest.requireMock('@/lib/csrf') as { + generateCSRFToken: jest.Mock; + setCSRFTokenToStorage: jest.Mock; +}; jest.mock('@/lib/security/captcha', () => ({ generateCaptcha: jest.fn(() => ({ @@ -60,7 +90,9 @@ jest.mock('@/lib/security/captcha', () => ({ })), })); -const { generateCaptcha } = jest.requireMock('@/lib/security/captcha') as any; +const { generateCaptcha } = jest.requireMock('@/lib/security/captcha') as { + generateCaptcha: jest.Mock; +}; jest.mock('@/lib/constants', () => ({ COMPANY_INFO: { @@ -72,7 +104,7 @@ jest.mock('@/lib/constants', () => ({ })); jest.mock('@/components/ui/button', () => ({ - Button: ({ children, className, disabled, ...props }: any) => ( + Button: ({ children, className, disabled, ...props }: MotionComponentProps) => ( @@ -80,7 +112,7 @@ jest.mock('@/components/ui/button', () => ({ })); jest.mock('@/components/ui/input', () => ({ - Input: ({ label, id, placeholder, required, value, onChange, onBlur, error, ...props }: any) => ( + Input: ({ label, id, placeholder, required, value, onChange, onBlur, error, ...props }: InputComponentProps) => (
({ })); jest.mock('@/components/ui/textarea', () => ({ - Textarea: ({ label, id, placeholder, rows, required, value, onChange, onBlur, error, ...props }: any) => ( + Textarea: ({ label, id, placeholder, rows, required, value, onChange, onBlur, error, ...props }: InputComponentProps) => (