throttle.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import throttle from '../src/throttle';
  6. describe( 'throttle', () => {
  7. const sandbox = sinon.sandbox.create( {
  8. useFakeTimers: true
  9. } );
  10. beforeEach( () => {
  11. sandbox.useFakeTimers( { now: 1000 } );
  12. } );
  13. afterEach( () => {
  14. sandbox.restore();
  15. } );
  16. it( 'should run first call synchronously', () => {
  17. const spy = sinon.spy();
  18. const throttledFn = throttle( spy, 100 );
  19. throttledFn();
  20. sinon.assert.calledOnce( spy );
  21. sandbox.clock.runAll();
  22. sinon.assert.calledOnce( spy );
  23. } );
  24. it( 'should run next calls after specified amount of time', () => {
  25. const spy = sinon.spy();
  26. const throttledFn = throttle( spy, 100 );
  27. throttledFn();
  28. throttledFn();
  29. sinon.assert.calledOnce( spy );
  30. sandbox.clock.tick( 99 );
  31. sinon.assert.calledOnce( spy );
  32. sandbox.clock.tick( 1 );
  33. sinon.assert.calledTwice( spy );
  34. } );
  35. it( 'should skip the call if another call is scheduled', () => {
  36. const spy = sinon.spy();
  37. const throttledFn = throttle( spy, 100 );
  38. const isFirstInvoked = throttledFn();
  39. const willSecondInvoke = throttledFn();
  40. const willThirdInvoke = throttledFn();
  41. expect( isFirstInvoked ).to.be.true;
  42. expect( willSecondInvoke ).to.be.true;
  43. expect( willThirdInvoke ).to.be.false;
  44. sandbox.clock.runAll();
  45. sinon.assert.calledTwice( spy );
  46. } );
  47. it( 'should call the next call after the specified amount of time', () => {
  48. const spy = sinon.spy();
  49. const throttledFn = throttle( spy, 100 );
  50. throttledFn();
  51. throttledFn();
  52. sandbox.clock.tick( 50 );
  53. sinon.assert.calledOnce( spy );
  54. sandbox.clock.tick( 50 );
  55. sinon.assert.calledTwice( spy );
  56. throttledFn();
  57. sandbox.clock.tick( 100 );
  58. sinon.assert.calledThrice( spy );
  59. } );
  60. describe( 'flush', () => {
  61. it( 'should be provide as a method on the throttled function', () => {
  62. const spy = sinon.spy();
  63. const throttledFn = throttle( spy, 100 );
  64. expect( throttledFn.flush ).to.be.a( 'function' );
  65. } );
  66. it( 'should enable calling the throttled call immediately', () => {
  67. const spy = sinon.spy();
  68. const throttledFn = throttle( spy, 100 );
  69. throttledFn();
  70. throttledFn();
  71. sinon.assert.calledOnce( spy );
  72. throttledFn.flush();
  73. sinon.assert.calledTwice( spy );
  74. sandbox.clock.runAll();
  75. sinon.assert.calledTwice( spy );
  76. } );
  77. it( 'should do nothing if there is no scheduled call', () => {
  78. const spy = sinon.spy();
  79. const throttledFn = throttle( spy, 100 );
  80. throttledFn();
  81. sinon.assert.calledOnce( spy );
  82. throttledFn.flush();
  83. sinon.assert.calledOnce( spy );
  84. sandbox.clock.runAll();
  85. sinon.assert.calledOnce( spy );
  86. } );
  87. it( 'should enable calling after the flushed call', () => {
  88. const spy = sinon.spy();
  89. const throttledFn = throttle( spy, 100 );
  90. throttledFn();
  91. throttledFn();
  92. throttledFn.flush();
  93. throttledFn();
  94. sinon.assert.calledThrice( spy );
  95. sandbox.clock.runAll();
  96. sinon.assert.calledThrice( spy );
  97. } );
  98. } );
  99. } );