| 
									
										
										
										
											2016-06-23 09:47:54 -07: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 16:38:52 -07:00
										 |  |  | import {DatePipe} from '@angular/common'; | 
					
						
							| 
									
										
										
										
											2017-05-18 13:46:51 -07:00
										 |  |  | import {JitReflector} from '@angular/compiler'; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | import {PipeResolver} from '@angular/compiler/src/pipe_resolver'; | 
					
						
							| 
									
										
										
										
											2017-03-02 12:12:46 -08:00
										 |  |  | import {browserDetection} from '@angular/platform-browser/testing/src/browser_util'; | 
					
						
							| 
									
										
										
										
											2015-07-04 22:34:54 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   describe('DatePipe', () => { | 
					
						
							| 
									
										
										
										
											2016-10-20 16:05:50 -07:00
										 |  |  |     let date: Date; | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02:00
										 |  |  |     const isoStringWithoutTime = '2015-01-01'; | 
					
						
							| 
									
										
										
										
											2016-10-20 16:05:50 -07:00
										 |  |  |     let pipe: DatePipe; | 
					
						
							| 
									
										
										
										
											2015-07-04 22:34:54 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02: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-09-01 00:55:13 +02: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 22:34:54 +04:30
										 |  |  |     beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2016-10-02 14:12:14 -07:00
										 |  |  |       date = new Date(2015, 5, 15, 9, 3, 1); | 
					
						
							| 
									
										
										
										
											2016-08-26 09:16:01 -07:00
										 |  |  |       pipe = new DatePipe('en-US'); | 
					
						
							| 
									
										
										
										
											2015-07-04 22:34:54 +04:30
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-18 13:46:51 -07:00
										 |  |  |     it('should be marked as pure', () => { | 
					
						
							|  |  |  |       expect(new PipeResolver(new JitReflector()).resolve(DatePipe) !.pure).toEqual(true); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-10-27 12:43:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |     describe('supports', () => { | 
					
						
							|  |  |  |       it('should support date', () => { expect(() => pipe.transform(date)).not.toThrow(); }); | 
					
						
							| 
									
										
										
										
											2016-10-20 16:05:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |       it('should support int', () => { expect(() => pipe.transform(123456789)).not.toThrow(); }); | 
					
						
							| 
									
										
										
										
											2016-10-20 16:05:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |       it('should support numeric strings', | 
					
						
							|  |  |  |          () => { expect(() => pipe.transform('123456789')).not.toThrow(); }); | 
					
						
							| 
									
										
										
										
											2016-06-10 00:32:36 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |       it('should support decimal strings', | 
					
						
							|  |  |  |          () => { expect(() => pipe.transform('123456789.11')).not.toThrow(); }); | 
					
						
							| 
									
										
										
										
											2016-08-14 21:05:21 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |       it('should support ISO string', | 
					
						
							| 
									
										
										
										
											2016-11-09 02:45:12 +03:00
										 |  |  |          () => expect(() => pipe.transform('2015-06-15T21:43:11Z')).not.toThrow()); | 
					
						
							| 
									
										
										
										
											2016-06-17 10:57:32 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-09 02:45:12 +03:00
										 |  |  |       it('should return null for empty string', () => expect(pipe.transform('')).toEqual(null)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-26 18:23:53 +03:00
										 |  |  |       it('should return null for NaN', () => expect(pipe.transform(Number.NaN)).toEqual(null)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02:00
										 |  |  |       it('should support ISO string without time', | 
					
						
							|  |  |  |          () => { expect(() => pipe.transform(isoStringWithoutTime)).not.toThrow(); }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-09 02:45:12 +03:00
										 |  |  |       it('should not support other objects', | 
					
						
							| 
									
										
										
										
											2017-01-27 13:19:00 -08:00
										 |  |  |          () => expect(() => pipe.transform({})).toThrowError(/InvalidPipeArgument/)); | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-06-17 10:57:32 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |     describe('transform', () => { | 
					
						
							|  |  |  |       it('should format each component correctly', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const dateFixtures: any = { | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02:00
										 |  |  |           'y': '2015', | 
					
						
							|  |  |  |           'yy': '15', | 
					
						
							|  |  |  |           'M': '6', | 
					
						
							|  |  |  |           'MM': '06', | 
					
						
							|  |  |  |           'MMM': 'Jun', | 
					
						
							|  |  |  |           'MMMM': 'June', | 
					
						
							|  |  |  |           'd': '15', | 
					
						
							|  |  |  |           'dd': '15', | 
					
						
							|  |  |  |           'EEE': 'Mon', | 
					
						
							|  |  |  |           'EEEE': 'Monday' | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const isoStringWithoutTimeFixtures: any = { | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02:00
										 |  |  |           'y': '2015', | 
					
						
							|  |  |  |           'yy': '15', | 
					
						
							|  |  |  |           'M': '1', | 
					
						
							|  |  |  |           'MM': '01', | 
					
						
							|  |  |  |           'MMM': 'Jan', | 
					
						
							|  |  |  |           'MMMM': 'January', | 
					
						
							|  |  |  |           'd': '1', | 
					
						
							|  |  |  |           'dd': '01', | 
					
						
							|  |  |  |           'EEE': 'Thu', | 
					
						
							|  |  |  |           'EEEE': 'Thursday' | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |         if (!browserDetection.isOldChrome) { | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02:00
										 |  |  |           dateFixtures['h'] = '9'; | 
					
						
							|  |  |  |           dateFixtures['hh'] = '09'; | 
					
						
							|  |  |  |           dateFixtures['j'] = '9 AM'; | 
					
						
							|  |  |  |           isoStringWithoutTimeFixtures['h'] = '12'; | 
					
						
							|  |  |  |           isoStringWithoutTimeFixtures['hh'] = '12'; | 
					
						
							|  |  |  |           isoStringWithoutTimeFixtures['j'] = '12 AM'; | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02: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 20:57:04 -02:00
										 |  |  |             dateFixtures['HH'] = '09'; | 
					
						
							|  |  |  |             isoStringWithoutTimeFixtures['HH'] = '00'; | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |           } | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02: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-09-01 00:55:13 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Object.keys(dateFixtures).forEach((pattern: string) => { | 
					
						
							|  |  |  |           expectDateFormatAs(date, pattern, dateFixtures[pattern]); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-03 00:51:59 +02:00
										 |  |  |         if (!browserDetection.isOldChrome) { | 
					
						
							|  |  |  |           Object.keys(isoStringWithoutTimeFixtures).forEach((pattern: string) => { | 
					
						
							|  |  |  |             expectDateFormatAs( | 
					
						
							|  |  |  |                 isoStringWithoutTime, pattern, isoStringWithoutTimeFixtures[pattern]); | 
					
						
							|  |  |  |           }); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |         expect(pipe.transform(date, 'Z')).toBeDefined(); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-07-02 01:34:57 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |       it('should format common multi component patterns', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const dateFixtures: any = { | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02: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-09-01 00:55:13 +02: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 20:57:04 -02:00
										 |  |  |           dateFixtures['ms'] = '31'; | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |         if (!browserDetection.isOldChrome) { | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02:00
										 |  |  |           dateFixtures['jm'] = '9:03 AM'; | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Object.keys(dateFixtures).forEach((pattern: string) => { | 
					
						
							|  |  |  |           expectDateFormatAs(date, pattern, dateFixtures[pattern]); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-07-04 22:34:54 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |       it('should format with pattern aliases', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |         const dateFixtures: any = { | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02: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-09-01 00:55:13 +02: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 20:57:04 -02:00
										 |  |  |             dateFixtures['medium'] = 'Jun 15, 2015 9:03:01 AM'; | 
					
						
							|  |  |  |             dateFixtures['short'] = '6/15/2015 9:03 AM'; | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |           } else { | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02:00
										 |  |  |             dateFixtures['medium'] = 'Jun 15, 2015, 9:03:01 AM'; | 
					
						
							|  |  |  |             dateFixtures['short'] = '6/15/2015, 9:03 AM'; | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |         if (!browserDetection.isOldChrome) { | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02:00
										 |  |  |           dateFixtures['mediumTime'] = '9:03:01 AM'; | 
					
						
							|  |  |  |           dateFixtures['shortTime'] = '9:03 AM'; | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-11-10 20:57:04 -02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Object.keys(dateFixtures).forEach((pattern: string) => { | 
					
						
							|  |  |  |           expectDateFormatAs(date, pattern, dateFixtures[pattern]); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-04 22:34:54 +04:30
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-30 21:44:19 +03: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-09-01 00:55:13 +02:00
										 |  |  |       it('should remove bidi control characters', | 
					
						
							| 
									
										
										
										
											2017-03-24 09:54:02 -07:00
										 |  |  |          () => expect(pipe.transform(date, 'MM/dd/yyyy') !.length).toEqual(10)); | 
					
						
							| 
									
										
										
										
											2016-09-01 00:55:13 +02:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-07-04 22:34:54 +04:30
										 |  |  |   }); | 
					
						
							|  |  |  | } |