Build App: a stopwatch that shows the time in seconds with one decimal place, like 0.0 s and 1.5 s. It starts stopped, at 0.0 s.
A button reads Start while it's stopped and Stop while it runs. While it runs, the time goes up by a tenth of a second every 100 ms. Stop freezes the time, and Start carries on from there.
Reset stops the stopwatch and puts it back to 0.0 s.
Leave nothing running in the background: stopping, resetting, or removing the stopwatch from the page clears its interval, and it never runs two intervals at once.
<p>1.5 s</p>
<button>Stop</button>
<button>Reset</button>
render(<App />);
expect(screen.getByText("0.0 s")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Start" })).toBeInTheDocument();
const clock = useFakeTimers();
render(<App />);
await user.click(screen.getByRole("button", { name: "Start" }));
await clock.advance(100);
expect(screen.getByText("0.1 s")).toBeInTheDocument();
await clock.advance(1400);
expect(screen.getByText("1.5 s")).toBeInTheDocument();
useFakeTimers();
render(<App />);
await user.click(screen.getByRole("button", { name: "Start" }));
expect(screen.getByRole("button", { name: "Stop" })).toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Start" })).not.toBeInTheDocument();
const clock = useFakeTimers();
render(<App />);
await user.click(screen.getByRole("button", { name: "Start" }));
await clock.advance(1200);
await user.click(screen.getByRole("button", { name: "Stop" }));
await clock.advance(3000);
expect(screen.getByText("1.2 s")).toBeInTheDocument();
const clock = useFakeTimers();
render(<App />);
await user.click(screen.getByRole("button", { name: "Start" }));
await clock.advance(500);
await user.click(screen.getByRole("button", { name: "Stop" }));
await clock.advance(1000);
await user.click(screen.getByRole("button", { name: "Start" }));
await clock.advance(300);
expect(screen.getByText("0.8 s")).toBeInTheDocument();
const clock = useFakeTimers();
render(<App />);
await user.click(screen.getByRole("button", { name: "Start" }));
await clock.advance(200);
expect(clock.pending()).toBeGreaterThan(0);
await user.click(screen.getByRole("button", { name: "Stop" }));
expect(clock.pending()).toBe(0);
const clock = useFakeTimers();
render(<App />);
await user.click(screen.getByRole("button", { name: "Start" }));
await clock.advance(700);
await user.click(screen.getByRole("button", { name: "Reset" }));
expect(screen.getByText("0.0 s")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Start" })).toBeInTheDocument();
await clock.advance(1000);
expect(screen.getByText("0.0 s")).toBeInTheDocument();
const clock = useFakeTimers();
render(<App />);
for (let i = 0; i < 2; i++) {
await user.click(screen.getByRole("button", { name: "Start" }));
await user.click(screen.getByRole("button", { name: "Stop" }));
}
await user.click(screen.getByRole("button", { name: "Start" }));
await clock.advance(1000);
expect(screen.getByText("1.0 s")).toBeInTheDocument();
const clock = useFakeTimers();
const { unmount } = render(<App />);
await user.click(screen.getByRole("button", { name: "Start" }));
await clock.advance(300);
unmount();
expect(clock.pending()).toBe(0);
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 Stopwatch. 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.