|
| 1 | +import { dateToString, dateToNgbDateStruct, dateToISOFormat, isValidDate, yearFromString } from './date.util'; |
| 2 | + |
| 3 | +describe('Date Utils', () => { |
| 4 | + |
| 5 | + describe('dateToISOFormat', () => { |
| 6 | + it('should convert Date to YYYY-MM-DDThh:mm:ssZ string', () => { |
| 7 | + // NOTE: month is zero indexed which is why it increases by one |
| 8 | + expect(dateToISOFormat(new Date(Date.UTC(2022, 5, 3)))).toEqual('2022-06-03T00:00:00Z'); |
| 9 | + }); |
| 10 | + it('should convert Date string to YYYY-MM-DDThh:mm:ssZ string', () => { |
| 11 | + expect(dateToISOFormat('2022-06-03')).toEqual('2022-06-03T00:00:00Z'); |
| 12 | + }); |
| 13 | + it('should convert Month string to YYYY-MM-DDThh:mm:ssZ string', () => { |
| 14 | + expect(dateToISOFormat('2022-06')).toEqual('2022-06-01T00:00:00Z'); |
| 15 | + }); |
| 16 | + it('should convert Year string to YYYY-MM-DDThh:mm:ssZ string', () => { |
| 17 | + expect(dateToISOFormat('2022')).toEqual('2022-01-01T00:00:00Z'); |
| 18 | + }); |
| 19 | + it('should convert ISO Date string to YYYY-MM-DDThh:mm:ssZ string', () => { |
| 20 | + // NOTE: Time is always zeroed out as proven by this test. |
| 21 | + expect(dateToISOFormat('2022-06-03T03:24:04Z')).toEqual('2022-06-03T00:00:00Z'); |
| 22 | + }); |
| 23 | + it('should convert NgbDateStruct to YYYY-MM-DDThh:mm:ssZ string', () => { |
| 24 | + // NOTE: month is zero indexed which is why it increases by one |
| 25 | + const date = new Date(2022, 5, 3); |
| 26 | + expect(dateToISOFormat(dateToNgbDateStruct(date))).toEqual('2022-06-03T00:00:00Z'); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('dateToString', () => { |
| 31 | + it('should convert Date to YYYY-MM-DD string', () => { |
| 32 | + // NOTE: month is zero indexed which is why it increases by one |
| 33 | + expect(dateToString(new Date(2022, 5, 3))).toEqual('2022-06-03'); |
| 34 | + }); |
| 35 | + it('should convert Date with time to YYYY-MM-DD string', () => { |
| 36 | + // NOTE: month is zero indexed which is why it increases by one |
| 37 | + expect(dateToString(new Date(2022, 5, 3, 3, 24, 0))).toEqual('2022-06-03'); |
| 38 | + }); |
| 39 | + it('should convert Month only to YYYY-MM-DD string', () => { |
| 40 | + // NOTE: month is zero indexed which is why it increases by one |
| 41 | + expect(dateToString(new Date(2022, 5))).toEqual('2022-06-01'); |
| 42 | + }); |
| 43 | + it('should convert ISO Date to YYYY-MM-DD string', () => { |
| 44 | + expect(dateToString(new Date('2022-06-03T03:24:00Z'))).toEqual('2022-06-03'); |
| 45 | + }); |
| 46 | + it('should convert NgbDateStruct to YYYY-MM-DD string', () => { |
| 47 | + // NOTE: month is zero indexed which is why it increases by one |
| 48 | + const date = new Date(2022, 5, 3); |
| 49 | + expect(dateToString(dateToNgbDateStruct(date))).toEqual('2022-06-03'); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + |
| 54 | + describe('isValidDate', () => { |
| 55 | + it('should return false for null', () => { |
| 56 | + expect(isValidDate(null)).toBe(false); |
| 57 | + }); |
| 58 | + it('should return false for empty string', () => { |
| 59 | + expect(isValidDate('')).toBe(false); |
| 60 | + }); |
| 61 | + it('should return false for text', () => { |
| 62 | + expect(isValidDate('test')).toBe(false); |
| 63 | + }); |
| 64 | + it('should return true for YYYY', () => { |
| 65 | + expect(isValidDate('2022')).toBe(true); |
| 66 | + }); |
| 67 | + it('should return true for YYYY-MM', () => { |
| 68 | + expect(isValidDate('2022-12')).toBe(true); |
| 69 | + }); |
| 70 | + it('should return true for YYYY-MM-DD', () => { |
| 71 | + expect(isValidDate('2022-06-03')).toBe(true); |
| 72 | + }); |
| 73 | + it('should return true for YYYY-MM-DDTHH:MM:SS', () => { |
| 74 | + expect(isValidDate('2022-06-03T10:20:30')).toBe(true); |
| 75 | + }); |
| 76 | + it('should return true for YYYY-MM-DDTHH:MM:SSZ', () => { |
| 77 | + expect(isValidDate('2022-06-03T10:20:30Z')).toBe(true); |
| 78 | + }); |
| 79 | + it('should return false for a month that does not exist', () => { |
| 80 | + expect(isValidDate('2022-13')).toBe(false); |
| 81 | + }); |
| 82 | + it('should return false for a day that does not exist', () => { |
| 83 | + expect(isValidDate('2022-02-60')).toBe(false); |
| 84 | + }); |
| 85 | + it('should return false for a time that does not exist', () => { |
| 86 | + expect(isValidDate('2022-02-60T10:60:20')).toBe(false); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('yearFromString', () => { |
| 91 | + it('should return year from YYYY string', () => { |
| 92 | + expect(yearFromString('2022')).toEqual(2022); |
| 93 | + }); |
| 94 | + it('should return year from YYYY-MM string', () => { |
| 95 | + expect(yearFromString('1970-06')).toEqual(1970); |
| 96 | + }); |
| 97 | + it('should return year from YYYY-MM-DD string', () => { |
| 98 | + expect(yearFromString('1914-10-23')).toEqual(1914); |
| 99 | + }); |
| 100 | + it('should return year from YYYY-MM-DDTHH:MM:SSZ string', () => { |
| 101 | + expect(yearFromString('1914-10-23T10:20:30Z')).toEqual(1914); |
| 102 | + }); |
| 103 | + it('should return null if invalid date', () => { |
| 104 | + expect(yearFromString('test')).toBeNull(); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments