Build App: the password part of the tea shop's sign-in form. It has a password box labelled Password, a button next to it that shows or hides what you've typed, and a Sign in button that submits the form.
The password starts hidden (type="password") and the toggle reads Show password. Clicking the toggle shows the password (type="text") and the toggle reads Hide password. Clicking it again hides it. What you've typed stays in the box either way.
The toggle is inside the form, so make sure clicking it doesn't submit the form. Submitting, by clicking Sign in or pressing Enter in the box, calls the onSignIn prop with the password, shown or hidden. onSignIn may be missing (the Preview doesn't pass it), so don't fail without it.
The tab order is the box, then the toggle, then Sign in. Pressing the toggle from the keyboard leaves the focus on it, so you can press it again to hide the password.
<App onSignIn={(password) => signIn(password)} />
// renders, before you click:
<form>
<label>Password <input type="password" /></label>
<button type="button">Show password</button>
<button type="submit">Sign in</button>
</form>
render(<App />);
expect(screen.getByLabelText("Password")).toHaveAttribute("type", "password");
expect(screen.getByRole("button", { name: "Show password" })).toBeInTheDocument();
render(<App />);
await user.type(screen.getByLabelText("Password"), "oolong42");
await user.click(screen.getByRole("button", { name: "Show password" }));
expect(screen.getByLabelText("Password")).toHaveAttribute("type", "text");
expect(screen.getByRole("button", { name: "Hide password" })).toBeInTheDocument();
render(<App />);
await user.click(screen.getByRole("button", { name: "Show password" }));
await user.click(screen.getByRole("button", { name: "Hide password" }));
expect(screen.getByLabelText("Password")).toHaveAttribute("type", "password");
expect(screen.getByRole("button", { name: "Show password" })).toBeInTheDocument();
render(<App />);
await user.type(screen.getByLabelText("Password"), "oolong42");
await user.click(screen.getByRole("button", { name: "Show password" }));
expect(screen.getByLabelText("Password")).toHaveValue("oolong42");
await user.type(screen.getByLabelText("Password"), "!");
await user.click(screen.getByRole("button", { name: "Hide password" }));
expect(screen.getByLabelText("Password")).toHaveValue("oolong42!");
const onSignIn = spy();
render(<App onSignIn={onSignIn} />);
await user.type(screen.getByLabelText("Password"), "oolong42");
await user.click(screen.getByRole("button", { name: "Show password" }));
expect(onSignIn).not.toHaveBeenCalled();
const onSignIn = spy();
render(<App onSignIn={onSignIn} />);
await user.type(screen.getByLabelText("Password"), "oolong42");
await user.click(screen.getByRole("button", { name: "Show password" }));
await user.click(screen.getByRole("button", { name: "Sign in" }));
expect(onSignIn).toHaveBeenCalledTimes(1);
expect(onSignIn).toHaveBeenCalledWith("oolong42");
const onSignIn = spy();
render(<App onSignIn={onSignIn} />);
await user.type(screen.getByLabelText("Password"), "oolong42{Enter}");
expect(onSignIn).toHaveBeenCalledWith("oolong42");
render(<App />);
await user.tab();
expect(screen.getByLabelText("Password")).toHaveFocus();
await user.tab();
expect(screen.getByRole("button", { name: "Show password" })).toHaveFocus();
await user.tab();
expect(screen.getByRole("button", { name: "Sign in" })).toHaveFocus();
render(<App />);
await user.tab();
await user.tab();
await user.keyboard("{Enter}");
expect(screen.getByLabelText("Password")).toHaveAttribute("type", "text");
expect(screen.getByRole("button", { name: "Hide password" })).toHaveFocus();
await user.keyboard(" ");
expect(screen.getByLabelText("Password")).toHaveAttribute("type", "password");
expect(screen.getByRole("button", { name: "Show password" })).toHaveFocus();
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with Show password. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.