Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MM-103: Home component form which gets user's name #8

Merged
merged 2 commits into from
Jul 31, 2024
Merged
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 src/Home/Home.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { fireEvent, render, screen } from "@testing-library/react";
import Home from "./Home";
import "@testing-library/jest-dom";

test("user's name is displayed on form submission", () => {
render(<Home />);

const name = "Luigi";
const textArea = screen.getByLabelText(/Enter name:/);
fireEvent.change(textArea, { target: { value: name }});

const submitButton = screen.getByRole("button");
fireEvent.click(submitButton);

const welcomeMessage = screen.getByText(/Welcome Luigi!/i);
expect(welcomeMessage).toBeInTheDocument();
})
21 changes: 21 additions & 0 deletions src/Home/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
import { useState } from "react";

function Home() {

const [username, setUsername] = useState("");
const [submitStatus, setSubmitStatus] = useState(false);

const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
setSubmitStatus(true);
}

return (
<>
<form onSubmit={handleSubmit}>
<label>Enter name:
<input type="text"
value={username}
onChange={username => setUsername(username.target.value)}
/>
</label>
<button type="submit">Submit</button>
{submitStatus ? <p>Welcome {username}!</p> : null }
</form>
</>
)
}
Expand Down
Loading