Просмотр исходного кода

✅ test(admin): fix test failures and improve test stability

- add next-themes component mocks to admin activity and route tests
- wrap user interactions in act() to properly handle state updates in Users.test.tsx
- ensure consistent test behavior across admin test files
yourname 4 месяцев назад
Родитель
Сommit
f4043bb3de

+ 9 - 0
tests/integration/client/admin/activities.test.tsx

@@ -8,6 +8,15 @@ import { TestWrapper } from '~/utils/client/test-render';
 // Import mocked modules
 import { activityClient } from '@/client/api';
 
+// Mock next-themes 组件
+vi.mock('next-themes', () => ({
+  ThemeProvider: ({ children }: { children: React.ReactNode }) => children,
+  useTheme: () => ({
+    theme: 'light',
+    setTheme: vi.fn(),
+  }),
+}));
+
 // Mock API 客户端
 vi.mock('@/client/api', () => ({
   activityClient: {

+ 9 - 0
tests/integration/client/admin/routes.test.tsx

@@ -8,6 +8,15 @@ import { TestWrapper } from '~/utils/client/test-render';
 // Import mocked modules
 import { routeClient } from '@/client/api';
 
+// Mock next-themes 组件
+vi.mock('next-themes', () => ({
+  ThemeProvider: ({ children }: { children: React.ReactNode }) => children,
+  useTheme: () => ({
+    theme: 'light',
+    setTheme: vi.fn(),
+  }),
+}));
+
 // Mock API 客户端
 vi.mock('@/client/api', () => ({
   routeClient: {

+ 11 - 7
tests/unit/client/pages/Users.test.tsx

@@ -1,5 +1,5 @@
 import { describe, it, expect, vi, beforeEach } from 'vitest';
-import { render, screen, waitFor } from '@testing-library/react';
+import { render, screen, waitFor, act } from '@testing-library/react';
 import userEvent from '@testing-library/user-event';
 import '@testing-library/jest-dom';
 import { TestWrapper } from '~/utils/client/test-render';
@@ -139,9 +139,11 @@ describe('UsersPage Component', () => {
 
     // 在搜索框中输入关键词 - 使用paste来避免防抖中间状态
     const searchInput = screen.getByPlaceholderText('搜索用户名、昵称或邮箱...');
-    await user.clear(searchInput);
-    await user.click(searchInput);
-    await user.paste('admin');
+    await act(async () => {
+      await user.clear(searchInput);
+      await user.click(searchInput);
+      await user.paste('admin');
+    });
 
     // 等待防抖完成(300ms + 缓冲时间)
     await new Promise(resolve => setTimeout(resolve, 400));
@@ -637,9 +639,11 @@ describe('UsersPage Component', () => {
 
     // 搜索不存在的用户
     const searchInput = screen.getByPlaceholderText('搜索用户名、昵称或邮箱...');
-    await user.clear(searchInput);
-    await user.click(searchInput);
-    await user.paste('nonexistent');
+    await act(async () => {
+      await user.clear(searchInput);
+      await user.click(searchInput);
+      await user.paste('nonexistent');
+    });
 
     // 等待防抖完成
     await new Promise(resolve => setTimeout(resolve, 400));