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';
|
2016-09-27 20:12:25 -04:00
|
|
|
import {beforeEach, describe, expect, it} from '@angular/core/testing/testing_internal';
|
2016-06-23 19:42:25 -04:00
|
|
|
import {browserDetection} from '@angular/platform-browser/testing/browser_util';
|
2015-07-04 14:04:54 -04:00
|
|
|
|
|
|
|
export function main() {
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('DatePipe', () => {
|
2016-06-17 13:57:32 -04:00
|
|
|
var date: Date;
|
|
|
|
var pipe: DatePipe;
|
2015-07-04 14:04:54 -04:00
|
|
|
|
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(); });
|
|
|
|
it('should support int', () => { expect(() => pipe.transform(123456789)).not.toThrow(); });
|
|
|
|
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',
|
|
|
|
() => { expect(() => pipe.transform('2015-06-15T21:43:11Z')).not.toThrow(); });
|
2016-06-17 13:57:32 -04:00
|
|
|
|
2016-08-31 18:55:13 -04:00
|
|
|
it('should not support other objects', () => {
|
|
|
|
expect(() => pipe.transform({})).toThrow();
|
|
|
|
expect(() => pipe.transform('')).toThrow();
|
2016-06-17 13:57:32 -04:00
|
|
|
});
|
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', () => {
|
|
|
|
expect(pipe.transform(date, 'y')).toEqual('2015');
|
|
|
|
expect(pipe.transform(date, 'yy')).toEqual('15');
|
|
|
|
expect(pipe.transform(date, 'M')).toEqual('6');
|
|
|
|
expect(pipe.transform(date, 'MM')).toEqual('06');
|
|
|
|
expect(pipe.transform(date, 'MMM')).toEqual('Jun');
|
|
|
|
expect(pipe.transform(date, 'MMMM')).toEqual('June');
|
|
|
|
expect(pipe.transform(date, 'd')).toEqual('15');
|
2016-10-19 13:05:13 -04:00
|
|
|
expect(pipe.transform(date, 'EEE')).toEqual('Mon');
|
2016-08-31 18:55:13 -04:00
|
|
|
expect(pipe.transform(date, 'EEEE')).toEqual('Monday');
|
|
|
|
if (!browserDetection.isOldChrome) {
|
2016-07-01 18:34:57 -04:00
|
|
|
expect(pipe.transform(date, 'h')).toEqual('9');
|
|
|
|
expect(pipe.transform(date, 'hh')).toEqual('09');
|
|
|
|
expect(pipe.transform(date, 'j')).toEqual('9 AM');
|
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) {
|
|
|
|
expect(pipe.transform(date, 'HH')).toEqual('09');
|
|
|
|
}
|
2016-10-19 13:05:13 -04:00
|
|
|
|
|
|
|
expect(pipe.transform(date, 'E')).toEqual('M');
|
|
|
|
expect(pipe.transform(date, 'L')).toEqual('J');
|
2016-06-18 12:03:58 -04:00
|
|
|
expect(pipe.transform(date, 'm')).toEqual('3');
|
|
|
|
expect(pipe.transform(date, 's')).toEqual('1');
|
|
|
|
expect(pipe.transform(date, 'mm')).toEqual('03');
|
|
|
|
expect(pipe.transform(date, 'ss')).toEqual('01');
|
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-10-19 13:05:13 -04:00
|
|
|
expect(pipe.transform(date, 'EEE, M/d/y')).toEqual('Mon, 6/15/2015');
|
|
|
|
expect(pipe.transform(date, 'EEE, M/d')).toEqual('Mon, 6/15');
|
2016-08-31 18:55:13 -04:00
|
|
|
expect(pipe.transform(date, 'MMM d')).toEqual('Jun 15');
|
|
|
|
expect(pipe.transform(date, 'dd/MM/yyyy')).toEqual('15/06/2015');
|
|
|
|
expect(pipe.transform(date, 'MM/dd/yyyy')).toEqual('06/15/2015');
|
2016-10-19 13:05:13 -04:00
|
|
|
expect(pipe.transform(date, 'yMEEEd')).toEqual('20156Mon15');
|
|
|
|
expect(pipe.transform(date, 'MEEEd')).toEqual('6Mon15');
|
2016-08-31 18:55:13 -04:00
|
|
|
expect(pipe.transform(date, 'MMMd')).toEqual('Jun15');
|
|
|
|
expect(pipe.transform(date, 'yMMMMEEEEd')).toEqual('Monday, June 15, 2015');
|
|
|
|
// IE and Edge can't format a date to minutes and seconds without hours
|
|
|
|
if (!browserDetection.isEdge && !browserDetection.isIE ||
|
|
|
|
!browserDetection.supportsNativeIntlApi) {
|
2016-06-18 12:03:58 -04:00
|
|
|
expect(pipe.transform(date, 'ms')).toEqual('31');
|
2016-08-31 18:55:13 -04:00
|
|
|
}
|
|
|
|
if (!browserDetection.isOldChrome) {
|
2016-07-01 18:34:57 -04:00
|
|
|
expect(pipe.transform(date, 'jm')).toEqual('9:03 AM');
|
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', () => {
|
|
|
|
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) {
|
|
|
|
expect(pipe.transform(date, 'medium')).toEqual('Jun 15, 2015 9:03:01 AM');
|
|
|
|
expect(pipe.transform(date, 'short')).toEqual('6/15/2015 9:03 AM');
|
|
|
|
} else {
|
|
|
|
expect(pipe.transform(date, 'medium')).toEqual('Jun 15, 2015, 9:03:01 AM');
|
|
|
|
expect(pipe.transform(date, 'short')).toEqual('6/15/2015, 9:03 AM');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expect(pipe.transform(date, 'MM/dd/yyyy')).toEqual('06/15/2015');
|
|
|
|
expect(pipe.transform(date, 'fullDate')).toEqual('Monday, June 15, 2015');
|
|
|
|
expect(pipe.transform(date, 'longDate')).toEqual('June 15, 2015');
|
|
|
|
expect(pipe.transform(date, 'mediumDate')).toEqual('Jun 15, 2015');
|
|
|
|
expect(pipe.transform(date, 'shortDate')).toEqual('6/15/2015');
|
|
|
|
if (!browserDetection.isOldChrome) {
|
2016-07-01 18:34:57 -04:00
|
|
|
expect(pipe.transform(date, 'mediumTime')).toEqual('9:03:01 AM');
|
|
|
|
expect(pipe.transform(date, 'shortTime')).toEqual('9:03 AM');
|
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 remove bidi control characters',
|
|
|
|
() => { expect(pipe.transform(date, 'MM/dd/yyyy').length).toEqual(10); });
|
|
|
|
});
|
2015-07-04 14:04:54 -04:00
|
|
|
});
|
|
|
|
}
|