2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
import {DatePipe} from '@angular/common';
|
|
|
|
import {PipeResolver} from '@angular/compiler/src/pipe_resolver';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {browserDetection} from '@angular/platform-browser/testing/src/browser_util';
|
2015-07-04 14:04:54 -04:00
|
|
|
|
|
|
|
export function main() {
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('DatePipe', () => {
|
2016-10-20 19:05:50 -04:00
|
|
|
let date: Date;
|
2016-11-10 17:57:04 -05:00
|
|
|
const isoStringWithoutTime = '2015-01-01';
|
2016-10-20 19:05:50 -04:00
|
|
|
let pipe: DatePipe;
|
2015-07-04 14:04:54 -04:00
|
|
|
|
2016-11-10 17:57:04 -05:00
|
|
|
// Check the transformation of a date into a pattern
|
|
|
|
function expectDateFormatAs(date: Date | string, pattern: any, output: string): void {
|
|
|
|
expect(pipe.transform(date, pattern)).toEqual(output);
|
|
|
|
}
|
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
// TODO: reactivate the disabled expectations once emulators are fixed in SauceLabs
|
|
|
|
// In some old versions of Chrome in Android emulators, time formatting returns dates in the
|
|
|
|
// timezone of the VM host,
|
|
|
|
// instead of the device timezone. Same symptoms as
|
|
|
|
// https://bugs.chromium.org/p/chromium/issues/detail?id=406382
|
|
|
|
// This happens locally and in SauceLabs, so some checks are disabled to avoid failures.
|
|
|
|
// Tracking issue: https://github.com/angular/angular/issues/11187
|
|
|
|
|
2015-07-04 14:04:54 -04:00
|
|
|
beforeEach(() => {
|
2016-10-02 17:12:14 -04:00
|
|
|
date = new Date(2015, 5, 15, 9, 3, 1);
|
2016-08-26 12:16:01 -04:00
|
|
|
pipe = new DatePipe('en-US');
|
2015-07-04 14:04:54 -04:00
|
|
|
});
|
|
|
|
|
2015-10-27 19:37:08 -04:00
|
|
|
it('should be marked as pure',
|
|
|
|
() => { expect(new PipeResolver().resolve(DatePipe).pure).toEqual(true); });
|
2015-10-27 15:43:34 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
describe('supports', () => {
|
|
|
|
it('should support date', () => { expect(() => pipe.transform(date)).not.toThrow(); });
|
2016-10-20 19:05:50 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
it('should support int', () => { expect(() => pipe.transform(123456789)).not.toThrow(); });
|
2016-10-20 19:05:50 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
it('should support numeric strings',
|
|
|
|
() => { expect(() => pipe.transform('123456789')).not.toThrow(); });
|
2016-06-09 17:32:36 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
it('should support decimal strings',
|
|
|
|
() => { expect(() => pipe.transform('123456789.11')).not.toThrow(); });
|
2016-08-14 13:05:21 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
it('should support ISO string',
|
2016-11-08 18:45:12 -05:00
|
|
|
() => expect(() => pipe.transform('2015-06-15T21:43:11Z')).not.toThrow());
|
2016-06-17 13:57:32 -04:00
|
|
|
|
2016-11-08 18:45:12 -05:00
|
|
|
it('should return null for empty string', () => expect(pipe.transform('')).toEqual(null));
|
|
|
|
|
2017-01-26 10:23:53 -05:00
|
|
|
it('should return null for NaN', () => expect(pipe.transform(Number.NaN)).toEqual(null));
|
|
|
|
|
2016-11-10 17:57:04 -05:00
|
|
|
it('should support ISO string without time',
|
|
|
|
() => { expect(() => pipe.transform(isoStringWithoutTime)).not.toThrow(); });
|
|
|
|
|
2016-11-08 18:45:12 -05:00
|
|
|
it('should not support other objects',
|
2017-01-27 16:19:00 -05:00
|
|
|
() => expect(() => pipe.transform({})).toThrowError(/InvalidPipeArgument/));
|
2016-08-31 18:55:13 -04:00
|
|
|
});
|
2016-06-17 13:57:32 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
describe('transform', () => {
|
|
|
|
it('should format each component correctly', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const dateFixtures: any = {
|
2016-11-10 17:57:04 -05:00
|
|
|
'y': '2015',
|
|
|
|
'yy': '15',
|
|
|
|
'M': '6',
|
|
|
|
'MM': '06',
|
|
|
|
'MMM': 'Jun',
|
|
|
|
'MMMM': 'June',
|
|
|
|
'd': '15',
|
|
|
|
'dd': '15',
|
|
|
|
'EEE': 'Mon',
|
|
|
|
'EEEE': 'Monday'
|
|
|
|
};
|
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const isoStringWithoutTimeFixtures: any = {
|
2016-11-10 17:57:04 -05:00
|
|
|
'y': '2015',
|
|
|
|
'yy': '15',
|
|
|
|
'M': '1',
|
|
|
|
'MM': '01',
|
|
|
|
'MMM': 'Jan',
|
|
|
|
'MMMM': 'January',
|
|
|
|
'd': '1',
|
|
|
|
'dd': '01',
|
|
|
|
'EEE': 'Thu',
|
|
|
|
'EEEE': 'Thursday'
|
|
|
|
};
|
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
if (!browserDetection.isOldChrome) {
|
2016-11-10 17:57:04 -05:00
|
|
|
dateFixtures['h'] = '9';
|
|
|
|
dateFixtures['hh'] = '09';
|
|
|
|
dateFixtures['j'] = '9 AM';
|
|
|
|
isoStringWithoutTimeFixtures['h'] = '12';
|
|
|
|
isoStringWithoutTimeFixtures['hh'] = '12';
|
|
|
|
isoStringWithoutTimeFixtures['j'] = '12 AM';
|
2016-08-31 18:55:13 -04:00
|
|
|
}
|
2016-11-10 17:57:04 -05:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
// IE and Edge can't format a date to minutes and seconds without hours
|
|
|
|
if (!browserDetection.isEdge && !browserDetection.isIE ||
|
|
|
|
!browserDetection.supportsNativeIntlApi) {
|
|
|
|
if (!browserDetection.isOldChrome) {
|
2016-11-10 17:57:04 -05:00
|
|
|
dateFixtures['HH'] = '09';
|
|
|
|
isoStringWithoutTimeFixtures['HH'] = '00';
|
2016-08-31 18:55:13 -04:00
|
|
|
}
|
2016-11-10 17:57:04 -05:00
|
|
|
dateFixtures['E'] = 'M';
|
|
|
|
dateFixtures['L'] = 'J';
|
|
|
|
dateFixtures['m'] = '3';
|
|
|
|
dateFixtures['s'] = '1';
|
|
|
|
dateFixtures['mm'] = '03';
|
|
|
|
dateFixtures['ss'] = '01';
|
|
|
|
isoStringWithoutTimeFixtures['m'] = '0';
|
|
|
|
isoStringWithoutTimeFixtures['s'] = '0';
|
|
|
|
isoStringWithoutTimeFixtures['mm'] = '00';
|
|
|
|
isoStringWithoutTimeFixtures['ss'] = '00';
|
2016-08-31 18:55:13 -04:00
|
|
|
}
|
2016-11-10 17:57:04 -05:00
|
|
|
|
|
|
|
Object.keys(dateFixtures).forEach((pattern: string) => {
|
|
|
|
expectDateFormatAs(date, pattern, dateFixtures[pattern]);
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.keys(isoStringWithoutTimeFixtures).forEach((pattern: string) => {
|
|
|
|
expectDateFormatAs(isoStringWithoutTime, pattern, isoStringWithoutTimeFixtures[pattern]);
|
|
|
|
});
|
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
expect(pipe.transform(date, 'Z')).toBeDefined();
|
|
|
|
});
|
2016-07-01 18:34:57 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
it('should format common multi component patterns', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const dateFixtures: any = {
|
2016-11-10 17:57:04 -05:00
|
|
|
'EEE, M/d/y': 'Mon, 6/15/2015',
|
|
|
|
'EEE, M/d': 'Mon, 6/15',
|
|
|
|
'MMM d': 'Jun 15',
|
|
|
|
'dd/MM/yyyy': '15/06/2015',
|
|
|
|
'MM/dd/yyyy': '06/15/2015',
|
|
|
|
'yMEEEd': '20156Mon15',
|
|
|
|
'MEEEd': '6Mon15',
|
|
|
|
'MMMd': 'Jun15',
|
|
|
|
'yMMMMEEEEd': 'Monday, June 15, 2015'
|
|
|
|
};
|
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
// IE and Edge can't format a date to minutes and seconds without hours
|
|
|
|
if (!browserDetection.isEdge && !browserDetection.isIE ||
|
|
|
|
!browserDetection.supportsNativeIntlApi) {
|
2016-11-10 17:57:04 -05:00
|
|
|
dateFixtures['ms'] = '31';
|
2016-08-31 18:55:13 -04:00
|
|
|
}
|
2016-11-10 17:57:04 -05:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
if (!browserDetection.isOldChrome) {
|
2016-11-10 17:57:04 -05:00
|
|
|
dateFixtures['jm'] = '9:03 AM';
|
2016-08-31 18:55:13 -04:00
|
|
|
}
|
2016-11-10 17:57:04 -05:00
|
|
|
|
|
|
|
Object.keys(dateFixtures).forEach((pattern: string) => {
|
|
|
|
expectDateFormatAs(date, pattern, dateFixtures[pattern]);
|
|
|
|
});
|
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
});
|
2015-07-04 14:04:54 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
it('should format with pattern aliases', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const dateFixtures: any = {
|
2016-11-10 17:57:04 -05:00
|
|
|
'MM/dd/yyyy': '06/15/2015',
|
|
|
|
'fullDate': 'Monday, June 15, 2015',
|
|
|
|
'longDate': 'June 15, 2015',
|
|
|
|
'mediumDate': 'Jun 15, 2015',
|
|
|
|
'shortDate': '6/15/2015'
|
|
|
|
};
|
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
if (!browserDetection.isOldChrome) {
|
|
|
|
// IE and Edge do not add a coma after the year in these 2 cases
|
|
|
|
if ((browserDetection.isEdge || browserDetection.isIE) &&
|
|
|
|
browserDetection.supportsNativeIntlApi) {
|
2016-11-10 17:57:04 -05:00
|
|
|
dateFixtures['medium'] = 'Jun 15, 2015 9:03:01 AM';
|
|
|
|
dateFixtures['short'] = '6/15/2015 9:03 AM';
|
2016-08-31 18:55:13 -04:00
|
|
|
} else {
|
2016-11-10 17:57:04 -05:00
|
|
|
dateFixtures['medium'] = 'Jun 15, 2015, 9:03:01 AM';
|
|
|
|
dateFixtures['short'] = '6/15/2015, 9:03 AM';
|
2016-08-31 18:55:13 -04:00
|
|
|
}
|
|
|
|
}
|
2016-11-10 17:57:04 -05:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
if (!browserDetection.isOldChrome) {
|
2016-11-10 17:57:04 -05:00
|
|
|
dateFixtures['mediumTime'] = '9:03:01 AM';
|
|
|
|
dateFixtures['shortTime'] = '9:03 AM';
|
2016-08-31 18:55:13 -04:00
|
|
|
}
|
2016-11-10 17:57:04 -05:00
|
|
|
|
|
|
|
Object.keys(dateFixtures).forEach((pattern: string) => {
|
|
|
|
expectDateFormatAs(date, pattern, dateFixtures[pattern]);
|
|
|
|
});
|
|
|
|
|
2015-07-04 14:04:54 -04:00
|
|
|
});
|
2016-08-31 18:55:13 -04:00
|
|
|
|
2017-01-30 13:44:19 -05:00
|
|
|
it('should format invalid in IE ISO date',
|
|
|
|
() => expect(pipe.transform('2017-01-11T09:25:14.014-0500')).toEqual('Jan 11, 2017'));
|
|
|
|
|
|
|
|
it('should format invalid in Safari ISO date',
|
|
|
|
() => expect(pipe.transform('2017-01-20T19:00:00+0000')).toEqual('Jan 20, 2017'));
|
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
it('should remove bidi control characters',
|
2017-01-30 13:44:19 -05:00
|
|
|
() => expect(pipe.transform(date, 'MM/dd/yyyy').length).toEqual(10));
|
2016-08-31 18:55:13 -04:00
|
|
|
});
|
2015-07-04 14:04:54 -04:00
|
|
|
});
|
|
|
|
}
|