time.test.js 486 B

1234567891011121314151617181920
  1. import { throttle } from '../../src/js/utils/time';
  2. describe( 'Time function ', () => {
  3. test( '"throttle" should reduce frequency of a callback function.', done => {
  4. const callback = jest.fn();
  5. const throttled = throttle( callback, 40 );
  6. let counter = 0;
  7. const intervalID = setInterval( () => {
  8. counter++;
  9. throttled();
  10. if ( counter >= 10 ) {
  11. expect( callback ).toHaveBeenCalledTimes( 4 );
  12. clearInterval( intervalID );
  13. done();
  14. }
  15. }, 20 );
  16. } );
  17. } );