Created
December 30, 2011 13:43
-
-
Save jdudek/1539919 to your computer and use it in GitHub Desktop.
Clock in JS tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Countdown | |
constructor: (@clock, @seconds) -> | |
$.extend(this, new Observable) | |
@finished = false | |
start: => | |
fn = => | |
this.trigger("updated", @seconds) | |
@seconds-- | |
if @seconds >= 0 | |
@clock.setTimeout(1000, fn) | |
else | |
@finished = true | |
this.trigger("finished") | |
fn() | |
describe "Countdown", -> | |
beforeEach -> | |
@clock = new TestClock | |
@countdown = new Countdown(@clock, 5) | |
@updateObserver = jasmine.createSpy("updateObserver") | |
@finishObserver = jasmine.createSpy("finishObserver") | |
@countdown.bind("updated", @updateObserver) | |
@countdown.bind("finished", @finishObserver) | |
@countdown.start() | |
describe "after 1 second", -> | |
beforeEach -> | |
@clock.advance(seconds: 1) | |
it "should update to 4 seconds", -> | |
expect(@updateObserver).toHaveBeenCalledWith(4) | |
describe "after 5 seconds", -> | |
beforeEach -> | |
@clock.advance(seconds: 5) | |
it "should update with each second", -> | |
expect(@updateObserver).toHaveBeenCalledWith(4) | |
expect(@updateObserver).toHaveBeenCalledWith(3) | |
expect(@updateObserver).toHaveBeenCalledWith(2) | |
expect(@updateObserver).toHaveBeenCalledWith(1) | |
expect(@updateObserver).toHaveBeenCalledWith(0) | |
it "should trigger 'finished' event", -> | |
expect(@finishObserver).toHaveBeenCalled() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment