瀏覽代碼

✅ test(integration): 重构残疾人管理集成测试以使用共享测试工具

- 导入 `completeRadixSelectFlow` 工具函数用于简化Radix UI选择器交互测试
- 移除对 `@d8d/file-management-ui/components` 的模拟,因为相关组件已更新或移除
- 新增对 `@d8d/shared-ui-components/utils/hc` 中 `rpcClient` 的模拟,提供平台、公司、渠道及残疾人数据的模拟响应
- 将表单测试中的手动 `fireEvent.change` 调用替换为 `completeRadixSelectFlow` 调用,【提升代码可读性和维护性】
- 更新区域选择器的测试ID断言从 `area-select-form` 到 `area-select`,以匹配组件更新
- 在多个测试用例(创建、编辑、搜索、高级搜索)中统一使用工具函数处理选择器交互
yourname 1 周之前
父節點
當前提交
3c4f08caa4

+ 118 - 45
allin-packages/disability-person-management-ui/tests/integration/disability-person.integration.test.tsx

@@ -1,6 +1,7 @@
 import { describe, it, expect, vi, beforeEach } from 'vitest';
 import { render, screen, fireEvent, waitFor, act } from '@testing-library/react';
 import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
+import { completeRadixSelectFlow } from '@d8d/shared-ui-components/tests/utils';
 import DisabilityPersonManagement from '../../src/components/DisabilityPersonManagement';
 import { disabilityClientManager } from '../../src/api/disabilityClient';
 
@@ -383,20 +384,114 @@ vi.mock('@d8d/area-management-ui/api', () => ({
   },
 }));
 
