Skip to content

Instantly share code, notes, and snippets.

@jdudek jdudek/gist:1539919
Created Dec 30, 2011

Embed
What would you like to do?
Clock in JS tests
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
You can’t perform that action at this time.