test(datetime): add unit tests for parseDate()

This commit is contained in:
Manu Mtz.-Almeida
2018-12-05 18:26:39 +01:00
committed by Manu MA
parent aa9d0d4282
commit e75618e82f
2 changed files with 68 additions and 2 deletions

View File

@ -1,4 +1,4 @@
import { convertDataToISO } from './datetime-util';
import { convertDataToISO, parseDate } from './datetime-util';
describe('datetime-util', () => {
describe('convertDataToISO', () => {
@ -207,4 +207,70 @@ describe('datetime-util', () => {
});
});
});
describe('parseDate', () => {
it('should parse a single year', () => {
const date = parseDate('1000');
expect(date).toEqual({
"day": undefined,
"hour": undefined,
"millisecond": undefined,
"minute": undefined,
"month": undefined,
"second": undefined,
"tzOffset": 0,
"year": 1000,
});
});
it('should parse a time', () => {
const date = parseDate('12:20');
expect(date).toEqual({
"day": undefined,
"hour": 12,
"millisecond": undefined,
"minute": 20,
"month": undefined,
"second": undefined,
"tzOffset": 0,
"year": undefined,
});
});
it('should parse a full ISO date', () => {
const date = parseDate('1994-12-15T13:47:20Z');
expect(date).toEqual({
"day": 15,
"hour": 13,
"millisecond": undefined,
"minute": 47,
"month": 12,
"second": 20,
"tzOffset": 0,
"year": 1994,
});
});
it('should parse a partial ISO date', () => {
const date = parseDate('2018-01-02');
expect(date).toEqual({
"day": 2,
"hour": undefined,
"millisecond": undefined,
"minute": undefined,
"month": 1,
"second": undefined,
"tzOffset": 0,
"year": 2018,
});
});
it('should return undefined', () => {
expect(parseDate(null)).toBeUndefined();
expect(parseDate(undefined)).toBeUndefined();
expect(parseDate('')).toBeUndefined();
expect(parseDate('3432-12-12-234')).toBeUndefined();
});
});
});

View File

@ -178,7 +178,7 @@ export function parseDate(val: string | undefined | null): DatetimeData | undefi
// ISO 8601 format: 1994-12-15T13:47:20Z
let parse: any[] | null = null;
if (val != null) {
if (val) {
// try parsing for just time first, HH:MM
parse = TIME_REGEXP.exec(val);
if (parse) {