diff --git a/BUG.md b/BUG.md new file mode 100644 index 0000000..92423ef --- /dev/null +++ b/BUG.md @@ -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 +
+``` + +## 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 \ No newline at end of file diff --git a/README.md b/README.md index 20b4126..f96a412 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cypress/e2e/login.cy.js b/cypress/e2e/login.cy.js index c2fbb3a..82b2107 100644 --- a/cypress/e2e/login.cy.js +++ b/cypress/e2e/login.cy.js @@ -1,2 +1,30 @@ describe('Login Component', () => { -}) \ No newline at end of file + 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'); + }); +}); \ No newline at end of file diff --git a/src/components/LoginForm.js b/src/components/LoginForm.js index 26c8cc3..a51883e 100644 --- a/src/components/LoginForm.js +++ b/src/components/LoginForm.js @@ -15,9 +15,14 @@ function LoginForm({ onLogin }) { })); }; + const handleSubmit = (e) => { + e.preventDefault(); + onLogin(formData); + }; + return (
- +

Login