Build App: the like button under a tea on the shop's page. It takes one prop, likes, the number of likes the tea already has (0 when it isn't given), and shows the count under the button, like 12 likes.
The button reads Like. Clicking it likes the tea: the count goes up by one and the button is pressed, aria-pressed="true". Clicking it again takes your like back: the count goes back down and aria-pressed is "false". You can only like a tea once, however many times you click.
The word after the count follows the number: 1 like, but 0 likes and 2 likes.
The button's name stays Like whether it's pressed or not. aria-pressed already tells a screen reader that it's on, so changing the name as well would make it sound like a different button.
If the page passes a new likes (someone else liked the tea), the count shows it, plus one if you've liked it.
<App likes={12} />
// renders, before you click:
<button aria-pressed="false">Like</button>
<p>12 likes</p>
render(<App likes={12} />);
expect(screen.getByText("12 likes")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Like" })).toHaveAttribute("aria-pressed", "false");
render(<App />);
expect(screen.getByText("0 likes")).toBeInTheDocument();
render(<App likes={12} />);
await user.click(screen.getByRole("button", { name: "Like" }));
expect(screen.getByText("13 likes")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Like" })).toHaveAttribute("aria-pressed", "true");
render(<App likes={12} />);
const button = screen.getByRole("button", { name: "Like" });
await user.click(button);
await user.click(button);
expect(screen.getByText("12 likes")).toBeInTheDocument();
expect(button).toHaveAttribute("aria-pressed", "false");
render(<App likes={12} />);
const button = screen.getByRole("button", { name: "Like" });
await user.click(button);
await user.click(button);
await user.click(button);
expect(screen.getByText("13 likes")).toBeInTheDocument();
expect(button).toHaveAttribute("aria-pressed", "true");
render(<App likes={0} />);
await user.click(screen.getByRole("button", { name: "Like" }));
expect(screen.getByText("1 like")).toBeInTheDocument();
render(<App likes={12} />);
await user.tab();
expect(screen.getByRole("button", { name: "Like" })).toHaveFocus();
await user.keyboard("{Enter}");
expect(screen.getByText("13 likes")).toBeInTheDocument();
await user.keyboard(" ");
expect(screen.getByText("12 likes")).toBeInTheDocument();
const { rerender } = render(<App likes={12} />);
await user.click(screen.getByRole("button", { name: "Like" }));
rerender(<App likes={20} />);
expect(screen.getByText("21 likes")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Like" })).toHaveAttribute("aria-pressed", "true");
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 Like button. 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.