history-delete.cy.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * 测试文件: History - 删除功能
  3. *
  4. * 测试场景:
  5. * 1. 成功删除未锁定的记录
  6. * 2. 尝试删除锁定的记录(应显示警告)
  7. * 3. 删除API失败的情况(网络错误/服务器错误)
  8. * 4. 取消删除操作
  9. */
  10. import { mockLoginSuccess } from '../../support/mock/handlers/user';
  11. import {
  12. mockFetchHistoryDataWithUnlocked,
  13. mockFetchHistoryDataWithLocked,
  14. mockDeleteStudySuccess,
  15. mockDeleteStudyFailure,
  16. DeleteStudy,
  17. FetchHistoryDataWithUnlocked,
  18. FetchHistoryDataWithLocked,
  19. } from '../../support/mock/handlers/worklist';
  20. import {
  21. mockI18nSuccess,
  22. mockGetLanguageListSuccess,
  23. mockAllRequiredAPIs,
  24. } from '../../support/mock/handlers/i18n';
  25. import { mockGetQuotaSuccess } from '../../support/mock/handlers/quota';
  26. import LoginPage from '../../support/pageObjects/LoginPage';
  27. import MainPage from '../../support/pageObjects/MainPage';
  28. import HistoryPage from '../../support/pageObjects/HistoryPage';
  29. describe('History - 删除功能测试', () => {
  30. const loginPage = new LoginPage();
  31. const mainPage = new MainPage();
  32. const historyPage = new HistoryPage();
  33. beforeEach(() => {
  34. // Mock多语言资源和必要的API
  35. mockI18nSuccess('zh_CN');
  36. mockGetLanguageListSuccess();
  37. mockAllRequiredAPIs('zh_CN');
  38. // Mock登录成功响应
  39. mockLoginSuccess();
  40. // Mock配额成功
  41. mockGetQuotaSuccess();
  42. // 登录系统
  43. loginPage.visit();
  44. loginPage.login('admin', '123456');
  45. // 等待页面渲染和路由跳转
  46. cy.wait(1500);
  47. // 验证登录成功:登录页面元素不再存在
  48. loginPage.getUsernameInput().should('not.exist');
  49. loginPage.getPasswordInput().should('not.exist');
  50. loginPage.getSubmitButton().should('not.exist');
  51. // ⚠️ 不在这里导航,让每个测试用例自己设置mock并导航
  52. });
  53. it('应该成功删除未锁定的记录', () => {
  54. // 设置History数据mock(未锁定)
  55. mockFetchHistoryDataWithUnlocked();
  56. mockDeleteStudySuccess();
  57. // 导航到History列表
  58. mainPage.clickHistorylistButton();
  59. cy.wait(FetchHistoryDataWithUnlocked);
  60. // 验证初始状态:2条记录
  61. historyPage.verifyRowCount(2);
  62. // 调试:点击前查看Redux state
  63. cy.window().its('store').invoke('getState').then(state => {
  64. cy.log('Before click - selectedIds:', state.historySelection?.selectedIds || []);
  65. });
  66. // 点击第一行(未锁定)选中
  67. historyPage.clickRowByIndex(0);
  68. // 等待React更新state
  69. cy.wait(500);
  70. // 调试:点击后查看Redux state
  71. cy.window().its('store').invoke('getState').then(state => {
  72. cy.log('After click - selectedIds:', state.historySelection?.selectedIds || []);
  73. });
  74. historyPage.verifyRowSelected(0);
  75. // 点击删除按钮
  76. historyPage.clickDeleteButton();
  77. // 等待 提示框出现
  78. cy.wait(1500);
  79. // 验证确认对话框出现
  80. historyPage.getDeleteConfirmModal();
  81. cy.contains('确认删除').should('be.visible');
  82. // 确认删除
  83. historyPage.confirmDeleteInModal();
  84. // 等待删除API调用
  85. cy.wait(DeleteStudy).then((interception) => {
  86. expect(interception.response?.statusCode).to.eq(200);
  87. expect(interception.response?.body.code).to.eq('0x000000');
  88. });
  89. // 验证删除后只剩1条记录(本地删除,无需等待列表刷新)
  90. historyPage.verifyRowCount(1);
  91. });
  92. it('尝试删除锁定的记录时应显示警告', () => {
  93. // 设置History数据mock(已锁定)
  94. mockFetchHistoryDataWithLocked();
  95. // 导航到History列表
  96. mainPage.clickHistorylistButton();
  97. cy.wait(FetchHistoryDataWithLocked);
  98. // 验证有2条记录
  99. historyPage.verifyRowCount(2);
  100. // 点击第一行(已锁定)选中
  101. historyPage.clickRowByIndex(0);
  102. historyPage.verifyRowSelected(0);
  103. // 点击删除按钮
  104. historyPage.clickDeleteButton();
  105. // 验证警告消息:锁定状态不可删除
  106. historyPage.verifyDeleteWarningMessage('锁定状态不可删除');
  107. // 验证没有弹出确认对话框(因为被提前拦截了)
  108. historyPage.verifyModalNotExist();
  109. // 验证记录仍然存在
  110. historyPage.verifyRowCount(2);
  111. });
  112. it('删除API失败时应显示错误提示', () => {
  113. // 设置History数据mock和失败的删除API
  114. mockFetchHistoryDataWithUnlocked();
  115. mockDeleteStudyFailure();
  116. // 导航到History列表
  117. mainPage.clickHistorylistButton();
  118. cy.wait(FetchHistoryDataWithUnlocked);
  119. // 验证有2条记录
  120. historyPage.verifyRowCount(2);
  121. // 点击第一行选中
  122. historyPage.clickRowByIndex(0);
  123. historyPage.verifyRowSelected(0);
  124. // 点击删除按钮
  125. historyPage.clickDeleteButton();
  126. // 验证确认对话框出现
  127. historyPage.getDeleteConfirmModal();
  128. // 确认删除
  129. historyPage.confirmDeleteInModal();
  130. // 等待删除API调用(失败)
  131. cy.wait(DeleteStudy).then((interception) => {
  132. expect(interception.response?.statusCode).to.eq(200);
  133. expect(interception.response?.body.code).to.not.eq('0x000000');
  134. });
  135. // 验证记录仍然存在(因为删除失败)
  136. historyPage.verifyRowCount(2);
  137. });
  138. it('取消删除操作时记录应保持不变', () => {
  139. // 设置History数据mock
  140. mockFetchHistoryDataWithUnlocked();
  141. // 导航到History列表
  142. mainPage.clickHistorylistButton();
  143. cy.wait(FetchHistoryDataWithUnlocked);
  144. // 验证有2条记录
  145. historyPage.verifyRowCount(2);
  146. // 点击第一行选中
  147. historyPage.clickRowByIndex(0);
  148. historyPage.verifyRowSelected(0);
  149. // 点击删除按钮
  150. historyPage.clickDeleteButton();
  151. // 验证确认对话框出现
  152. historyPage.getDeleteConfirmModal();
  153. // 取消删除
  154. historyPage.cancelDeleteInModal();
  155. // 验证对话框关闭
  156. historyPage.verifyModalNotExist();
  157. // 验证记录仍然存在
  158. historyPage.verifyRowCount(2);
  159. });
  160. it('未选中任何记录时点击删除应显示警告', () => {
  161. // 设置History数据mock
  162. mockFetchHistoryDataWithUnlocked();
  163. // 导航到History列表
  164. mainPage.clickHistorylistButton();
  165. cy.wait(FetchHistoryDataWithUnlocked);
  166. // 验证有2条记录
  167. historyPage.verifyRowCount(2);
  168. // 不选中任何记录,直接点击删除按钮
  169. historyPage.clickDeleteButton();
  170. // 验证警告消息:请先选择要删除的项目
  171. historyPage.verifyDeleteWarningMessage('请先选择要删除的项目');
  172. // 验证没有弹出确认对话框
  173. historyPage.verifyModalNotExist();
  174. // 验证记录仍然存在
  175. historyPage.verifyRowCount(2);
  176. });
  177. });