1 | import * as datetimeUtility from './datetime_utility';
|
2 |
|
3 | describe('Date time utils', () => {
|
4 | describe('newDate', () => {
|
5 | it('returns new date instance from existing date instance', () => {
|
6 | const initialDate = new Date(2019, 0, 1);
|
7 | const copiedDate = datetimeUtility.newDate(initialDate);
|
8 |
|
9 | expect(copiedDate.getTime()).toBe(initialDate.getTime());
|
10 |
|
11 | initialDate.setMonth(initialDate.getMonth() + 1);
|
12 |
|
13 | expect(copiedDate.getTime()).not.toBe(initialDate.getTime());
|
14 | });
|
15 |
|
16 | it('returns date instance when provided date param is not of type date or is undefined', () => {
|
17 | const initialDate = datetimeUtility.newDate();
|
18 |
|
19 | expect(initialDate instanceof Date).toBe(true);
|
20 | });
|
21 | });
|
22 |
|
23 | describe('get day difference', () => {
|
24 | it('should return 7', () => {
|
25 | const firstDay = new Date('07/01/2016');
|
26 | const secondDay = new Date('07/08/2016');
|
27 | const difference = datetimeUtility.getDayDifference(firstDay, secondDay);
|
28 |
|
29 | expect(difference).toBe(7);
|
30 | });
|
31 |
|
32 | it('should return 31', () => {
|
33 | const firstDay = new Date('07/01/2016');
|
34 | const secondDay = new Date('08/01/2016');
|
35 | const difference = datetimeUtility.getDayDifference(firstDay, secondDay);
|
36 |
|
37 | expect(difference).toBe(31);
|
38 | });
|
39 |
|
40 | it('should return 365', () => {
|
41 | const firstDay = new Date('07/02/2015');
|
42 | const secondDay = new Date('07/01/2016');
|
43 | const difference = datetimeUtility.getDayDifference(firstDay, secondDay);
|
44 |
|
45 | expect(difference).toBe(365);
|
46 | });
|
47 | });
|
48 |
|
49 | describe('getDateInPast', () => {
|
50 | const date = new Date('2019-07-16T00:00:00.000Z');
|
51 | const daysInPast = 90;
|
52 |
|
53 | it('returns the correct date in the past', () => {
|
54 | const dateInPast = datetimeUtility.getDateInPast(date, daysInPast);
|
55 | const expectedDateInPast = new Date('2019-04-17T00:00:00.000Z');
|
56 |
|
57 | expect(dateInPast).toStrictEqual(expectedDateInPast);
|
58 | });
|
59 |
|
60 | it('does not modifiy the original date', () => {
|
61 | datetimeUtility.getDateInPast(date, daysInPast);
|
62 | expect(date).toStrictEqual(new Date('2019-07-16T00:00:00.000Z'));
|
63 | });
|
64 | });
|
65 |
|
66 | describe('getDateInFuture', () => {
|
67 | const date = new Date('2019-07-16T00:00:00.000Z');
|
68 | const daysInFuture = 90;
|
69 |
|
70 | it('returns the correct date in the future', () => {
|
71 | const dateInFuture = datetimeUtility.getDateInFuture(date, daysInFuture);
|
72 | const expectedDateInFuture = new Date('2019-10-14T00:00:00.000Z');
|
73 |
|
74 | expect(dateInFuture).toStrictEqual(expectedDateInFuture);
|
75 | });
|
76 |
|
77 | it('does not modifiy the original date', () => {
|
78 | datetimeUtility.getDateInFuture(date, daysInFuture);
|
79 | expect(date).toStrictEqual(new Date('2019-07-16T00:00:00.000Z'));
|
80 | });
|
81 | });
|
82 |
|
83 | describe('areDatesEqual', () => {
|
84 | it.each`
|
85 | name | input | expected
|
86 | ${'Returns false for no inputs'} | ${[]} | ${false}
|
87 | ${'Returns false for null inputs'} | ${[null, null]} | ${false}
|
88 | ${'Returns false for empty inputs'} | ${['', '']} | ${false}
|
89 | ${'Returns true for date without timestamp'} | ${['2020-01-10', '2020-01-10']} | ${true}
|
90 | ${'Returns false for different dates in local tz'} | ${['Mon Apr 27 2020 15:21:22 GMT-0700 (Pacific Daylight Time)', 'Mon Apr 27 2020 15:22:22 GMT-0700 (Pacific Daylight Time)']} | ${false}
|
91 | ${'Returns true for same dates in local tz'} | ${['Mon Apr 27 2020 15:22:22 GMT-0700 (Pacific Daylight Time)', 'Mon Apr 27 2020 15:22:22 GMT-0700 (Pacific Daylight Time)']} | ${true}
|
92 | ${'Returns false for different dates in UTC'} | ${['2020-04-27 22:22:21 UTC', '2020-04-27 22:22:22 UTC']} | ${false}
|
93 | ${'Returns true for same dates in UTC'} | ${['2020-04-27 22:22:22 UTC', '2020-04-27 22:22:22 UTC']} | ${true}
|
94 | `(`$name`, ({ input, expected }) => {
|
95 | expect(datetimeUtility.areDatesEqual(...input)).toBe(expected);
|
96 | });
|
97 | });
|
98 | });
|