2
0

async-select-test.spec.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { test, expect } from '../../utils/test-setup';
  2. import { readFileSync } from 'fs';
  3. import { join, dirname } from 'path';
  4. import { fileURLToPath } from 'url';
  5. import { selectRadixOptionAsync } from '@d8d/e2e-test-utils';
  6. const __filename = fileURLToPath(import.meta.url);
  7. const __dirname = dirname(__filename);
  8. const testUsers = JSON.parse(readFileSync(join(__dirname, '../../fixtures/test-users.json'), 'utf-8'));
  9. /**
  10. * 专门测试异步 Select 功能的简单测试
  11. * 用于验证 Story 2.3 的 selectRadixOptionAsync 实现
  12. */
  13. test.describe.serial('异步 Select 测试 (Story 2.3)', () => {
  14. test.beforeEach(async ({ adminLoginPage, disabilityPersonPage }) => {
  15. // 以管理员身份登录后台
  16. await adminLoginPage.goto();
  17. await adminLoginPage.login(testUsers.admin.username, testUsers.admin.password);
  18. await adminLoginPage.expectLoginSuccess();
  19. await disabilityPersonPage.goto();
  20. });
  21. test('测试省份选择(异步加载)', async ({ disabilityPersonPage, page }) => {
  22. console.log('=== 开始测试省份选择 ===');
  23. // 点击新增按钮打开对话框
  24. await disabilityPersonPage.openCreateDialog();
  25. console.log('对话框已打开');
  26. // 使用 selectRadixOptionAsync 选择省份
  27. await selectRadixOptionAsync(page, '省份 *', '广东省');
  28. console.log('✓ 省份选择成功');
  29. // 验证城市选择器已启用(省份选择后城市应该可用了)
  30. const cityCombobox = page.getByRole('combobox', { name: '城市' });
  31. await expect(cityCombobox).not.toBeDisabled();
  32. console.log('✓ 城市选择器已启用');
  33. });
  34. test('测试城市选择(异步加载,依赖省份)', async ({ disabilityPersonPage, page }) => {
  35. console.log('=== 开始测试城市选择 ===');
  36. // 点击新增按钮打开对话框
  37. await disabilityPersonPage.openCreateDialog();
  38. // 先选择省份
  39. await selectRadixOptionAsync(page, '省份 *', '湖北省');
  40. console.log('✓ 省份选择成功');
  41. // 选择城市 - selectRadixOptionAsync 自动处理异步加载,无需 waitForTimeout hack
  42. await selectRadixOptionAsync(page, '城市', '武汉市');
  43. console.log('✓ 城市选择成功');
  44. });
  45. test('测试完整的省份+城市选择流程', async ({ disabilityPersonPage, page }) => {
  46. console.log('=== 开始完整流程测试 ===');
  47. // 点击新增按钮
  48. await disabilityPersonPage.openCreateDialog();
  49. // 填写一些基本信息以到达地址选择区域
  50. await page.getByLabel('姓名 *').fill('测试用户');
  51. await page.getByLabel('性别 *').selectOption('男');
  52. await page.getByLabel('身份证号 *').fill('110101199001011234');
  53. await page.getByLabel('残疾证号 *').fill('CJZ20240001');
  54. await page.getByLabel('联系电话 *').fill('13800138000');
  55. await page.getByLabel('身份证地址 *').fill('测试地址');
  56. console.log('✓ 基本信息已填写');
  57. // 测试省份选择(异步)
  58. const startTime1 = Date.now();
  59. await selectRadixOptionAsync(page, '省份 *', '广东省');
  60. const duration1 = Date.now() - startTime1;
  61. console.log(`✓ 省份选择成功,耗时: ${duration1}ms`);
  62. // 测试城市选择(异步,依赖省份)
  63. const startTime2 = Date.now();
  64. await selectRadixOptionAsync(page, '城市', '深圳市');
  65. const duration2 = Date.now() - startTime2;
  66. console.log(`✓ 城市选择成功,耗时: ${duration2}ms`);
  67. // 验证选择器性能(应该在合理时间内完成)
  68. // 根据 Dev Notes 规范:最大可接受时间为 5 秒
  69. expect(duration1).toBeLessThan(5000); // 省份选择应在 5 秒内完成
  70. expect(duration2).toBeLessThan(5000); // 城市选择应在 5 秒内完成
  71. console.log('✓ 性能验证通过');
  72. });
  73. test.describe('边界场景测试', () => {
  74. test('测试超时场景 - 无效的省份值', async ({ disabilityPersonPage, page }) => {
  75. console.log('=== 测试超时场景 ===');
  76. await disabilityPersonPage.openCreateDialog();
  77. // 尝试选择不存在的省份(应该触发超时或错误)
  78. let errorThrown = false;
  79. try {
  80. await selectRadixOptionAsync(page, '省份 *', '不存在的省份XYZ', { timeout: 3000 });
  81. } catch (error: any) {
  82. errorThrown = true;
  83. console.log(`✓ 正确抛出错误: ${error.message}`);
  84. expect(error.message).toContain('selectRadixOptionAsync');
  85. }
  86. expect(errorThrown).toBe(true);
  87. });
  88. test('测试重复选择相同的省份', async ({ disabilityPersonPage, page }) => {
  89. console.log('=== 测试重复选择 ===');
  90. await disabilityPersonPage.openCreateDialog();
  91. // 第一次选择
  92. await selectRadixOptionAsync(page, '省份 *', '广东省');
  93. console.log('✓ 第一次选择成功');
  94. // 重复选择相同省份(应该正常工作)
  95. await selectRadixOptionAsync(page, '省份 *', '广东省');
  96. console.log('✓ 重复选择成功');
  97. });
  98. test('测试城市选择依赖 - 省份变化后城市可用性', async ({ disabilityPersonPage, page }) => {
  99. console.log('=== 测试省份变化场景 ===');
  100. await disabilityPersonPage.openCreateDialog();
  101. // 选择第一个省份
  102. await selectRadixOptionAsync(page, '省份 *', '广东省');
  103. await selectRadixOptionAsync(page, '城市', '广州市');
  104. console.log('✓ 广东省-广州市 选择成功');
  105. // 更换省份
  106. await selectRadixOptionAsync(page, '省份 *', '湖北省');
  107. await selectRadixOptionAsync(page, '城市', '武汉市');
  108. console.log('✓ 湖北省-武汉市 选择成功');
  109. });
  110. });
  111. });