33import assert from 'assert' ;
44import timeStampTests from './test-data/time-stamp-fixtures.js' ;
55import localeTests from './test-data/locale-test-fixtures.js' ;
6+ import formatToPartTests from './test-data/format-to-parts-fixtures.js' ;
67import polyfill from '../src/code/polyfill.js' ;
78import dataLoader from '../src/code/data-loader.js' ;
89import tzdataMoonLanding from './test-data/tzdata-moon-nearside.js' ;
910import tzdata from '../src/data/tzdata.js' ;
1011import localeData from '../src/data/locale.js' ;
1112import metazone from '../src/data/metazone.js' ;
1213
13- const isNode = ( typeof global !== " undefined" && { } . toString . call ( global ) === '[object global]' ) ;
14+ const isNode = ( typeof global !== ' undefined' && { } . toString . call ( global ) === '[object global]' ) ;
1415const myGlobal = ( isNode ) ? global : window ;
1516
1617
@@ -21,29 +22,63 @@ tzdata(myGlobal); // Loads timezone iana data in memory
2122localeData ( myGlobal ) ; // Loads timezone CLDR data in memory
2223tzdataMoonLanding ( myGlobal ) ;
2324
24- function formatAssert ( actual ) {
25+ function formatAssert ( expected , actual ) {
26+ // Browsers do not follow number formating in consistent way.
27+ // This causes a mismatch between rest of the formatting.
28+ // Our test in 'test-complete.js' does test actual match,
29+ // but here we only ensure that no exception happening, while formatting.
2530 assert ( actual , 'must not be a falsy value' ) ;
2631}
2732
28- describe ( 'Polyfill with complete package' , ( ) => {
33+ if ( myGlobal . Intl . __disableRegExpRestore ) {
34+ myGlobal . Intl . __disableRegExpRestore ( ) ;
35+ }
36+
37+ describe ( 'SauceLabs# ' , ( ) => {
2938 describe ( 'DateTimeFormat' , ( ) => {
3039 describe ( 'Instanceof integrity' , ( ) => {
31- it ( 'nativedDateTimeFormat instanceof Intl.DateTimeFormat' , ( ) => {
32- const nativedDateTimeFormat = new Intl . DateTimeFormat ( 'en' , {
40+ it ( 'native DateTimeFormat instanceof Intl.DateTimeFormat' , ( ) => {
41+ const nativeDateTimeFormat = new Intl . DateTimeFormat ( 'en' , {
3342 timeZone : 'America/Los_Angeles'
3443 } ) ;
35- assert . equal ( nativedDateTimeFormat instanceof Intl . DateTimeFormat , true ) ;
44+ assert . equal ( nativeDateTimeFormat instanceof Intl . DateTimeFormat , true ) ;
3645 } ) ;
3746
38- it ( 'polyfilledDateTimeFormat instanceof Intl.DateTimeFormat' , ( ) => {
47+ it ( 'polyfilled DateTimeFormat instanceof Intl.DateTimeFormat' , ( ) => {
3948 const polyfilledDateTimeFormat = new Intl . DateTimeFormat ( 'en' , {
4049 timeZone : 'Moon/Nearside'
4150 } ) ;
4251 assert . equal ( polyfilledDateTimeFormat instanceof Intl . DateTimeFormat , true ) ;
4352 } ) ;
4453 } ) ;
4554
46- describe ( '.format(locale, option)' , ( ) => {
55+ describe . skip ( '.formatToParts(date)' , ( ) => {
56+ it ( 'polyfilled DateTimeFormat should implement iff native DateTimeFormat implemented it' , ( ) => {
57+ const nativeDateTimeFormat = new Intl . DateTimeFormat ( 'en' , { timeZone : 'UTC' } ) ; // UTC is always implemented
58+ const polyfilledDateTimeFormat = new Intl . DateTimeFormat ( 'en' , { timeZone : 'Moon/Nearside' } ) ; // Moon/Nearside can never be implemented.
59+ assert ( ! ( ( polyfilledDateTimeFormat . formatToParts ) ^ ( nativeDateTimeFormat . formatToParts ) ) , 'formatToParts implementation mismatched' ) ;
60+ } ) ;
61+
62+ if ( ! new Intl . DateTimeFormat ( 'en' , {
63+ timeZone : 'Moon/Nearside'
64+ } ) . formatToParts ) {
65+ return ;
66+ }
67+
68+ formatToPartTests . forEach ( test => {
69+ it ( `with locale ${ test . locale } timeZone ${ test . timeZone } and format ${ test . nameFormat } ` , ( ) => {
70+ const dateTimeFormat = new Intl . DateTimeFormat ( test . locale , {
71+ timeZone : test . timeZone ,
72+ timeZoneName : test . nameFormat
73+ } ) ;
74+ const parts = dateTimeFormat . formatToParts ( test . date ) ;
75+ const timeZonePart = parts . reduce ( ( found , part ) => ( ( part . type === 'timeZoneName' ) ? part : found ) , null ) ;
76+ assert . equal ( timeZonePart . value , test . expectedTimeZoneName ) ;
77+ } ) ;
78+ } ) ;
79+ } ) ;
80+
81+ describe ( '.format(date, option)' , ( ) => {
4782 timeStampTests . slice ( 0 , 1000 ) . forEach ( testFixture => {
4883 const param = testFixture [ 0 ] . split ( ':' ) ,
4984 locale = param [ 0 ] ,
@@ -127,6 +162,11 @@ describe('Polyfill with complete package', () => {
127162 } ) ;
128163 } ) ;
129164 describe ( '.supportedLocalesOf()' , ( ) => {
165+ if ( ! Intl . DateTimeFormat . supportedLocalesOf ) {
166+ console . log ( 'supportedLocalesOf is not available in this environment' ) ;
167+ return ;
168+ }
169+
130170 it ( 'should work as usual.' , ( ) => {
131171 const supportedLocales = Intl . DateTimeFormat . supportedLocalesOf ( 'en' ) ;
132172
0 commit comments