Last active
November 1, 2016 03:12
-
-
Save gary-b/bdc67fae4767c7da08ee93965a955fa5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 | |
*/ | |
import {DatePipe} from '@angular/common'; | |
import {PipeResolver} from '@angular/compiler/src/pipe_resolver'; | |
import {beforeEach, describe, expect, it} from '@angular/core/testing/testing_internal'; | |
import {browserDetection} from '@angular/platform-browser/testing/browser_util'; | |
export function main() { | |
describe('DatePipe Locale Support', () => { | |
var date: Date; | |
var pipe: DatePipe; | |
beforeEach(() => { | |
date = new Date(2015, 5, 15, 9, 3, 1); | |
pipe = new DatePipe('en-US'); | |
}); | |
describe('transform', () => { | |
/* Results from Chrome and IE recorded in 2nd parameter to matcher function */ | |
it('should output the day part correctly when locale prefixes the hour with it', () => { | |
pipe = new DatePipe('ar'); | |
// this test doesnt fail, Intl.DateTimeFormat output is "٩ ص" | |
//"٩ ص".indexOf("ص") | |
// = 2 in chrome - (so unicodes writing direction support avoids any issue) | |
expect(pipe.transform(date, 'a')).toEqual('ص', 'Locale: ar, Chrome:ok, IE:ok'); | |
pipe = new DatePipe('zh'); | |
// theres no space in Intl.DateTimeFormat output at all on chrome (上午9时), but IE it is "上午 9", | |
// cldr says h:"ah时" - no space. Unicode cldr page states Chrome uses it | |
expect(pipe.transform(date, 'a')).toEqual('上午', 'Locale:zh, Chrome:undefined, IE:9'); | |
pipe = new DatePipe('he'); | |
// IE/edge Intl.DateTimeFormat uses 09 AM format, chrome = "9 לפנה״צ" | |
// "9 לפנה״צ".indexOf('לפנה״צ') | |
// = 3 in chrome - so writing direction support avoids the issue | |
expect(pipe.transform(date, 'a')).toEqual('לפנה״צ', 'Locale: he, Chrome:ok, IE:AM'); | |
pipe = new DatePipe('hu'); | |
expect(pipe.transform(date, 'a')).toEqual('de.', 'Locale: hu, Chrome:9. IE:ok'); | |
pipe = new DatePipe('ja'); | |
//again no space in Intl.DateTimeFormat output 午前9時 on chrome, IE outputs ""午前 9" | |
//the 24 hour output of the browser is 9時 for japanese | |
expect(pipe.transform(date, 'a')).toEqual('午前', 'Locale: ja, Chrome:undefined, IE:9'); | |
pipe = new DatePipe('ko'); | |
// chrome Intl.DateTimeFormat: "오전 9시" IE:"오전 9" | |
expect(pipe.transform(date, 'a')).toEqual('오전', 'Locale: ko, Chrome:9시, IE:9'); | |
pipe = new DatePipe('ta'); | |
expect(pipe.transform(date, 'a')).toEqual('முற்பகல்', 'Locale: ta, Chrome:9, IE:09'); | |
pipe = new DatePipe('tr'); | |
//chrome Intl.DateTimeFormat: "ÖÖ 9" IE: "9 ÖÖ" | |
expect(pipe.transform(date, 'a')).toEqual('ÖÖ', 'Locale: tr, Chrome:9, IE: ok'); | |
}); | |
it('should output the day part correctly when locale uses multiple words', () => { | |
pipe = new DatePipe('sr'); | |
// Serbian can be written in Cryllic or Latin alphabets, though the IE result should be "pre podne" | |
expect(pipe.transform(date, 'a')).toEqual('пре подне', 'Locale: sr, Chrome:npe, IE: pre'); | |
pipe = new DatePipe('pt-PT'); | |
// chrome Intl.DateTimeFormat: "9 da manhã" | |
// IE Intl.DateTimeFormat: "9 "<- doesnt output a day part at all | |
expect(pipe.transform(date, 'a')).toEqual('da manhã', 'Locale: pt-PT, Chrome:da, IE:"", '); | |
pipe = new DatePipe('es'); | |
// chrome Intl.DateTimeFormat: "9 a. m.", IE: "9 "<- doesnt output a day part at all | |
expect(pipe.transform(date, 'a')).toEqual('a. m.', 'Locale: es, Chrome:a. IE:""'); | |
}); | |
it('should output the day part correctly when number has 0 prefix', () => { | |
pipe = new DatePipe('lt'); | |
expect(pipe.transform(date, 'a')).toEqual('priešpiet', 'Locale: lt,Chrome:ok, IE:ok'); | |
}); | |
// Note for the following tests: | |
// There is a bug in IE/edge where it outputs minute part when requesting hour alone | |
// This seems to show up in most of the remaining tests | |
it('should use locales 0 symbol when padding numbers to 2 characters', () => { | |
// My uncommitted version HH uses custom code (repeated for all the unique numeral symbols i found on chrome) | |
pipe = new DatePipe('ar'); | |
expect(pipe.transform(date, 'HH')).toEqual('٠٩', 'Locale: ar, Chrome:ok, IE:٠٩:00'); | |
pipe = new DatePipe('bn'); | |
// chrome Intl.DateTimeFormat:"৯ am" IE:"পুর্বাহ্ন 09" - chrome uses Bengali numerals, IE western arabic and day parts different | |
expect(pipe.transform(date, 'HH')).toEqual('০৯', 'Locale: bn, Chrome:ok, IE:09:00'); | |
pipe = new DatePipe('mr'); | |
expect(pipe.transform(date, 'HH')).toEqual('०९', 'Locale: mr, Chrome:ok, IE:09:00'); | |
// hh uses the browser to get 2 digits, theres a fallback check in angular code however | |
// to pad the number if the browser didnt | |
// chrome Intl.DateTimeFormat: "९ म.पू.", IE: "म.पू. 09" | |
// THE BROWSERS LOCATE THE DAY PARTS IN DIFFERENT POSITIONS and use different numeral systems | |
expect(pipe.transform(date, 'hh')).toEqual('०९', 'Locale: mr, Chrome:0९, IE:म.पू.'); | |
}); | |
it('should output short timezone correctly when locale outputs hour postfix', () => { | |
pipe = new DatePipe('ar'); | |
// as mentioned before arabic usually ok due to unicode writing direction support | |
expect(pipe.transform(date, 'Z')).toEqual('جرينتش+١', 'Locale: ar, Chrome: ok, IE:00'); | |
pipe = new DatePipe('zh'); | |
// theres no space in chromes Intl.DateTimeFormat output at all: GMT+19时 | |
expect(pipe.transform(date, 'Z')).toEqual('GMT+1', 'Locale: zh, Chrome:+19时, IE:00'); | |
}); | |
it('should output short timezone correctly when locale includes hour signifier like 09 h UTC', () => { | |
//chromes Intl.DateTimeFormat output: 09 h UTC | |
pipe = new DatePipe('fr'); | |
expect(pipe.transform(date, 'Z')).toEqual('UTC+1', 'Locale: fr, Chrome: h UTC+1, IE:00'); | |
//chromes Intl.DateTimeFormat output: 09 Uhr GMT | |
pipe = new DatePipe('de'); | |
expect(pipe.transform(date, 'Z')).toEqual('GMT+1', 'Locale: de, Chrome: Uhr GMT+1, IE:00'); | |
}); | |
it('should output long timezone correctly when locale outputs hour postfix', () => { | |
pipe = new DatePipe('ar'); | |
// as mentioned before arabic usually ok due to unicode writing direction support | |
expect(pipe.transform(date, 'z')).toEqual('توقيت بريطانيا الصيفي', 'Locale: ar, chrome: ok, IE:00'); | |
pipe = new DatePipe('zh'); | |
// again, no spaces in chromes Intl.DateTimeFormat output: 英国夏令时间9时 | |
expect(pipe.transform(date, 'z')).toEqual('英国夏令时间', 'Locale: zh,Chrome: 令时间9时, IE:00'); | |
}); | |
it('should output long timezone correctly when locale includes hour signifier like 03 h UTC', () => { | |
pipe = new DatePipe('fr'); | |
expect(pipe.transform(date, 'z')).toEqual('heure d’été britannique', 'Locale: fr, chrome:h heure d’été britannique, IE:00'); | |
pipe = new DatePipe('de'); | |
expect(pipe.transform(date, 'z')).toEqual('Britische Sommerzeit', 'Locale: de, chrome:Uhr Britische Sommerzeit, IE:00'); | |
}); | |
it('should output era correctly in english', () => { | |
// i couldnt get IE Intl.DateTimeFormat to ever output the era in my tests | |
// chrome doesnt output it on its own | |
expect(pipe.transform(date, 'G')).toEqual('A', 'chrome:6 15, 2015 A, IE:6/15/2015'); | |
expect(pipe.transform(date, 'GGG')).toEqual('AD', 'chrome:6 15, 2015 AD, IE:6/15/2015'); | |
expect(pipe.transform(date, 'GGGG')).toEqual('Anno Domini', 'chrome:6 15, 2015 Anno Domini, IE:6/15/2015'); | |
}); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment