test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. var debounce = require('.')
  2. var sinon = require('sinon')
  3. describe('housekeeping', function() {
  4. it('should be defined as a function', function() {
  5. expect(typeof debounce).toEqual('function')
  6. })
  7. })
  8. describe('catch issue #3 - Debounced function executing early?', function() {
  9. // use sinon to control the clock
  10. var clock
  11. beforeEach(function(){
  12. clock = sinon.useFakeTimers()
  13. })
  14. afterEach(function(){
  15. clock.restore()
  16. })
  17. it('should debounce with fast timeout', function() {
  18. var callback = sinon.spy()
  19. // set up debounced function with wait of 100
  20. var fn = debounce(callback, 100)
  21. // call debounced function at interval of 50
  22. setTimeout(fn, 100)
  23. setTimeout(fn, 150)
  24. setTimeout(fn, 200)
  25. setTimeout(fn, 250)
  26. // set the clock to 100 (period of the wait) ticks after the last debounced call
  27. clock.tick(350)
  28. // the callback should have been triggered once
  29. expect(callback.callCount).toEqual(1)
  30. })
  31. })
  32. describe('forcing execution', function() {
  33. // use sinon to control the clock
  34. var clock
  35. beforeEach(function(){
  36. clock = sinon.useFakeTimers()
  37. })
  38. afterEach(function(){
  39. clock.restore()
  40. })
  41. it('should not execute prior to timeout', function() {
  42. var callback = sinon.spy()
  43. // set up debounced function with wait of 100
  44. var fn = debounce(callback, 100)
  45. // call debounced function at interval of 50
  46. setTimeout(fn, 100)
  47. setTimeout(fn, 150)
  48. // set the clock to 25 (period of the wait) ticks after the last debounced call
  49. clock.tick(175)
  50. // the callback should not have been called yet
  51. expect(callback.callCount).toEqual(0)
  52. })
  53. it('should execute prior to timeout when flushed', function() {
  54. var callback = sinon.spy()
  55. // set up debounced function with wait of 100
  56. var fn = debounce(callback, 100)
  57. // call debounced function at interval of 50
  58. setTimeout(fn, 100)
  59. setTimeout(fn, 150)
  60. // set the clock to 25 (period of the wait) ticks after the last debounced call
  61. clock.tick(175)
  62. fn.flush()
  63. // the callback has been called
  64. expect(callback.callCount).toEqual(1)
  65. })
  66. it('should not execute again after timeout when flushed before the timeout', function() {
  67. var callback = sinon.spy()
  68. // set up debounced function with wait of 100
  69. var fn = debounce(callback, 100)
  70. // call debounced function at interval of 50
  71. setTimeout(fn, 100)
  72. setTimeout(fn, 150)
  73. // set the clock to 25 (period of the wait) ticks after the last debounced call
  74. clock.tick(175)
  75. fn.flush()
  76. // the callback has been called here
  77. expect(callback.callCount).toEqual(1)
  78. // move to past the timeout
  79. clock.tick(225)
  80. // the callback should have only been called once
  81. expect(callback.callCount).toEqual(1)
  82. })
  83. it('should not execute on a timer after being flushed', function() {
  84. var callback = sinon.spy()
  85. // set up debounced function with wait of 100
  86. var fn = debounce(callback, 100)
  87. // call debounced function at interval of 50
  88. setTimeout(fn, 100)
  89. setTimeout(fn, 150)
  90. // set the clock to 25 (period of the wait) ticks after the last debounced call
  91. clock.tick(175)
  92. fn.flush()
  93. // the callback has been called here
  94. expect(callback.callCount).toEqual(1)
  95. // schedule again
  96. setTimeout(fn, 250)
  97. // move to past the new timeout
  98. clock.tick(400)
  99. // the callback should have been called again
  100. expect(callback.callCount).toEqual(2)
  101. })
  102. it('should not execute when flushed if nothing was scheduled', function() {
  103. var callback = sinon.spy()
  104. // set up debounced function with wait of 100
  105. var fn = debounce(callback, 100)
  106. fn.flush()
  107. // the callback should not have been called
  108. expect(callback.callCount).toEqual(0)
  109. })
  110. })