-// Mock 文件选择器组件
-vi.mock('@d8d/file-management-ui/components', () => ({
-  FileSelector: vi.fn(({ onChange, placeholder }) => (
-    <div data-testid="file-selector">
-      <button
-        data-testid="file-selector-button"
-        onClick={() => onChange && onChange(1)}
-      >
-        {placeholder || '选择文件'}
-      </button>
-    </div>
-  )),
+// Mock 共享工具包中的rpcClient函数
+vi.mock('@d8d/shared-ui-components/utils/hc', () => ({
+  rpcClient: vi.fn(() => {
+    // 返回一个模拟的RPC客户端
+    const mockClient = {
+      getAllPlatforms: {
+        $get: vi.fn(() => Promise.resolve({
+          status: 200,
+          json: async () => ({
+            data: [
+              { id: 1, name: '平台1' },
+              { id: 2, name: '平台2' },
+              { id: 3, name: '平台3' }
+            ],
+            total: 3
+          })
+        }))
+      },
+      getAllCompanies: {
+        $get: vi.fn(() => Promise.resolve({
+          status: 200,
+          json: async () => ({
+            data: [
+              { id: 1, name: '公司1' },
+              { id: 2, name: '公司2' },
+              { id: 3, name: '公司3' }
+            ],
+            total: 3
+          })
+        }))
+      },
+      getAllChannels: {
+        $get: vi.fn(() => Promise.resolve({
+          status: 200,
+          json: async () => ({
+            data: [
+              { id: 1, name: '渠道1' },
+              { id: 2, name: '渠道2' },
+              { id: 3, name: '渠道3' }
+            ],
+            total: 3
+          })
+        }))
+      },
+      // 残疾人API - 用于残疾人选择器组件
+      searchDisabledPersons: {
+        $get: vi.fn(() => Promise.resolve({
+          status: 200,
+          json: async () => ({
+            data: [
+              {
+                id: 1,
+                name: '测试残疾人',
+                gender: '男',
+                idCard: '110101199001011234', // 添加身份证号字段
+                disabilityId: 'D123456',
+                disabilityType: '肢体残疾',
+                disabilityLevel: '三级',
+                phone: '13800138000',
+                province: '北京',
+                city: '北京市',
+                provinceId: 1,
+                cityId: 2,
+                isInBlackList: 0
+              }
+            ],
+            total: 1,
+            page: 1,
+            pageSize: 10
+          })
+        }))
+      },
+      getAllDisabledPersons: {
+        $get: vi.fn(() => Promise.resolve({
+          status: 200,
+          json: async () => ({
+            data: [
+              {
+                id: 1,
+                name: '测试残疾人',
+                gender: '男',
+                idCard: '110101199001011234', // 添加身份证号字段
+                disabilityId: 'D123456',
+                disabilityType: '肢体残疾',
+                disabilityLevel: '三级',
+                phone: '13800138000',
+                province: '北京',
+                city: '北京市',
+                provinceId: 1,
+                cityId: 2,
+                isInBlackList: 0
+              }
+            ],
+            total: 1,
+            page: 1,
+            pageSize: 10
+          })
+        }))
+      }
+    };
+
+    return mockClient;
+  })
 }));
 
+
+
+
 describe('残疾人个人管理集成测试', () => {
   let queryClient: QueryClient;
 
@@ -482,16 +577,8 @@ describe('残疾人个人管理集成测试', () => {
     fireEvent.change(disabilityLevelSelect, { target: { value: '二级' } });
 
     // 选择省份和城市
-    const provinceSelect = screen.getByTestId('province-select-form');
-    const citySelect = screen.getByTestId('city-select-form');
-
-    await act(async () => {
-      fireEvent.change(provinceSelect, { target: { value: '1' } });
-    });
-
-    await act(async () => {
-      fireEvent.change(citySelect, { target: { value: '2' } });
-    });
+    await completeRadixSelectFlow('area-select-province', '1', { useFireEvent: true });
+    await completeRadixSelectFlow('area-select-city', '2', { useFireEvent: true });
 
     // 提交表单
     const submitButton = screen.getByText('创建');
@@ -672,19 +759,16 @@ describe('残疾人个人管理集成测试', () => {
     });
 
     // 验证区域选择器存在
-    expect(screen.getByTestId('area-select-form')).toBeInTheDocument();
+    expect(screen.getByTestId('area-select')).toBeInTheDocument();
 
     // 选择省份
-    const provinceSelect = screen.getByTestId('province-select-form');
-    fireEvent.change(provinceSelect, { target: { value: '1' } });
+    await completeRadixSelectFlow('area-select-province', '1', { useFireEvent: true });
 
     // 选择城市
-    const citySelect = screen.getByTestId('city-select-form');
-    fireEvent.change(citySelect, { target: { value: '2' } });
+    await completeRadixSelectFlow('area-select-city', '2', { useFireEvent: true });
 
     // 选择区县
-    const districtSelect = screen.getByTestId('district-select-form');
-    fireEvent.change(districtSelect, { target: { value: '3' } });
+    await completeRadixSelectFlow('area-select-district', '3', { useFireEvent: true });
   });
 
   it('应该测试文件选择器集成', async () => {
@@ -753,28 +837,17 @@ describe('残疾人个人管理集成测试', () => {
     fireEvent.change(idAddressInput, { target: { value: '北京市东城区' } });
 
     // 选择性别
-    const genderSelect = screen.getByTestId('gender-select');
-    fireEvent.change(genderSelect, { target: { value: '男' } });
+    await completeRadixSelectFlow('gender-select', '男', { useFireEvent: true });
 
     // 选择残疾类型
-    const disabilityTypeSelect = screen.getByTestId('disability-type-select');
-    fireEvent.change(disabilityTypeSelect, { target: { value: '视力残疾' } });
+    await completeRadixSelectFlow('disability-type-select', '视力残疾', { useFireEvent: true });
 
     // 选择残疾等级
-    const disabilityLevelSelect = screen.getByTestId('disability-level-select');
-    fireEvent.change(disabilityLevelSelect, { target: { value: '一级' } });
+    await completeRadixSelectFlow('disability-level-select', '一级', { useFireEvent: true });
 
     // 选择省份和城市
-    const provinceSelect = screen.getByTestId('province-select-form');
-    const citySelect = screen.getByTestId('city-select-form');
-
-    await act(async () => {
-      fireEvent.change(provinceSelect, { target: { value: '1' } });
-    });
-
-    await act(async () => {
-      fireEvent.change(citySelect, { target: { value: '2' } });
-    });
+    await completeRadixSelectFlow('area-select-province', '1', { useFireEvent: true });
+    await completeRadixSelectFlow('area-select-city', '2', { useFireEvent: true });
 
     // 提交表单
     const submitButton = screen.getByText('创建');