diff --git a/cypress/e2e/login.cy.js b/cypress/e2e/login.cy.js index c2fbb3a..97fc1c8 100644 --- a/cypress/e2e/login.cy.js +++ b/cypress/e2e/login.cy.js @@ -1,2 +1,47 @@ -describe('Login Component', () => { -}) \ No newline at end of file +// End to End Test for Login Flow +// Confirm the login form and its elements are present +// Confirm that it allows typing in name and password fields +// Confirm that a successful login leads to a redirect +// Confirm that a successfu logout leads to Login page +const userName = "Oluwafemi Oloyede"; +const password = "Password123"; + +describe("Login Flow", () => { + beforeEach(() => { + cy.visit("/"); + }); + + it("should render login form", () => { + cy.get("form.login-form").should("exist"); + cy.get("input[name='name']").should("exist"); + cy.get("input[name='password']").should("exist"); + cy.get("button.login-button").should("contain", "Login"); + }); + + it("should allow typing in name and password fields", () => { + cy.get("input[name='name']") + .type(`${userName}`) + .should("have.value", `${userName}`); + cy.get("input[name='password']") + .type(`${password}`) + .should("have.value", `${password}`); + }); + + it("should login successfully and redirect to welcome page", () => { + cy.login(userName, password); + + cy.contains(`Welcome, ${userName}!`).should("be.visible"); + cy.contains("You have successfully logged in.").should("be.visible"); + cy.get("button.logout-button").should("exist"); + }); + + it("should logout and return to login screen", () => { + cy.login(userName, password); + + cy.get("button.logout-button").click(); + + // Confirm we're back on the login screen + cy.get("form.login-form").should("exist"); + cy.contains("Login").should("exist"); + }); +}); diff --git a/cypress/support/commands.js b/cypress/support/commands.js index 9d3825e..c6eb6f5 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -1,25 +1,9 @@ -// *********************************************** -// This example commands.js shows you how to -// create various custom commands and overwrite -// existing commands. -// -// For more comprehensive examples of custom -// commands please read more here: -// https://on.cypress.io/custom-commands -// *********************************************** -// -// -// -- This is a parent command -- -// Cypress.Commands.add('login', (email, password) => { ... }) -// -// -// -- This is a child command -- -// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... }) -// -// -// -- This is a dual command -- -// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... }) -// -// -// -- This will overwrite an existing command -- -// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... }) \ No newline at end of file +// Login command +Cypress.Commands.add("login", (userName, password) => { + // Fill in the login form + cy.get("input[name='name']").type(userName); + cy.get("input[name='password']").type(password); + + // Click the login button + cy.get("button.login-button").click(); +}); \ No newline at end of file diff --git a/src/App.js b/src/App.js index c383c91..5b8275e 100644 --- a/src/App.js +++ b/src/App.js @@ -1,21 +1,26 @@ -import React, { useState } from 'react'; -import './App.css'; -import LoginForm from './components/LoginForm'; -import Welcome from './components/Welcome'; +import React, { useState } from "react"; +import "./App.css"; +import LoginForm from "./components/LoginForm"; +import Welcome from "./components/Welcome"; function App() { const [isLoggedIn, setIsLoggedIn] = useState(false); - const [userName, setUserName] = useState(''); + const [userName, setUserName] = useState(""); + // Handle Login and redirect to Welcome page const handleLogin = (formData) => { - // In a real app, you would validate credentials here - setIsLoggedIn(true); - setUserName(formData.name); + const { name, password } = formData; + + // Sanity check: To be sure the user fills the form + if (name.length && password.length) { + setIsLoggedIn(true); + setUserName(name); + } }; const handleLogout = () => { setIsLoggedIn(false); - setUserName(''); + setUserName(""); }; return ( diff --git a/src/components/LoginForm.js b/src/components/LoginForm.js index 26c8cc3..a7de286 100644 --- a/src/components/LoginForm.js +++ b/src/components/LoginForm.js @@ -1,17 +1,17 @@ -import React, { useState } from 'react'; -import './LoginForm.css'; +import React, { useState } from "react"; +import "./LoginForm.css"; function LoginForm({ onLogin }) { const [formData, setFormData] = useState({ - name: '', - password: '' + name: "", + password: "", }); const handleChange = (e) => { const { name, value } = e.target; - setFormData(prevState => ({ + setFormData((prevState) => ({ ...prevState, - [name]: value + [name]: value, })); }; @@ -41,7 +41,14 @@ function LoginForm({ onLogin }) { required /> - @@ -49,4 +56,4 @@ function LoginForm({ onLogin }) { ); } -export default LoginForm; \ No newline at end of file +export default LoginForm;