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', () => {
13 jest.clearAllTimers();
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 = {
34 expect(result.current).toStrictEqual(initialExpectedValue);
36 // fast-forward time until 1 millisecond before it should be updated
38 jest.advanceTimersByTime(SECOND - 1);
40 expect(result.current).toStrictEqual(initialExpectedValue);
42 // fast-forward until 1st update should be
44 jest.advanceTimersByTime(1);
46 expect(result.current).toStrictEqual({ ...initialExpectedValue, diff: diff - SECOND, seconds: 35 });
49 it('should allow configuration of interval', () => {
50 const diff = 123456789;
52 const expiry = new Date(Date.now() + diff);
53 const { result } = renderHook(() => useDateCountdown(expiry, { interval }));
55 const initialExpectedValue = {
63 expect(result.current).toStrictEqual(initialExpectedValue);
65 // fast-forward time until 1 millisecond before it should be updated
67 jest.advanceTimersByTime(interval - 1);
69 expect(result.current).toStrictEqual(initialExpectedValue);
71 // fast-forward until 1st update should be
73 jest.advanceTimersByTime(1);
75 expect(result.current).toStrictEqual({ ...initialExpectedValue, diff: diff - interval });
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();
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();
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();
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;
113 jest.advanceTimersByTime(advanceBy);
116 expect(result.current).toStrictEqual({
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({
141 // fast-forward time 1 Day
143 jest.advanceTimersByTime(DAY);
146 expect(result.current).toStrictEqual({
155 // fast-forward time 1 Hour
157 jest.advanceTimersByTime(HOUR);
160 expect(result.current).toStrictEqual({
169 // fast-forward time 1 Minute
171 jest.advanceTimersByTime(MINUTE);
174 expect(result.current).toStrictEqual({
183 // fast-forward time 1 Second
185 jest.advanceTimersByTime(SECOND);
188 expect(result.current).toStrictEqual({
199 jest.advanceTimersByTime(1 * DAY + 11 * HOUR + 2 * MINUTE + 31 * SECOND);
201 expect(result.current).toStrictEqual({
210 // fast-forward to past expiry
212 jest.advanceTimersByTime(1 * DAY + 2 * HOUR + 3 * MINUTE + 4 * SECOND);
214 expect(result.current).toStrictEqual({