Merge branch 'INDA-330-pii-update' into 'main'
[ProtonMail-WebClient.git] / packages / hooks / useDateCountdown.test.ts
blob0a35befc124d10c102a70fc1e2065cf87219a96f
1 import { act, renderHook } from '@testing-library/react-hooks';
3 import { DAY, HOUR, MINUTE, SECOND } from '@proton/shared/lib/constants';
5 import useDateCountdown from './useDateCountdown';
7 describe('useDateCountdown', () => {
8     beforeAll(() => {
9         jest.useFakeTimers();
10     });
12     afterEach(() => {
13         jest.clearAllTimers();
14     });
16     afterAll(() => {
17         jest.useRealTimers();
18     });
20     describe('interval', () => {
21         it('should default interval to update every second', () => {
22             const diff = 123456789;
23             const expiry = new Date(Date.now() + diff);
24             const { result } = renderHook(() => useDateCountdown(expiry));
26             const initialExpectedValue = {
27                 days: 1,
28                 diff,
29                 expired: false,
30                 hours: 10,
31                 minutes: 17,
32                 seconds: 36,
33             };
34             expect(result.current).toStrictEqual(initialExpectedValue);
36             // fast-forward time until 1 millisecond before it should be updated
37             act(() => {
38                 jest.advanceTimersByTime(SECOND - 1);
39             });
40             expect(result.current).toStrictEqual(initialExpectedValue);
42             // fast-forward until 1st update should be
43             act(() => {
44                 jest.advanceTimersByTime(1);
45             });
46             expect(result.current).toStrictEqual({ ...initialExpectedValue, diff: diff - SECOND, seconds: 35 });
47         });
49         it('should allow configuration of interval', () => {
50             const diff = 123456789;
51             const interval = 500;
52             const expiry = new Date(Date.now() + diff);
53             const { result } = renderHook(() => useDateCountdown(expiry, { interval }));
55             const initialExpectedValue = {
56                 days: 1,
57                 diff,
58                 expired: false,
59                 hours: 10,
60                 minutes: 17,
61                 seconds: 36,
62             };
63             expect(result.current).toStrictEqual(initialExpectedValue);
65             // fast-forward time until 1 millisecond before it should be updated
66             act(() => {
67                 jest.advanceTimersByTime(interval - 1);
68             });
69             expect(result.current).toStrictEqual(initialExpectedValue);
71             // fast-forward until 1st update should be
72             act(() => {
73                 jest.advanceTimersByTime(1);
74             });
75             expect(result.current).toStrictEqual({ ...initialExpectedValue, diff: diff - interval });
76         });
77     });
79     describe('expired', () => {
80         it('should be false when date is in the future', () => {
81             const expiry = new Date(Date.now() + 1000);
82             const { result } = renderHook(() => useDateCountdown(expiry));
84             const value = result.current;
86             expect(value.expired).toBeFalsy();
87         });
89         it('should be false when date now', () => {
90             const expiry = new Date(Date.now());
91             const { result } = renderHook(() => useDateCountdown(expiry));
93             const value = result.current;
95             expect(value.expired).toBeFalsy();
96         });
98         it('should be true when date is in the past', () => {
99             const expiry = new Date(Date.now() - 1000);
100             const { result } = renderHook(() => useDateCountdown(expiry));
102             const value = result.current;
104             expect(value.expired).toBeTruthy();
105         });
107         it('should return negative diff', () => {
108             const expiry = new Date(Date.now());
109             const { result } = renderHook(() => useDateCountdown(expiry));
111             const advanceBy = 1 * DAY + 2 * HOUR + 3 * MINUTE + 4 * SECOND;
112             act(() => {
113                 jest.advanceTimersByTime(advanceBy);
114             });
116             expect(result.current).toStrictEqual({
117                 diff: -advanceBy,
118                 expired: true,
119                 days: 1,
120                 hours: 2,
121                 minutes: 3,
122                 seconds: 4,
123             });
124         });
125     });
127     it('should correctly countdown', () => {
128         let diff = 2 * DAY + 12 * HOUR + 3 * MINUTE + 32 * SECOND;
129         const expiry = new Date(Date.now() + diff);
130         const { result } = renderHook(() => useDateCountdown(expiry));
132         expect(result.current).toStrictEqual({
133             diff,
134             expired: false,
135             days: 2,
136             hours: 12,
137             minutes: 3,
138             seconds: 32,
139         });
141         // fast-forward time 1 Day
142         act(() => {
143             jest.advanceTimersByTime(DAY);
144         });
145         diff -= DAY;
146         expect(result.current).toStrictEqual({
147             diff,
148             expired: false,
149             days: 1,
150             hours: 12,
151             minutes: 3,
152             seconds: 32,
153         });
155         // fast-forward time 1 Hour
156         act(() => {
157             jest.advanceTimersByTime(HOUR);
158         });
159         diff -= HOUR;
160         expect(result.current).toStrictEqual({
161             diff,
162             expired: false,
163             days: 1,
164             hours: 11,
165             minutes: 3,
166             seconds: 32,
167         });
169         // fast-forward time 1 Minute
170         act(() => {
171             jest.advanceTimersByTime(MINUTE);
172         });
173         diff -= MINUTE;
174         expect(result.current).toStrictEqual({
175             diff,
176             expired: false,
177             days: 1,
178             hours: 11,
179             minutes: 2,
180             seconds: 32,
181         });
183         // fast-forward time 1 Second
184         act(() => {
185             jest.advanceTimersByTime(SECOND);
186         });
187         diff -= SECOND;
188         expect(result.current).toStrictEqual({
189             diff,
190             expired: false,
191             days: 1,
192             hours: 11,
193             minutes: 2,
194             seconds: 31,
195         });
197         // fast-forward rest
198         act(() => {
199             jest.advanceTimersByTime(1 * DAY + 11 * HOUR + 2 * MINUTE + 31 * SECOND);
200         });
201         expect(result.current).toStrictEqual({
202             diff: 0,
203             expired: false,
204             days: 0,
205             hours: 0,
206             minutes: 0,
207             seconds: 0,
208         });
210         // fast-forward to past expiry
211         act(() => {
212             jest.advanceTimersByTime(1 * DAY + 2 * HOUR + 3 * MINUTE + 4 * SECOND);
213         });
214         expect(result.current).toStrictEqual({
215             diff: -93784000,
216             expired: true,
217             days: 1,
218             hours: 2,
219             minutes: 3,
220             seconds: 4,
221         });
222     });