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
7 changes: 5 additions & 2 deletions cypress/e2e/login.cy.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
describe('Login Component', () => {
})
describe('Login Flow with Component Switch', () => {
it('logs in and shows welcome message with username', () => {
cy.login('layoolar','password');
});
});
33 changes: 8 additions & 25 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
// ***********************************************
// 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) => { ... })
Cypress.Commands.add('login', (name = 'testuser', password = 'anything') => {
cy.visit('http://localhost:3000');
cy.get('[data-testid="input-name"]').type(name);
cy.get('[data-testid="input-password"]').type(password);
cy.get('[data-testid="login-button"]').click();
cy.get('[data-testid="login-form"]').should('not.exist');
cy.contains(`Welcome, ${name}`).should('exist');
});
11 changes: 9 additions & 2 deletions src/components/LoginForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ function LoginForm({ onLogin }) {
}));
};

const handleSubmit = (e) => {
e.preventDefault();
onLogin(formData); // or however you’re calling it
};

return (
<div className="login-form-container">
<form className="login-form">
<form className="login-form" data-testid="login-form" onSubmit={handleSubmit}>
<h2>Login</h2>
<div className="form-group">
<label htmlFor="name">Name:</label>
Expand All @@ -28,6 +33,7 @@ function LoginForm({ onLogin }) {
value={formData.name}
onChange={handleChange}
required
data-testid="input-name"
/>
</div>
<div className="form-group">
Expand All @@ -39,9 +45,10 @@ function LoginForm({ onLogin }) {
value={formData.password}
onChange={handleChange}
required
data-testid="input-password"
/>
</div>
<button type="submit" className="login-button">
<button type="submit" className="login-button" data-testid="login-button">
Login
</button>
</form>
Expand Down