The road outside the tea shop is getting a new crossing, and the council wants a model of its traffic light. Build TrafficLight: a light that starts on red and cycles red, then green, then yellow, then red again, for as long as it's on the page.
Red stays on for redMs milliseconds, green for greenMs, and yellow for yellowMs. They're props, with the defaults 4000, 3000, and 1000.
Show the light that's on as one word in a <p>: Red, Green, or Yellow. Draw the three lamps however you like, as long as they have no text.
Use one timer at a time. When the traffic light leaves the page, the cycle stops and no timer is left running.
<TrafficLight /> // red 4 s, green 3 s, yellow 1 s
<TrafficLight redMs={2000} greenMs={2000} yellowMs={500} />
// renders, while it's green:
<p>Green</p>
useFakeTimers();
render(<App />);
expect(screen.getByText("Red")).toBeInTheDocument();
expect(screen.queryByText("Green")).not.toBeInTheDocument();
expect(screen.queryByText("Yellow")).not.toBeInTheDocument();
const clock = useFakeTimers();
render(<App />);
await clock.advance(3999);
expect(screen.getByText("Red")).toBeInTheDocument();
await clock.advance(1);
expect(screen.getByText("Green")).toBeInTheDocument();
expect(screen.queryByText("Red")).not.toBeInTheDocument();
const clock = useFakeTimers();
render(<App />);
await clock.advance(4000);
await clock.advance(2999);
expect(screen.getByText("Green")).toBeInTheDocument();
await clock.advance(1);
expect(screen.getByText("Yellow")).toBeInTheDocument();
const clock = useFakeTimers();
render(<App />);
await clock.advance(7000);
expect(screen.getByText("Yellow")).toBeInTheDocument();
await clock.advance(1000);
expect(screen.getByText("Red")).toBeInTheDocument();
const clock = useFakeTimers();
render(<App />);
await clock.advance(8000 * 3);
expect(screen.getByText("Red")).toBeInTheDocument();
await clock.advance(4000);
expect(screen.getByText("Green")).toBeInTheDocument();
await clock.advance(3000);
expect(screen.getByText("Yellow")).toBeInTheDocument();
const clock = useFakeTimers();
render(<App redMs={1000} greenMs={500} yellowMs={200} />);
await clock.advance(1000);
expect(screen.getByText("Green")).toBeInTheDocument();
await clock.advance(500);
expect(screen.getByText("Yellow")).toBeInTheDocument();
await clock.advance(200);
expect(screen.getByText("Red")).toBeInTheDocument();
const clock = useFakeTimers();
render(<App />);
expect(clock.pending()).toBe(1);
await clock.advance(4000);
expect(clock.pending()).toBe(1);
await clock.advance(4000);
expect(clock.pending()).toBe(1);
const clock = useFakeTimers();
const { unmount } = render(<App />);
await clock.advance(5000);
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 Traffic light. 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.