diff --git a/cypress/e2e/login.cy.js b/cypress/e2e/login.cy.js index c2fbb3a..9fc4f3c 100644 --- a/cypress/e2e/login.cy.js +++ b/cypress/e2e/login.cy.js @@ -1,2 +1,45 @@ describe('Login Component', () => { + beforeEach(() => { + cy.visit('/'); + }); + + it('should render name and password inputs and login button', () => { + cy.get('input[name="name"]').should('exist'); + cy.get('input[name="password"]').should('exist'); + cy.get('button[type="submit"]').should('exist'); + + + }); + it('prevents submission when input fields are empty', () => { + cy.get('button[type="submit"]').click(); + cy.get('input[name="name"]').then($input => { + expect($input[0].checkValidity()).to.be.false; + }); + + cy.get('input[name="password"]').then($input => { + expect($input[0].checkValidity()).to.be.false; + }); + + }); + + it('logs in successfully with valid credentials and redirects', () => { + cy.visit('/'); + + cy.get('input[name="name"]').type('suleiman'); + cy.get('input[name="password"]').type('suleimanpassword'); + cy.get('button[type="submit"]').click(); + + cy.contains('Welcome, suleiman!').should('exist'); + + + }); + + it('shows error or blocks login with invalid credentials', () => { + cy.get('input[name="name"]').type('wronguser'); + cy.get('input[name="password"]').type('wrongpass'); + cy.get('button[type="submit"]').click(); + + cy.contains('Welcome').should('not.exist'); + }); + }) \ No newline at end of file diff --git a/src/App.js b/src/App.js index c383c91..afc9bf1 100644 --- a/src/App.js +++ b/src/App.js @@ -9,8 +9,13 @@ function App() { const handleLogin = (formData) => { // In a real app, you would validate credentials here - setIsLoggedIn(true); - setUserName(formData.name); + if (formData.name === 'suleiman' && formData.password === 'suleimanpassword'){ + setIsLoggedIn(true); + setUserName(formData.name); + } else { + alert('You have provided invalid credentials') + } + }; const handleLogout = () => { diff --git a/src/components/LoginForm.js b/src/components/LoginForm.js index 26c8cc3..64e86df 100644 --- a/src/components/LoginForm.js +++ b/src/components/LoginForm.js @@ -15,9 +15,15 @@ function LoginForm({ onLogin }) { })); }; + const handleSubmit = (e) => { + e.preventDefault(); + onLogin(formData); + }; + + return (