Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion cypress/e2e/login.cy.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
describe('Login Component', () => {
})
beforeEach(() => {
cy.visit('/');
});

it('displays the login form', () => {
cy.get('h2').should('contain', 'Login');
cy.get('input[name="name"]').should('exist');
cy.get('input[name="password"]').should('exist');
cy.get('button').contains('Login');
});

it('logs in successfully and redirects to welcome screen', () => {
cy.get('input[name="name"]').type('TestUser');
cy.get('input[name="password"]').type('password123');
cy.get('button').contains('Login').click();

cy.contains('Welcome, TestUser!').should('exist');
cy.get('button').contains('Logout').should('exist');
});

it('can logout and go back to login screen', () => {
cy.get('input[name="name"]').type('TestUser');
cy.get('input[name="password"]').type('password123');
cy.get('button').contains('Login').click();

cy.get('button').contains('Logout').click();
cy.contains('Login').should('exist');
});
Comment on lines +22 to +29

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Good coverage of logout functionality.

This test properly validates the complete login-logout flow.

There's duplication in the login steps between this test and the previous one. Applying the custom command suggestion would simplify this:

it('can logout and go back to login screen', () => {
-  cy.get('input[name="name"]').type('TestUser');
-  cy.get('input[name="password"]').type('password123');
-  cy.get('button').contains('Login').click();
+  cy.login('TestUser', 'password123');

  cy.get('button').contains('Logout').click();
  cy.contains('Login').should('exist');
});

Additionally, consider adding tests for negative scenarios such as invalid credentials or empty form submission.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it('can logout and go back to login screen', () => {
cy.get('input[name="name"]').type('TestUser');
cy.get('input[name="password"]').type('password123');
cy.get('button').contains('Login').click();
cy.get('button').contains('Logout').click();
cy.contains('Login').should('exist');
});
it('can logout and go back to login screen', () => {
cy.login('TestUser', 'password123');
cy.get('button').contains('Logout').click();
cy.contains('Login').should('exist');
});
🤖 Prompt for AI Agents
In cypress/e2e/login.cy.js around lines 22 to 29, the login steps are duplicated
from the previous test. Refactor by creating a custom Cypress command for the
login process and replace the repeated login steps with this command to simplify
the test. Additionally, add new tests to cover negative scenarios like invalid
credentials and empty form submissions to improve test coverage.

});
5 changes: 4 additions & 1 deletion src/components/LoginForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ function LoginForm({ onLogin }) {

return (
<div className="login-form-container">
<form className="login-form">
<form className="login-form" onSubmit={(e) => {
e.preventDefault(); // prevent default form reload
onLogin(formData); // call parent handler
}}>
<h2>Login</h2>
<div className="form-group">
<label htmlFor="name">Name:</label>
Expand Down