Pārlūkot izejas kodu

✅ test(area-select-form): 修复集成测试中的元素选择逻辑

- 将注释更新为更准确地描述等待省份数据加载并启用的状态
- 将 `getByText` 替换为 `findAllByText` 以处理多个匹配元素的情况
- 添加对匹配元素数量的断言,确保至少存在一个匹配项
- 更新城市和区县的选择逻辑,通过索引选择第二个匹配的 span 元素进行点击
- 在异步操作中包装 `fireEvent.click` 以确保状态更新完成
yourname 1 nedēļu atpakaļ
vecāks
revīzija
57e7ab5d84

+ 22 - 13
packages/area-management-ui/tests/integration/area-select-form.integration.test.tsx

@@ -219,7 +219,7 @@ describe('AreaSelectForm 集成测试', () => {
     render(<TestForm />);
 
     const provinceSelect = screen.getByTestId('test-area-province');
-    // 等待省份数据加载
+    // 等待省份数据加载并启用
     await waitFor(() => {
       expect(provinceSelect).toBeInTheDocument();
       expect(provinceSelect).not.toHaveAttribute('disabled');
@@ -246,12 +246,15 @@ describe('AreaSelectForm 集成测试', () => {
     fireEvent.click(citySelect);
 
     // 等待城市选项出现 - 城市数据应该是"北京市辖区"
-    await waitFor(() => {
-      expect(screen.getByText('北京市辖区')).toBeInTheDocument();
+    await waitFor(async () => {
+      const cityOptions = await screen.findAllByText('北京市辖区');
+      expect(cityOptions.length).toBeGreaterThanOrEqual(1);
     });
 
-    // 选择北京市辖区(城市)
-    fireEvent.click(screen.getByText('北京市辖区'));
+    // 选择北京市辖区(城市)- 使用 findAllByText 获取所有匹配,点击第二个(显示的span)
+    const cityOptions = await screen.findAllByText('北京市辖区');
+    expect(cityOptions.length).toBeGreaterThanOrEqual(2);
+    fireEvent.click(cityOptions[1]);
 
     // 提交表单
     const submitButton = screen.getByTestId('submit-button');
@@ -369,13 +372,16 @@ describe('AreaSelectForm 集成测试', () => {
     });
 
     // 等待城市选项出现 - 城市数据应该是"北京市辖区"
-    await waitFor(() => {
-      expect(screen.getByText('北京市辖区')).toBeInTheDocument();
+    await waitFor(async () => {
+      const cityOptions = await screen.findAllByText('北京市辖区');
+      expect(cityOptions.length).toBeGreaterThanOrEqual(1);
     });
 
-    // 选择北京市辖区(城市)
+    // 选择北京市辖区(城市)- 使用 findAllByText 获取所有匹配,点击第二个(显示的span)
+    const cityOptions = await screen.findAllByText('北京市辖区');
+    expect(cityOptions.length).toBeGreaterThanOrEqual(2);
     await act(async () => {
-      fireEvent.click(screen.getByText('北京市辖区'));
+      fireEvent.click(cityOptions[1]);
     });
 
     // 等待区县选择启用
@@ -391,13 +397,16 @@ describe('AreaSelectForm 集成测试', () => {
     });
 
     // 等待区县选项出现
-    await waitFor(() => {
-      expect(screen.getByText('东城区')).toBeInTheDocument();
+    await waitFor(async () => {
+      const districtOptions = await screen.findAllByText('东城区');
+      expect(districtOptions.length).toBeGreaterThanOrEqual(1);
     });
 
-    // 选择东城区
+    // 选择东城区 - 使用 findAllByText 获取所有匹配,点击第二个(显示的span)
+    const districtOptions = await screen.findAllByText('东城区');
+    expect(districtOptions.length).toBeGreaterThanOrEqual(2);
     await act(async () => {
-      fireEvent.click(screen.getByText('东城区'));
+      fireEvent.click(districtOptions[1]);
     });
 
     // 提交表单