diff --git a/kata-solutions/babysteps-timer/babysteps-typescript/spec/babystep.spec.ts b/kata-solutions/babysteps-timer/babysteps-typescript/spec/babystep.spec.ts index a983e007..60c62a08 100644 --- a/kata-solutions/babysteps-timer/babysteps-typescript/spec/babystep.spec.ts +++ b/kata-solutions/babysteps-timer/babysteps-typescript/spec/babystep.spec.ts @@ -44,8 +44,7 @@ describe("A new babysteps timer", function() { }) it("resets when time has passd beyond the expiry time", async(): Promise => { - // await fakeClock.nextCurrentTimeValueIs(121) + //await fakeClock.nextCurrentTimeValueIs(121) expect(document.querySelector("h1")?.innerHTML).to.equal("02:00") }) }) - diff --git a/kata-solutions/babysteps-timer/babysteps-typescript/src/babystep.ts b/kata-solutions/babysteps-timer/babysteps-typescript/src/babystep.ts index 8e6df3de..42edec8a 100644 --- a/kata-solutions/babysteps-timer/babysteps-typescript/src/babystep.ts +++ b/kata-solutions/babysteps-timer/babysteps-typescript/src/babystep.ts @@ -35,11 +35,11 @@ export function command(arg: string, clock: RealClock = new RealClock()): void { _threadTimer = setInterval(function() { if (_timerRunning) { - let elapsedTime: number = Date.now() - _currentCycleStartTime; + let elapsedTime: number = clock.currentTime() - _currentCycleStartTime; if (elapsedTime >= SecondsInCycle * 1000 + 980) { - _currentCycleStartTime = Date.now(); - elapsedTime = Date.now() - _currentCycleStartTime; + _currentCycleStartTime = clock.currentTime() + elapsedTime = clock.currentTime() - _currentCycleStartTime; } if (elapsedTime >= 5000 && elapsedTime < 6000 && _bodyBackgroundColor != BackgroundColorNeutral) { _bodyBackgroundColor = BackgroundColorNeutral; diff --git a/tdd-katas/babysteps-timer/babysteps-typescript/README.md b/tdd-katas/babysteps-timer/babysteps-typescript/README.md index 1522ff6c..f69714bd 100644 --- a/tdd-katas/babysteps-timer/babysteps-typescript/README.md +++ b/tdd-katas/babysteps-timer/babysteps-typescript/README.md @@ -250,5 +250,60 @@ Let's obtain that value via a separate function call: }) }) ``` + + Finally, all `Date.now()` statements in the babysteps timer can be + replaced by our `clock.currentTime()`: + + ```typescript + export function command(arg: string, clock: RealClock = new RealClock()): void { + let args = { Url: { AbsoluteUri: `command://${arg}/` } } + console.log('called', arg, args.Url.AbsoluteUri); + if (args.Url.AbsoluteUri == "command://start/") { + document.body.innerHTML = CreateTimerHtml(getRemainingTimeCaption(0), BackgroundColorNeutral, true); + + _timerRunning = true; + _currentCycleStartTime = clock.currentTime(); + + _threadTimer = setInterval(function() { + if (_timerRunning) { + let elapsedTime: number = clock.currentTime() - _currentCycleStartTime; + + if (elapsedTime >= SecondsInCycle * 1000 + 980) { + _currentCycleStartTime = clock.currentTime() + elapsedTime = clock.currentTime() - _currentCycleStartTime; + } + + // ... + ``` + + +## Check that the clock resets + +Next, we can write a test that tests that the clock resets after two minutes: + +
+A tests for a clock reset after two minutes + +```typescript +it("resets when time has passd beyond the expiry time", async(): Promise => { + await fakeClock.nextCurrentTimeValueIs(121) + expect(document.querySelector("h1")?.innerHTML).to.equal("02:00") +}) +``` + +This fails because a system call is made to play the audio, which we +obviously don't have available when running our tests (outside of the +browser): + +``` + 2) A new babysteps timer + resets when time has passd beyond the expiry time: + Uncaught ReferenceError: Audio is not defined + at playSound (src/babystep.ts:2:5894) + at Timeout._onTimeout (src/babystep.ts:2:2566) + at listOnTimeout (node:internal/timers:569:17) + at processTimers (node:internal/timers:512:7) +```
+## Intervening the audio \ No newline at end of file