Use source loader for email sprite icons
[ProtonMail-WebClient.git] / packages / utils / throttle.test.ts
blob7aad8717de6d903ada55970cb537901712d5a416
1 import throttle from './throttle';
3 describe('throttle()', () => {
4     beforeAll(() => {
5         jest.useFakeTimers();
6     });
8     afterEach(() => {
9         jest.clearAllTimers();
10     });
12     afterAll(() => {
13         jest.useRealTimers();
14     });
16     it('invokes function at most once every wait milliseconds', () => {
17         const functionToThrottle = jest.fn();
18         const wait = 1000;
19         const throttledFunction = throttle(functionToThrottle, wait);
21         throttledFunction();
22         expect(functionToThrottle).toHaveBeenCalledTimes(1);
24         // Call function just before the wait time expires
25         jest.advanceTimersByTime(wait - 1);
26         throttledFunction();
27         expect(functionToThrottle).toHaveBeenCalledTimes(1);
29         // fast-forward until 1st call should be executed
30         jest.advanceTimersByTime(1);
31         expect(functionToThrottle).toHaveBeenCalledTimes(2);
33         throttledFunction();
34         expect(functionToThrottle).toHaveBeenCalledTimes(2);
36         jest.advanceTimersByTime(wait);
37         expect(functionToThrottle).toHaveBeenCalledTimes(3);
38     });
39 });