Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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()
Add Comment
Please, Sign In to add comment