Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / utils / availability.test.ts
blob2c9d9fa5788981dc04a16e590c9ecfa472b61430
1 import { Availability, AvailabilityTypes } from './availability';
3 const FIVE_MINUTES_IN_MILLISECONDS = 5 * 60 * 1000;
5 jest.useFakeTimers();
6 jest.spyOn(global, 'setInterval');
7 jest.spyOn(global, 'clearInterval');
9 describe('Availability', () => {
10     let reportMock: jest.Mock;
12     beforeEach(() => {
13         reportMock = jest.fn();
14         Availability.clear();
15         jest.clearAllMocks();
16     });
18     it('should initialize with the correct interval', () => {
19         Availability.init(reportMock);
20         expect(setInterval).toHaveBeenCalledTimes(1);
21         expect(setInterval).toHaveBeenCalledWith(expect.any(Function), FIVE_MINUTES_IN_MILLISECONDS);
22     });
24     it('should call report with false when no error has occurred', () => {
25         Availability.init(reportMock);
26         jest.advanceTimersByTime(FIVE_MINUTES_IN_MILLISECONDS);
27         expect(reportMock).toHaveBeenCalledWith({ CRITICAL: false, ERROR: false, SENTRY: false });
28     });
30     it('should call report with sentry:true when a sentry error has occurred and 5 minutes have passed', () => {
31         Availability.init(reportMock);
32         jest.spyOn(Date, 'now').mockReturnValue(1);
33         Availability.mark(AvailabilityTypes.SENTRY);
34         jest.spyOn(Date, 'now').mockReturnValue(FIVE_MINUTES_IN_MILLISECONDS + 2);
35         jest.advanceTimersByTime(FIVE_MINUTES_IN_MILLISECONDS);
36         expect(reportMock).toHaveBeenCalledWith({ CRITICAL: false, ERROR: false, SENTRY: true });
37     });
39     it('should call report with critical:true when a critical error has occurred and 5 minutes have passed', () => {
40         Availability.init(reportMock);
41         jest.spyOn(Date, 'now').mockReturnValue(1);
42         Availability.mark(AvailabilityTypes.CRITICAL);
43         jest.spyOn(Date, 'now').mockReturnValue(FIVE_MINUTES_IN_MILLISECONDS + 2);
44         jest.advanceTimersByTime(FIVE_MINUTES_IN_MILLISECONDS);
45         expect(reportMock).toHaveBeenCalledWith({ CRITICAL: true, ERROR: false, SENTRY: false });
46     });
48     it('should call report with error:true when an error has occurred and 5 minutes have passed', () => {
49         Availability.init(reportMock);
50         jest.spyOn(Date, 'now').mockReturnValue(1);
51         Availability.mark(AvailabilityTypes.ERROR);
52         jest.spyOn(Date, 'now').mockReturnValue(FIVE_MINUTES_IN_MILLISECONDS + 2);
53         jest.advanceTimersByTime(FIVE_MINUTES_IN_MILLISECONDS);
54         expect(reportMock).toHaveBeenCalledWith({ CRITICAL: false, ERROR: true, SENTRY: false });
55     });
57     it('should call report with every keys to true when an all kind of errors has occurred and 5 minutes have passed', () => {
58         Availability.init(reportMock);
59         jest.spyOn(Date, 'now').mockReturnValue(1);
60         Availability.mark(AvailabilityTypes.ERROR);
61         Availability.mark(AvailabilityTypes.SENTRY);
62         Availability.mark(AvailabilityTypes.CRITICAL);
63         jest.spyOn(Date, 'now').mockReturnValue(FIVE_MINUTES_IN_MILLISECONDS + 2);
64         jest.advanceTimersByTime(FIVE_MINUTES_IN_MILLISECONDS);
65         expect(reportMock).toHaveBeenCalledWith({ CRITICAL: true, ERROR: true, SENTRY: true });
66     });
68     it('should call report with false when an error has occurred but 5 minutes have not passed', () => {
69         Availability.init(reportMock);
70         jest.spyOn(Date, 'now').mockReturnValue(1);
71         Availability.mark(AvailabilityTypes.SENTRY);
72         jest.spyOn(Date, 'now').mockReturnValue(FIVE_MINUTES_IN_MILLISECONDS - 2);
73         jest.advanceTimersByTime(FIVE_MINUTES_IN_MILLISECONDS);
74         expect(reportMock).toHaveBeenCalledWith({ CRITICAL: false, ERROR: false, SENTRY: false });
75     });
77     it('should clear the interval on init', () => {
78         Availability.init(reportMock);
79         expect(clearInterval).toHaveBeenCalledTimes(1);
80     });
82     it('should clear the interval on clear', () => {
83         Availability.init(reportMock);
84         Availability.clear();
85         expect(clearInterval).toHaveBeenCalledTimes(2);
86     });
87 });