Browse Source

fix(login): 添加了登录失败场景的测试代码,并调整实现代码以确保测试能正确验证预期错误提示,使登录失败的错误信息展示和测试验证逻辑保持一致。

dengdx 3 days ago
parent
commit
e261f087bd
2 changed files with 26 additions and 2 deletions
  1. 22 0
      __e2e_test__/login.spec.ts
  2. 4 2
      src/pages/security/Login.tsx

+ 22 - 0
__e2e_test__/login.spec.ts

@@ -20,4 +20,26 @@ describe('Login Page', () => {
     // Wait for the login process to complete
     // Wait for the login process to complete
     cy.contains('登录成功').should('be.visible', { timeout: 10000 });
     cy.contains('登录成功').should('be.visible', { timeout: 10000 });
   });
   });
+
+  it('should show an error message for incorrect credentials', () => {
+    // Visit the base URL
+    cy.visit('/');
+
+    // Wait for the page to load and ensure the elements are present
+    cy.get('[data-testid="login-username-input"]').should('be.visible');
+    cy.get('[data-testid="login-password-input"]').should('be.visible');
+    cy.get('[data-testid="login-submit-button"]').should('be.visible');
+
+    // Enter the incorrect username
+    cy.get('[data-testid="login-username-input"]').type('wronguser');
+
+    // Enter the incorrect password
+    cy.get('[data-testid="login-password-input"]').type('wrongpassword');
+
+    // Click the login button
+    cy.get('[data-testid="login-submit-button"]').click();
+
+    // Wait for the error message to be visible
+    cy.contains('登录失败').should('be.visible', { timeout: 10000 });
+  });
 });
 });

+ 4 - 2
src/pages/security/Login.tsx

@@ -55,10 +55,12 @@ const Login: React.FC = () => {
         dispatch(setSystemMode(SystemMode.Normal));
         dispatch(setSystemMode(SystemMode.Normal));
         message.success('登录成功'); //todo 更详细的提示与异步过程
         message.success('登录成功'); //todo 更详细的提示与异步过程
       } else {
       } else {
-        message.error(result.description || '登录失败');
+        message.error(`登录失败,详情:${result.description}`);
       }
       }
     } catch (e) {
     } catch (e) {
-      message.error(`网络错误: ${e instanceof Error ? e.message : '未知错误'}`);
+      message.error(
+        `登录失败,网络错误: ${e instanceof Error ? e.message : '未知错误'}`
+      );
     }
     }
   };
   };