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
Binary file added ChatGPT_Assistance/ChatGPT_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ChatGPT_Assistance/ChatGPT_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ChatGPT_Assistance/ChatGPT_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ChatGPT_Assistance/ChatGPT_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,33 @@ This project uses Cypress for end-to-end testing. To run the tests:
## License

This project is for testing purposes only and is not licensed for public use.

---

## Developer Notes

**Name**: Eman Abdeen
**Challenge**: Junior Developer Challenge - Login Redirect Fix

### Changes Implemented:
1. **LoginForm.js**:
```javascript
// Added form submission handler
const handleSubmit = (e) => {
e.preventDefault();
if (onLogin) onLogin(formData);
navigate('/welcome');
};
```

2. **App.js**:
```javascript
// Wrapped app with Router
<Router>
<Routes>
{/* ... routes ... */}
</Routes>
</Router>
```

**Testing**: All Cypress tests now pass (redirect verification)
Binary file added cypress/downloads/downloads.htm
Binary file not shown.
46 changes: 46 additions & 0 deletions cypress/e2e/login.cy.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,48 @@
describe('Login Component', () => {

beforeEach(() => {
cy.visit('http://localhost:3000');

// Handle uncaught exceptions once for all tests
cy.once('uncaught:exception', () => false);
});

it('should have correct page title and heading', () => {
cy.title().should('eq', 'React App');
cy.get('h2').should('contain', 'Login');
});

it('should have correct login form structure', () => {
// Check form exists
cy.get('form.login-form').should('exist');

// Check name field
cy.get('#name').should('have.attr', 'type', 'text').and('have.attr', 'required');

// Check password field
cy.get('#password').should('have.attr', 'type', 'password').and('have.attr', 'required');

// Check submit button
cy.get('.login-button').should('contain', 'Login');

});

it('should allow login and redirect', () => {
// Fill and submit form
cy.get('#name').type('testuser');
cy.get('#password').type('password123');
cy.get('.login-button').click();

// Check redirect
cy.url().should('include', '/welcome');
cy.contains('Welcome, testuser!');

// Logout
cy.get('.logout-button').click();

// Verify back to login
cy.url().should('not.include', '/welcome');
cy.get('.login-form').should('exist');
});

})
1,778 changes: 1,517 additions & 261 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"@testing-library/user-event": "^13.5.0",
"react": "^19.1.0",
"react-dom": "^19.1.0",
"react-scripts": "5.0.1",
"react-router-dom": "^7.6.0",
"react-scripts": "^5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
Expand Down
35 changes: 28 additions & 7 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState } from 'react';
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
import './App.css';
import LoginForm from './components/LoginForm';
import Welcome from './components/Welcome';
Expand All @@ -19,13 +20,33 @@ function App() {
};

return (
<div className="App">
{isLoggedIn ? (
<Welcome userName={userName} onLogout={handleLogout} />
) : (
<LoginForm onLogin={handleLogin} />
)}
</div>
<Router>
<div className="App">
<Routes>
<Route
path="/"
element={
!isLoggedIn ? (
<LoginForm onLogin={handleLogin} />
) : (
<Welcome userName={userName} onLogout={handleLogout} />
)
}
/>
<Route
path="/welcome"
element={
isLoggedIn ? (
<Welcome userName={userName} onLogout={handleLogout} />
) : (
<Navigate to="/" replace />
)
}
/>
</Routes>
</div>
</Router>

);
}

Expand Down
11 changes: 10 additions & 1 deletion src/components/LoginForm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import './LoginForm.css';

function LoginForm({ onLogin }) {
Expand All @@ -7,6 +8,8 @@ function LoginForm({ onLogin }) {
password: ''
});

const navigate = useNavigate();

const handleChange = (e) => {
const { name, value } = e.target;
setFormData(prevState => ({
Expand All @@ -15,9 +18,15 @@ function LoginForm({ onLogin }) {
}));
};

const handleSubmit = (e) => {
e.preventDefault();
if (onLogin) onLogin(formData);
navigate('/welcome'); // Redirect to welcome page
};

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