Skip to content

Instantly share code, notes, and snippets.

@jdudek
Created December 30, 2011 13:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdudek/1539919 to your computer and use it in GitHub Desktop.
Save jdudek/1539919 to your computer and use it in GitHub Desktop.
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