Guest User

Untitled

a guest
Aug 31st, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. class Countdown
  2. constructor: (@clock, @seconds) ->
  3. $.extend(this, new Observable)
  4. @finished = false
  5.  
  6. start: =>
  7. fn = =>
  8. this.trigger("updated", @seconds)
  9. @seconds--
  10. if @seconds >= 0
  11. @clock.setTimeout(1000, fn)
  12. else
  13. @finished = true
  14. this.trigger("finished")
  15. fn()
  16.  
  17.  
  18. describe "Countdown", ->
  19. beforeEach ->
  20. @clock = new TestClock
  21. @countdown = new Countdown(@clock, 5)
  22. @updateObserver = jasmine.createSpy("updateObserver")
  23. @finishObserver = jasmine.createSpy("finishObserver")
  24. @countdown.bind("updated", @updateObserver)
  25. @countdown.bind("finished", @finishObserver)
  26. @countdown.start()
  27.  
  28. describe "after 1 second", ->
  29. beforeEach ->
  30. @clock.advance(seconds: 1)
  31.  
  32. it "should update to 4 seconds", ->
  33. expect(@updateObserver).toHaveBeenCalledWith(4)
  34.  
  35. describe "after 5 seconds", ->
  36. beforeEach ->
  37. @clock.advance(seconds: 5)
  38.  
  39. it "should update with each second", ->
  40. expect(@updateObserver).toHaveBeenCalledWith(4)
  41. expect(@updateObserver).toHaveBeenCalledWith(3)
  42. expect(@updateObserver).toHaveBeenCalledWith(2)
  43. expect(@updateObserver).toHaveBeenCalledWith(1)
  44. expect(@updateObserver).toHaveBeenCalledWith(0)
  45.  
  46. it "should trigger 'finished' event", ->
  47. expect(@finishObserver).toHaveBeenCalled()
Add Comment
Please, Sign In to add comment