|
| 1 | +import { DatePickerUtil } from '../../date-picker/date-picker.utils'; |
| 2 | +import { DatePart, DatePartInfo } from './date-time-editor.common'; |
| 3 | + |
| 4 | +function reduceToDictionary(parts: DatePartInfo[]) { |
| 5 | + return parts.reduce((obj, x) => { |
| 6 | + obj[x.type] = x; |
| 7 | + return obj; |
| 8 | + }, {}); |
| 9 | +} |
| 10 | + |
| 11 | +describe('Date Time Parsing', () => { |
| 12 | + it('should correctly parse all date time parts (base)', () => { |
| 13 | + const result = DatePickerUtil.parseDateTimeFormat('dd/MM/yyyy HH:mm:ss tt'); |
| 14 | + const expected = [ |
| 15 | + {start: 0, end: 2, type: DatePart.Date, format: 'dd'}, |
| 16 | + {start: 2, end: 3, type: DatePart.Literal, format: '/'}, |
| 17 | + {start: 3, end: 5, type: DatePart.Month, format: 'MM'}, |
| 18 | + {start: 5, end: 6, type: DatePart.Literal, format: '/'}, |
| 19 | + {start: 6, end: 10, type: DatePart.Year, format: 'yyyy'}, |
| 20 | + {start: 10, end: 11, type: DatePart.Literal, format: ' '}, |
| 21 | + {start: 11, end: 13, type: DatePart.Hours, format: 'HH'}, |
| 22 | + {start: 13, end: 14, type: DatePart.Literal, format: ':'}, |
| 23 | + {start: 14, end: 16, type: DatePart.Minutes, format: 'mm'}, |
| 24 | + {start: 16, end: 17, type: DatePart.Literal, format: ':'}, |
| 25 | + {start: 17, end: 19, type: DatePart.Seconds, format: 'ss'}, |
| 26 | + {start: 19, end: 20, type: DatePart.Literal, format: ' '}, |
| 27 | + {start: 20, end: 22, type: DatePart.AmPm, format: 'tt'} |
| 28 | + ]; |
| 29 | + expect(JSON.stringify(result)).toEqual(JSON.stringify(expected)); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should correctly parse date parts of with short formats', () => { |
| 33 | + let result = DatePickerUtil.parseDateTimeFormat('MM/dd/yyyy'); |
| 34 | + let resDict = reduceToDictionary(result); |
| 35 | + expect(result.length).toEqual(5); |
| 36 | + expect(resDict[DatePart.Month]).toEqual(jasmine.objectContaining({ start: 0, end: 2 })); |
| 37 | + expect(resDict[DatePart.Date]).toEqual(jasmine.objectContaining({ start: 3, end: 5 })); |
| 38 | + expect(resDict[DatePart.Year]).toEqual(jasmine.objectContaining({ start: 6, end: 10 })); |
| 39 | + |
| 40 | + result = DatePickerUtil.parseDateTimeFormat('M/d/yy'); |
| 41 | + resDict = reduceToDictionary(result); |
| 42 | + expect(result.length).toEqual(5); |
| 43 | + expect(resDict[DatePart.Month]).toEqual(jasmine.objectContaining({ start: 0, end: 2 })); |
| 44 | + expect(resDict[DatePart.Date]).toEqual(jasmine.objectContaining({ start: 3, end: 5 })); |
| 45 | + expect(resDict[DatePart.Year]).toEqual(jasmine.objectContaining({ start: 6, end: 8 })); |
| 46 | + |
| 47 | + result = DatePickerUtil.parseDateTimeFormat('dd.MM.yyyy г.'); |
| 48 | + resDict = reduceToDictionary(result); |
| 49 | + expect(result.length).toEqual(6); |
| 50 | + expect(resDict[DatePart.Date]).toEqual(jasmine.objectContaining({ start: 0, end: 2 })); |
| 51 | + expect(resDict[DatePart.Month]).toEqual(jasmine.objectContaining({ start: 3, end: 5 })); |
| 52 | + expect(resDict[DatePart.Year]).toEqual(jasmine.objectContaining({ start: 6, end: 10 })); |
| 53 | + |
| 54 | + return; // TODO |
| 55 | + result = DatePickerUtil.parseDateTimeFormat('dd.MM.yyyyг'); |
| 56 | + resDict = reduceToDictionary(result); |
| 57 | + expect(result.length).toEqual(6); |
| 58 | + expect(resDict[DatePart.Date]).toEqual(jasmine.objectContaining({ start: 0, end: 2 })); |
| 59 | + expect(resDict[DatePart.Month]).toEqual(jasmine.objectContaining({ start: 3, end: 5 })); |
| 60 | + expect(resDict[DatePart.Year]).toEqual(jasmine.objectContaining({ start: 6, end: 10 })); |
| 61 | + expect(result[5]?.format).toEqual('г'); |
| 62 | + |
| 63 | + result = DatePickerUtil.parseDateTimeFormat('yyyy/MM/d'); |
| 64 | + resDict = reduceToDictionary(result); |
| 65 | + expect(result.length).toEqual(5); |
| 66 | + expect(resDict[DatePart.Year]).toEqual(jasmine.objectContaining({ start: 0, end: 4 })); |
| 67 | + expect(resDict[DatePart.Month]).toEqual(jasmine.objectContaining({ start: 5, end: 7 })); |
| 68 | + expect(resDict[DatePart.Date]).toEqual(jasmine.objectContaining({ start: 8, end: 10 })); |
| 69 | + }); |
| 70 | +}); |
0 commit comments