Skip to content

Commit

Permalink
Implements form in Home component to get user's name, tests that user…
Browse files Browse the repository at this point in the history
…'s name is displayed on form submission
  • Loading branch information
natashabuckham committed Jul 31, 2024
1 parent 0722e84 commit 0c08787
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
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

0 comments on commit 0c08787

Please sign in to comment.