mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-24 23:01:57 +08:00
test(datetime): add unit tests for parseDate()
This commit is contained in:

committed by
Manu MA

parent
aa9d0d4282
commit
e75618e82f
@ -1,4 +1,4 @@
|
|||||||
import { convertDataToISO } from './datetime-util';
|
import { convertDataToISO, parseDate } from './datetime-util';
|
||||||
|
|
||||||
describe('datetime-util', () => {
|
describe('datetime-util', () => {
|
||||||
describe('convertDataToISO', () => {
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -178,7 +178,7 @@ export function parseDate(val: string | undefined | null): DatetimeData | undefi
|
|||||||
// ISO 8601 format: 1994-12-15T13:47:20Z
|
// ISO 8601 format: 1994-12-15T13:47:20Z
|
||||||
let parse: any[] | null = null;
|
let parse: any[] | null = null;
|
||||||
|
|
||||||
if (val != null) {
|
if (val) {
|
||||||
// try parsing for just time first, HH:MM
|
// try parsing for just time first, HH:MM
|
||||||
parse = TIME_REGEXP.exec(val);
|
parse = TIME_REGEXP.exec(val);
|
||||||
if (parse) {
|
if (parse) {
|
||||||
|
Reference in New Issue
Block a user