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
17 changes: 17 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
env: {
browser: true,
node: true,
'cypress/globals': true,
},
plugins: ['cypress'],
rules: {
'cypress/no-assigning-return-values': 'error', // Prevents assigning return values from Cypress commands
'cypress/no-unnecessary-waiting': 'warn', // Warns against using cy.wait() unnecessarily
'cypress/assertion-before-screenshot': 'error', // Ensures assertions are made before taking screenshots
'cypress/no-async-tests': 'error', // Prevents using async/await in Cypress tests
},
extends: [
'plugin:cypress/recommended'
],
};
27 changes: 26 additions & 1 deletion cypress/e2e/login.cy.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
/// <reference types="cypress" />
describe('Login Component', () => {
})
beforeEach(() => {
cy.visit('/'); // Adjust if needed (e.g. localhost:3000 or specific route)
});

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

it('shows error message if fields are empty', () => {
cy.get('button[type="submit"]').click();
cy.get('.error-message').should('contain', 'Name and Password cannot be empty');
});

it('logs in successfully and shows welcome message', () => {
cy.get('input[name="name"]').type('Habeeb');
cy.get('input[name="password"]').type('supersecret');
cy.get('button[type="submit"]').click();

cy.contains('Welcome, Habeeb!'); // Assuming Welcome component renders this
cy.get('button').contains('Logout').should('exist'); // Logout button from Welcome
});
});
24 changes: 23 additions & 1 deletion src/components/LoginForm.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
.form-group label {
display: block;
margin-bottom: 0.5rem;
text-align: start;
color: #555;
}

Expand Down Expand Up @@ -64,4 +65,25 @@
.login-button:focus {
outline: none;
box-shadow: 0 0 0 2px rgba(0, 102, 204, 0.2);
}
}

.error-container {
color: white;
background: #f00;
font-size: 0.875rem;
margin-top: 0.5rem;
padding: .8rem;
padding-block: .5rem;
display: flex;
justify-content: space-between;
margin-bottom: 1rem;
align-items: center;
}

.error-container button {
background: none;
outline: none;
border: none;
cursor: pointer;
color: white;
}
23 changes: 20 additions & 3 deletions src/components/LoginForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ function LoginForm({ onLogin }) {
name: '',
password: ''
});
const [error, setError] = useState(null);

const handleSubmit = (e) => {
e.preventDefault();

if (formData.name.trim() === "" || formData.password.trim() === "") {
setError("Name and Password cannot be empty");
return;
}


onLogin(formData);
}

const handleChange = (e) => {
const { name, value } = e.target;
Expand All @@ -17,8 +30,14 @@ function LoginForm({ onLogin }) {

return (
<div className="login-form-container">
<form className="login-form">
<form className="login-form" onSubmit={handleSubmit}>
<h2>Login</h2>
{error && (
<div className='error-container'>
<p className='error-message'>{error}</p>
<button className='error-button' onClick={() => setError(null)}>X</button>
</div>
)}
<div className="form-group">
<label htmlFor="name">Name:</label>
<input
Expand All @@ -27,7 +46,6 @@ function LoginForm({ onLogin }) {
name="name"
value={formData.name}
onChange={handleChange}
required
/>
</div>
<div className="form-group">
Expand All @@ -38,7 +56,6 @@ function LoginForm({ onLogin }) {
name="password"
value={formData.password}
onChange={handleChange}
required
/>
</div>
<button type="submit" className="login-button">
Expand Down
8 changes: 7 additions & 1 deletion src/index.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}

body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
Expand All @@ -10,4 +16,4 @@ body {
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
}