1 import throttle from './throttle';
3 describe('throttle()', () => {
16 it('invokes function at most once every wait milliseconds', () => {
17 const functionToThrottle = jest.fn();
19 const throttledFunction = throttle(functionToThrottle, wait);
22 expect(functionToThrottle).toHaveBeenCalledTimes(1);
24 // Call function just before the wait time expires
25 jest.advanceTimersByTime(wait - 1);
27 expect(functionToThrottle).toHaveBeenCalledTimes(1);
29 // fast-forward until 1st call should be executed
30 jest.advanceTimersByTime(1);
31 expect(functionToThrottle).toHaveBeenCalledTimes(2);
34 expect(functionToThrottle).toHaveBeenCalledTimes(2);
36 jest.advanceTimersByTime(wait);
37 expect(functionToThrottle).toHaveBeenCalledTimes(3);