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
29 changes: 29 additions & 0 deletions BUG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Bug Fix Documentation

## Issue Description
The login form wasn't properly handling form submission events. The form had a submit button but was missing the `onSubmit` handler to process the form data and call the `onLogin` prop.

## Solution
This was fixed by:
1. Adding a `handleSubmit` function that prevents the default form submission
2. Calling the `onLogin` prop with the form data
3. Adding the `onSubmit` handler to the form element

## Implementation Details
The fix was implemented in `LoginForm.js` by adding:
```javascript
const handleSubmit = (e) => {
e.preventDefault();
onLogin(formData);
};
```
And updating the form element to include the handler:
```javascript
<form className="login-form" onSubmit={handleSubmit}>
```

## Testing
The fix was verified using Cypress tests that confirm:
- The login form works as expected
- Successful login leads to proper redirection
- The welcome page displays the correct user information
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ This project uses Cypress for end-to-end testing. To run the tests:
└── package.json
```

## Bug Fix Explanation

The login form wasn't properly handling form submission events. The form had a submit button but was missing the `onSubmit` handler to process the form data and call the `onLogin` prop. This was fixed by adding a `handleSubmit` function that prevents the default form submission and calls `onLogin` with the form data, along with adding the `onSubmit` handler to the form element.

## Cypress Test Cases

The following test cases have been implemented to verify the login functionality:

1. **Login Form Display**
- Verifies that the login form is visible
- Checks for the presence of name input, password input, and submit button

2. **Successful Login and Redirect**
- Tests the complete login flow with valid credentials
- Verifies redirection to the welcome page
- Confirms the welcome message displays the correct username

3. **Logout Functionality**
- Verifies that the logout button is visible after login
- Ensures proper UI state after successful login

## Available Scripts

- `npm start` - Runs the app in development mode
Expand Down
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('should display login form', () => {
cy.get('.login-form').should('be.visible');
cy.get('input[name="name"]').should('exist');
cy.get('input[name="password"]').should('exist');
cy.get('button[type="submit"]').should('exist');
});

it('should successfully login and redirect to welcome page', () => {
cy.get('input[name="name"]').type('test1');
cy.get('input[name="password"]').type('password1');
cy.get('button[type="submit"]').click();

// Verify redirect to welcome page
cy.get('.welcome-container').should('be.visible');
cy.get('h1').should('contain', 'Welcome, test1!');
});

it('should show logout button after login', () => {
cy.get('input[name="name"]').type('test1');
cy.get('input[name="password"]').type('password1');
cy.get('button[type="submit"]').click();

cy.get('.logout-button').should('be.visible');
});
});
7 changes: 6 additions & 1 deletion src/components/LoginForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ function LoginForm({ onLogin }) {
}));
};

const handleSubmit = (e) => {
e.preventDefault();
onLogin(formData);
};

return (
<div className="login-form-container">
<form className="login-form">
<form className="login-form" onSubmit={handleSubmit}>
<h2>Login</h2>
<div className="form-group">
<label htmlFor="name">Name:</label>
Expand Down