diff --git a/cypress/e2e/login.cy.js b/cypress/e2e/login.cy.js index c2fbb3a..30f728f 100644 --- a/cypress/e2e/login.cy.js +++ b/cypress/e2e/login.cy.js @@ -1,2 +1,29 @@ +// cypress/e2e/login.cy.js + describe('Login Component', () => { -}) \ No newline at end of file + const username = 'Jane Doe'; + const password = 'supersecret'; + + beforeEach(() => { + // assumes your dev server is at localhost:3000 + cy.visit('/'); + }); + + it('renders the login form', () => { + cy.contains('h2', 'Login'); + cy.get('input[name="name"]').should('be.visible'); + cy.get('input[name="password"]').should('be.visible'); + cy.get('button[type="submit"]').contains('Login'); + }); + + it('allows a user to log in and shows the welcome screen', () => { + // fill out and submit + cy.get('input[name="name"]').type(username); + cy.get('input[name="password"]').type(password); + cy.get('button[type="submit"]').click(); + + // should render Welcome component + cy.contains(`Welcome, ${username}!`).should('be.visible'); + cy.get('button').contains('Logout').should('be.visible'); + }); +}); diff --git a/src/components/LoginForm.js b/src/components/LoginForm.js index 26c8cc3..2d903a2 100644 --- a/src/components/LoginForm.js +++ b/src/components/LoginForm.js @@ -2,51 +2,43 @@ import React, { useState } from 'react'; import './LoginForm.css'; function LoginForm({ onLogin }) { - const [formData, setFormData] = useState({ - name: '', - password: '' - }); + const [formData, setFormData] = useState({ name: '', password: '' }); const handleChange = (e) => { const { name, value } = e.target; - setFormData(prevState => ({ - ...prevState, - [name]: value - })); + setFormData(prev => ({ ...prev, [name]: value })); + }; + + const handleSubmit = (e) => { + e.preventDefault(); + onLogin(formData); }; return ( -