@@ -401,7 +401,7 @@ describe("date-time", () => {
401
401
new Date ( 2000 , 0 , 1 , 0 , 0 , 0 , 2 ) ,
402
402
"en-GB"
403
403
)
404
- ) . toStrictEqual ( "" ) ;
404
+ ) . toStrictEqual ( "0 seconds " ) ;
405
405
} ) ;
406
406
407
407
test ( "should format duration by locale" , ( ) => {
@@ -434,13 +434,139 @@ describe("date-time", () => {
434
434
) . toStrictEqual ( "-1 day" ) ;
435
435
} ) ;
436
436
437
- test ( "should format duration by options" , ( ) => {
438
- const startDate = new Date ( "2024-01-01T00:00:00Z" ) ;
439
- const endDate = new Date ( "2024-01-01T01:02:03Z" ) ;
440
- const options : Intl . DurationFormatOptions = { style : "narrow" } ;
437
+ describe ( 'formatDurationByOptions' , ( ) => {
438
+ const baseDate = new Date ( '2024-01-01T00:00:00Z' ) ;
439
+ const laterDate = new Date ( '2024-01-02T01:02:03Z' ) ;
440
+
441
+ test ( 'with long style' , ( ) => {
442
+ const options : Intl . DurationFormatOptions = {
443
+ style : 'long' ,
444
+ }
445
+ expect ( dateTime . formatDurationByOptions ( options , baseDate , laterDate ) ) . toBe ( '1 day, 1 hour, 2 minutes, 3 seconds' ) ;
446
+ } ) ;
441
447
442
- expect ( dateTime . formatDurationByOptions ( options , startDate , endDate ) ) . toBe (
443
- "1h 2m 3s"
444
- ) ;
445
- } ) ;
448
+ test ( 'with long style, 0 duration' , ( ) => {
449
+ let options : Intl . DurationFormatOptions = {
450
+ style : 'long' ,
451
+ }
452
+ expect ( dateTime . formatDurationByOptions ( options , baseDate , baseDate , 'en-US' ) ) . toBe ( '0 seconds' ) ;
453
+ options = {
454
+ style : 'narrow' ,
455
+ }
456
+ expect ( dateTime . formatDurationByOptions ( options , baseDate , baseDate , 'en-US' ) ) . toBe ( '0s' ) ;
457
+ options = {
458
+ style : 'narrow' ,
459
+ minutesDisplay : 'always' ,
460
+ } as Intl . DurationFormatOptions ;
461
+ expect ( dateTime . formatDurationByOptions ( options , baseDate , baseDate , 'en-US' ) ) . toBe ( '0m' ) ;
462
+ } ) ;
463
+
464
+ test ( 'with different style' , ( ) => {
465
+ const options : Intl . DurationFormatOptions = {
466
+ style : 'narrow' ,
467
+ } ;
468
+ expect ( dateTime . formatDurationByOptions ( options , baseDate , laterDate ) ) . toBe ( '1d 1h 2m 3s' ) ;
469
+ } ) ;
470
+
471
+ test ( 'with US locale' , ( ) => {
472
+ const options : Intl . DurationFormatOptions = {
473
+ style : 'narrow' ,
474
+ }
475
+ expect ( dateTime . formatDurationByOptions ( options , baseDate , laterDate , 'en-US' ) ) . toBe ( '1d 1h 2m 3s' ) ;
476
+ } ) ;
477
+
478
+ test ( 'with CN locale' , ( ) => {
479
+ const options : Intl . DurationFormatOptions = {
480
+ style : 'narrow' ,
481
+ }
482
+ expect ( dateTime . formatDurationByOptions ( options , baseDate , laterDate , 'zh-CN' ) ) . toBe ( '1天1小时2分钟3秒' ) ;
483
+ } ) ;
484
+
485
+ test ( 'with JP locale' , ( ) => {
486
+ // Japanese locale needs to display unit as long, as narrow is wrong, returns english, we override to long in the implementation
487
+ const options : Intl . DurationFormatOptions = {
488
+ style : 'narrow' ,
489
+ }
490
+ expect ( dateTime . formatDurationByOptions ( options , baseDate , laterDate , 'ja-JP' ) ) . toBe ( '1日1時間2分3秒' ) ;
491
+ } ) ;
492
+
493
+ test ( 'with KR locale' , ( ) => {
494
+ const options : Intl . DurationFormatOptions = {
495
+ style : 'narrow' ,
496
+ }
497
+ expect ( dateTime . formatDurationByOptions ( options , baseDate , laterDate , 'ko-KR' ) ) . toBe ( '1일1시간2분3초' ) ;
498
+ } ) ;
499
+
500
+ test ( 'with DE locale' , ( ) => {
501
+ const options : Intl . DurationFormatOptions = {
502
+ style : 'narrow' ,
503
+ }
504
+ expect ( dateTime . formatDurationByOptions ( options , baseDate , laterDate , 'de-DE' ) ) . toBe ( '1 T, 1 Std., 2 Min. und 3 Sek.' ) ;
505
+ } ) ;
506
+
507
+ } ) ;
508
+ describe ( 'formatDurationByOptions fallback' , ( ) => {
509
+ const originalDurationFormat = Intl . DurationFormat ;
510
+
511
+ beforeEach ( ( ) => {
512
+ // Mock DurationFormat as undefined using TypeScript type assertions
513
+ ( Intl as any ) . DurationFormat = undefined ;
514
+ } ) ;
515
+
516
+ afterEach ( ( ) => {
517
+ // Restore the original implementation
518
+ ( Intl as any ) . DurationFormat = originalDurationFormat ;
519
+ } ) ;
520
+
521
+ const baseDate = new Date ( '2024-01-01T00:00:00Z' ) ;
522
+ const laterDate = new Date ( '2024-01-02T01:02:03Z' ) ;
523
+
524
+ test ( 'with default options' , ( ) => {
525
+ const options : Intl . DurationFormatOptions = {
526
+ style : 'long' ,
527
+ } ;
528
+
529
+ // This should use the fallback implementation
530
+ const result = dateTime . formatDurationByOptions ( options , baseDate , laterDate ) ;
531
+ expect ( result ) . toStrictEqual ( '1 day 1 hour 2 minutes 3 seconds' ) ;
532
+ expect ( result ) . toBeDefined ( ) ;
533
+ // Add more specific expectations based on your fallback implementation
534
+ } ) ;
535
+
536
+ test ( 'throws error with null options' , ( ) => {
537
+ expect ( ( ) => {
538
+ // @ts -ignore - Deliberately testing invalid input
539
+ dateTime . formatDurationByOptions ( null , baseDate , laterDate ) ;
540
+ } ) . toThrow ( 'Please use formatDuration instead' ) ;
541
+ } ) ;
542
+
543
+ test ( 'with US locale' , ( ) => {
544
+ let options : Intl . DurationFormatOptions = {
545
+ style : 'long' ,
546
+ }
547
+ expect ( dateTime . formatDurationByOptions ( options , baseDate , laterDate , 'en-US' ) ) . toStrictEqual ( '1 day 1 hour 2 minutes 3 seconds' ) ;
548
+ options . style = 'narrow' ;
549
+ expect ( dateTime . formatDurationByOptions ( options , baseDate , laterDate , 'en-US' ) ) . toStrictEqual ( '1d 1h 2m 3s' ) ;
550
+ } ) ;
551
+
552
+ test ( 'with CN locale' , ( ) => {
553
+ let options : Intl . DurationFormatOptions = {
554
+ style : 'long' ,
555
+ }
556
+ expect ( dateTime . formatDurationByOptions ( options , baseDate , laterDate , 'zh-CN' ) ) . toStrictEqual ( '1天1小时2分钟3秒钟' ) ;
557
+ options . style = 'narrow' ;
558
+ expect ( dateTime . formatDurationByOptions ( options , baseDate , laterDate , 'zh-CN' ) ) . toStrictEqual ( '1天1小时2分钟3秒' ) ;
559
+ } ) ;
560
+
561
+ test ( 'with JP locale' , ( ) => {
562
+ // For Japanese locale, narrow is wrong, returns english, so we override to long in the fallback implementation
563
+ let options : Intl . DurationFormatOptions = {
564
+ style : 'long' ,
565
+ }
566
+ expect ( dateTime . formatDurationByOptions ( options , baseDate , laterDate , 'ja-JP' ) ) . toStrictEqual ( '1日1時間2分3秒' ) ;
567
+ options . style = 'narrow' ;
568
+ expect ( dateTime . formatDurationByOptions ( options , baseDate , laterDate , 'ja-JP' ) ) . toStrictEqual ( '1日1時間2分3秒' ) ;
569
+ } ) ;
570
+
571
+ } )
446
572
} ) ;
0 commit comments