|
1 | 1 | import { DateTimeUtil } from './date-time.util';
|
2 | 2 | import { DatePart, DatePartInfo } from '../../directives/date-time-editor/date-time-editor.common';
|
| 3 | +import { DataType } from '../../data-operations/data-util'; |
3 | 4 |
|
4 | 5 | const reduceToDictionary = (parts: DatePartInfo[]) => parts.reduce((obj, x) => {
|
5 | 6 | obj[x.type] = x;
|
@@ -233,6 +234,28 @@ describe(`DateTimeUtil Unit tests`, () => {
|
233 | 234 | expect(result).toEqual('MM/dd/yyyy');
|
234 | 235 | });
|
235 | 236 |
|
| 237 | + it('should properly build input formats based on locale for dateTime data type ', () => { |
| 238 | + let result = DateTimeUtil.getDefaultInputFormat('en-US', DataType.DateTime); |
| 239 | + expect(result.normalize('NFKC')).toEqual('MM/dd/yyyy, hh:mm:ss tt'); |
| 240 | + |
| 241 | + result = DateTimeUtil.getDefaultInputFormat('bg-BG', DataType.DateTime); |
| 242 | + expect(result.normalize('NFKC')).toEqual('dd.MM.yyyy г., HH:mm:ss'); |
| 243 | + |
| 244 | + result = DateTimeUtil.getDefaultInputFormat('fr-FR', DataType.DateTime); |
| 245 | + expect(result).toEqual('dd/MM/yyyy HH:mm:ss'); |
| 246 | + }); |
| 247 | + |
| 248 | + it('should properly build input formats based on locale for time data type ', () => { |
| 249 | + let result = DateTimeUtil.getDefaultInputFormat('en-US', DataType.Time); |
| 250 | + expect(result.normalize('NFKC')).toEqual('hh:mm tt'); |
| 251 | + |
| 252 | + result = DateTimeUtil.getDefaultInputFormat('bg-BG', DataType.Time); |
| 253 | + expect(result.normalize('NFKC')).toEqual('HH:mm'); |
| 254 | + |
| 255 | + result = DateTimeUtil.getDefaultInputFormat('fr-FR', DataType.Time); |
| 256 | + expect(result).toEqual('HH:mm'); |
| 257 | + }); |
| 258 | + |
236 | 259 | it('should correctly distinguish date from time characters', () => {
|
237 | 260 | expect(DateTimeUtil.isDateOrTimeChar('d')).toBeTrue();
|
238 | 261 | expect(DateTimeUtil.isDateOrTimeChar('M')).toBeTrue();
|
@@ -628,4 +651,59 @@ describe(`DateTimeUtil Unit tests`, () => {
|
628 | 651 | expect(DateTimeUtil.isValidDate(false)).toBeFalse();
|
629 | 652 | expect(DateTimeUtil.isValidDate(true)).toBeFalse();
|
630 | 653 | });
|
| 654 | + |
| 655 | + it('should correctly identify formats that would resolve to only numeric parts (and period) for the date/time parts', () => { |
| 656 | + // test with locale covering non-ASCII characters as well |
| 657 | + const locale = 'bg'; |
| 658 | + |
| 659 | + const numericFormats = ['y', 'yy', 'yyy', 'yyyy', 'M', 'MM', 'd', 'dd', 'h', 'hh', |
| 660 | + 'H', 'HH', 'm', 'mm', 's', 'ss', 'S', 'SS', 'SSS', |
| 661 | + 'dd-MM-yyyy', 'dd/M/yyyy HH:mm:ss tt', 'dd/M/yyyy HH:mm:ss:SS a', |
| 662 | + // literals are allowed in the format |
| 663 | + 'dd/MM/yyyy test hh:mm' |
| 664 | + ]; |
| 665 | + numericFormats.forEach(format => { |
| 666 | + expect(DateTimeUtil.isFormatNumeric(locale, format)).withContext(`Format: ${format}`).toBeTrue(); |
| 667 | + }); |
| 668 | + |
| 669 | + const nonNumericFormats = ['MMM', 'MMMM', 'MMMMM', 'medium', 'long', 'full', 'mediumDate', |
| 670 | + 'longDate', 'fullDate', 'longTime', 'fullTime', 'dd-MMM-yyyy', 'E', 'EE']; |
| 671 | + |
| 672 | + nonNumericFormats.forEach(format => { |
| 673 | + expect(DateTimeUtil.isFormatNumeric(locale, format)).withContext(`Format: ${format}`).toBeFalse(); |
| 674 | + }); |
| 675 | + }); |
| 676 | + |
| 677 | + it('getNumericInputFormat should return formats with date parts that the date-time editors can handle', () => { |
| 678 | + let locale = 'en-US'; |
| 679 | + |
| 680 | + // returns the equivalent of the predefined numeric formats as date parts |
| 681 | + // should be transformed as inputFormats for editing (numeric year, 2-digit parts for the rest) |
| 682 | + expect(DateTimeUtil.getNumericInputFormat(locale, 'short')).toBe('MM/dd/yyyy, hh:mm tt'); |
| 683 | + expect(DateTimeUtil.getNumericInputFormat(locale, 'shortDate')).toBe('MM/dd/yyyy'); |
| 684 | + expect(DateTimeUtil.getNumericInputFormat(locale, 'shortTime').normalize('NFKD')).toBe('hh:mm tt'); |
| 685 | + expect(DateTimeUtil.getNumericInputFormat(locale, 'mediumTime').normalize('NFKD')).toBe('hh:mm:ss tt'); |
| 686 | + |
| 687 | + // handle the predefined formats for different locales |
| 688 | + locale = 'bg-BG'; |
| 689 | + expect(DateTimeUtil.getNumericInputFormat(locale, 'short').normalize('NFKD')).toBe('dd.MM.yyyy г., HH:mm'); |
| 690 | + expect(DateTimeUtil.getNumericInputFormat(locale, 'shortDate').normalize('NFKD')).toBe('dd.MM.yyyy г.'); |
| 691 | + expect(DateTimeUtil.getNumericInputFormat(locale, 'shortTime').normalize('NFKD')).toBe('HH:mm'); |
| 692 | + expect(DateTimeUtil.getNumericInputFormat(locale, 'mediumTime').normalize('NFKD')).toBe('HH:mm:ss'); |
| 693 | + |
| 694 | + locale = 'ja-JP'; |
| 695 | + expect(DateTimeUtil.getNumericInputFormat(locale, 'short')).toBe('yyyy/MM/dd HH:mm'); |
| 696 | + expect(DateTimeUtil.getNumericInputFormat(locale, 'shortDate')).toBe('yyyy/MM/dd'); |
| 697 | + expect(DateTimeUtil.getNumericInputFormat(locale, 'shortTime').normalize('NFKD')).toBe('HH:mm'); |
| 698 | + expect(DateTimeUtil.getNumericInputFormat(locale, 'mediumTime').normalize('NFKD')).toBe('HH:mm:ss'); |
| 699 | + |
| 700 | + // returns the same format if it is custom and numeric |
| 701 | + expect(DateTimeUtil.getNumericInputFormat(locale, 'dd-MM-yyyy')).toBe('dd-MM-yyyy'); |
| 702 | + expect(DateTimeUtil.getNumericInputFormat(locale, 'dd/M/yyyy hh:mm:ss:SS aa')).toBe('dd/M/yyyy hh:mm:ss:SS aa'); |
| 703 | + |
| 704 | + // returns empty string if predefined and not among the numeric ones |
| 705 | + expect(DateTimeUtil.getNumericInputFormat(locale, 'medium')).toBe(''); |
| 706 | + expect(DateTimeUtil.getNumericInputFormat(locale, 'mediumDate')).toBe(''); |
| 707 | + expect(DateTimeUtil.getNumericInputFormat(locale, 'longTime')).toBe(''); |
| 708 | + }); |
631 | 709 | });
|
0 commit comments