@@ -5179,3 +5179,125 @@ describe("Node Timer: ref(), unref(),hasRef()", function () {
5179
5179
clock . uninstall ( ) ;
5180
5180
} ) ;
5181
5181
} ) ;
5182
+
5183
+ describe ( "Intl API" , function ( ) {
5184
+ /**
5185
+ * Tester function to check if the globally hijacked Intl object is plugging into the faked Clock
5186
+ *
5187
+ * @param {string } ianaTimeZone - IANA time zone name
5188
+ * @param {number } timestamp - UNIX timestamp
5189
+ * @returns {boolean }
5190
+ */
5191
+ function isFirstOfMonth ( ianaTimeZone , timestamp ) {
5192
+ return (
5193
+ new Intl . DateTimeFormat ( undefined , { timeZone : ianaTimeZone } )
5194
+ . formatToParts ( timestamp )
5195
+ . find ( ( part ) => part . type === "day" ) . value === "1"
5196
+ ) ;
5197
+ }
5198
+
5199
+ let clock ;
5200
+
5201
+ before ( function ( ) {
5202
+ clock = FakeTimers . install ( ) ;
5203
+ } ) ;
5204
+
5205
+ after ( function ( ) {
5206
+ clock . uninstall ( ) ;
5207
+ } ) ;
5208
+
5209
+ it ( "Executes formatRange like normal" , function ( ) {
5210
+ const start = new Date ( Date . UTC ( 2020 , 0 , 1 , 0 , 0 ) ) ;
5211
+ const end = new Date ( Date . UTC ( 2020 , 0 , 1 , 0 , 1 ) ) ;
5212
+ const options = {
5213
+ timeZone : "UTC" ,
5214
+ hour12 : false ,
5215
+ hour : "numeric" ,
5216
+ minute : "numeric" ,
5217
+ } ;
5218
+ assert . equals (
5219
+ new Intl . DateTimeFormat ( "en-GB" , options ) . formatRange ( start , end ) ,
5220
+ "00:00–00:01"
5221
+ ) ;
5222
+ } ) ;
5223
+
5224
+ it ( "Executes formatRangeToParts like normal" , function ( ) {
5225
+ const start = new Date ( Date . UTC ( 2020 , 0 , 1 , 0 , 0 ) ) ;
5226
+ const end = new Date ( Date . UTC ( 2020 , 0 , 1 , 0 , 1 ) ) ;
5227
+ const options = {
5228
+ timeZone : "UTC" ,
5229
+ hour12 : false ,
5230
+ hour : "numeric" ,
5231
+ minute : "numeric" ,
5232
+ } ;
5233
+ assert . equals (
5234
+ new Intl . DateTimeFormat ( "en-GB" , options ) . formatRangeToParts (
5235
+ start ,
5236
+ end
5237
+ ) ,
5238
+ [
5239
+ { type : "hour" , value : "00" , source : "startRange" } ,
5240
+ { type : "literal" , value : ":" , source : "startRange" } ,
5241
+ { type : "minute" , value : "00" , source : "startRange" } ,
5242
+ { type : "literal" , value : "–" , source : "shared" } ,
5243
+ { type : "hour" , value : "00" , source : "endRange" } ,
5244
+ { type : "literal" , value : ":" , source : "endRange" } ,
5245
+ { type : "minute" , value : "01" , source : "endRange" } ,
5246
+ ]
5247
+ ) ;
5248
+ } ) ;
5249
+
5250
+ it ( "Executes resolvedOptions like normal" , function ( ) {
5251
+ const options = {
5252
+ timeZone : "UTC" ,
5253
+ hour12 : false ,
5254
+ hour : "2-digit" ,
5255
+ minute : "2-digit" ,
5256
+ } ;
5257
+ assert . equals (
5258
+ new Intl . DateTimeFormat ( "en-GB" , options ) . resolvedOptions ( ) ,
5259
+ {
5260
+ locale : "en-GB" ,
5261
+ calendar : "gregory" ,
5262
+ numberingSystem : "latn" ,
5263
+ timeZone : "UTC" ,
5264
+ hour12 : false ,
5265
+ hourCycle : "h23" ,
5266
+ hour : "2-digit" ,
5267
+ minute : "2-digit" ,
5268
+ }
5269
+ ) ;
5270
+ } ) ;
5271
+
5272
+ it ( "formatToParts via isFirstOfMonth -> Returns true when passed a timestamp argument that is first of the month" , function ( ) {
5273
+ // June 1 04:00 UTC - Toronto is June 1 00:00
5274
+ assert . isTrue (
5275
+ isFirstOfMonth ( "America/Toronto" , Date . UTC ( 2022 , 5 , 1 , 4 ) )
5276
+ ) ;
5277
+ } ) ;
5278
+
5279
+ it ( "formatToParts via isFirstOfMonth -> Returns false when passed a timestamp argument that is not first of the month" , function ( ) {
5280
+ // June 1 00:00 UTC - Toronto is May 31 20:00
5281
+ assert . isFalse ( isFirstOfMonth ( "America/Toronto" , Date . UTC ( 2022 , 5 , 1 ) ) ) ;
5282
+ } ) ;
5283
+
5284
+ it ( "formatToParts via isFirstOfMonth -> Returns true when passed no timestamp and system time is first of the month" , function ( ) {
5285
+ // June 1 04:00 UTC - Toronto is June 1 00:00
5286
+ clock . now = Date . UTC ( 2022 , 5 , 1 , 4 ) ;
5287
+ assert . isTrue ( isFirstOfMonth ( "America/Toronto" ) ) ;
5288
+ } ) ;
5289
+
5290
+ it ( "formatToParts via isFirstOfMonth -> Returns false when passed no timestamp and system time is not first of the month" , function ( ) {
5291
+ // June 1 00:00 UTC - Toronto is May 31 20:00
5292
+ clock . now = Date . UTC ( 2022 , 5 , 1 ) ;
5293
+ assert . isFalse ( isFirstOfMonth ( "America/Toronto" ) ) ;
5294
+ } ) ;
5295
+
5296
+ it ( "Executes supportedLocalesOf like normal" , function ( ) {
5297
+ assert . equals (
5298
+ Intl . DateTimeFormat . supportedLocalesOf ( ) ,
5299
+ //eslint-disable-next-line no-underscore-dangle
5300
+ clock . _Intl . DateTimeFormat . supportedLocalesOf ( )
5301
+ ) ;
5302
+ } ) ;
5303
+ } ) ;
0 commit comments