Coding task · JavaScript and React course · Module 13: Testing React
Practice: Countdown Timer
An interval is a side effect, so we create it in useEffect, and the function returned from the effect cleans it up before the effect runs again and on unmount. Without the cleanup, Pause would not stop the countdown, because the old interval would keep running. In the test, jest.useFakeTimers() replaces the clock and jest.advanceTimersByTime(3000) moves it three seconds forward without waiting, so after Start and 3 s the test expects 7 s.
Task
Finish CountdownTimer, which counts down the seconds to the rocket launch after you click Start. ___BLANK1___: every second the updater function lowers the current value by 1 (use the current parameter, not the left variable, because the interval is created once and would see an old value). ___BLANK2___: the effect returns a cleanup function that calls clearInterval(id), so that Pause, Reset and unmounting stop the interval. ___BLANK3___: elapsed is the number of seconds that have already passed (seconds minus left); the progress bar shows it in aria-valuenow. The test turns on fake timers and moves time forward second by second.