7
7
*/
8
8
9
9
import { LOCALE_ID } from '@angular/core' ;
10
- import { TestBed , waitForAsync } from '@angular/core/testing' ;
10
+ import { TestBed } from '@angular/core/testing' ;
11
11
import { DateAdapter , MAT_DATE_LOCALE } from '@angular/material/core' ;
12
12
import { CalendarSystem , DateTime , FixedOffsetZone , Settings } from 'luxon' ;
13
13
import { LuxonDateModule } from './index' ;
@@ -21,14 +21,11 @@ const JAN = 1,
21
21
describe ( 'LuxonDateAdapter' , ( ) => {
22
22
let adapter : DateAdapter < DateTime > ;
23
23
24
- beforeEach ( waitForAsync ( ( ) => {
25
- TestBed . configureTestingModule ( {
26
- imports : [ LuxonDateModule ] ,
27
- } ) ;
28
-
24
+ beforeEach ( ( ) => {
25
+ TestBed . configureTestingModule ( { imports : [ LuxonDateModule ] } ) ;
29
26
adapter = TestBed . inject ( DateAdapter ) ;
30
27
adapter . setLocale ( 'en-US' ) ;
31
- } ) ) ;
28
+ } ) ;
32
29
33
30
it ( 'should get year' , ( ) => {
34
31
expect ( adapter . getYear ( DateTime . local ( 2017 , JAN , 1 ) ) ) . toBe ( 2017 ) ;
@@ -550,19 +547,158 @@ describe('LuxonDateAdapter', () => {
550
547
it ( 'should create invalid date' , ( ) => {
551
548
assertValidDate ( adapter , adapter . invalid ( ) , false ) ;
552
549
} ) ;
550
+
551
+ it ( 'should get hours' , ( ) => {
552
+ expect ( adapter . getHours ( DateTime . local ( 2024 , JAN , 1 , 14 ) ) ) . toBe ( 14 ) ;
553
+ } ) ;
554
+
555
+ it ( 'should get minutes' , ( ) => {
556
+ expect ( adapter . getMinutes ( DateTime . local ( 2024 , JAN , 1 , 14 , 53 ) ) ) . toBe ( 53 ) ;
557
+ } ) ;
558
+
559
+ it ( 'should get seconds' , ( ) => {
560
+ expect ( adapter . getSeconds ( DateTime . local ( 2024 , JAN , 1 , 14 , 53 , 42 ) ) ) . toBe ( 42 ) ;
561
+ } ) ;
562
+
563
+ it ( 'should set the time of a date' , ( ) => {
564
+ const target = DateTime . local ( 2024 , JAN , 1 , 0 , 0 , 0 ) ;
565
+ const result = adapter . setTime ( target , 14 , 53 , 42 ) ;
566
+ expect ( adapter . getHours ( result ) ) . toBe ( 14 ) ;
567
+ expect ( adapter . getMinutes ( result ) ) . toBe ( 53 ) ;
568
+ expect ( adapter . getSeconds ( result ) ) . toBe ( 42 ) ;
569
+ } ) ;
570
+
571
+ it ( 'should throw when passing in invalid hours to setTime' , ( ) => {
572
+ expect ( ( ) => adapter . setTime ( adapter . today ( ) , - 1 , 0 , 0 ) ) . toThrowError (
573
+ 'Invalid hours "-1". Hours value must be between 0 and 23.' ,
574
+ ) ;
575
+ expect ( ( ) => adapter . setTime ( adapter . today ( ) , 51 , 0 , 0 ) ) . toThrowError (
576
+ 'Invalid hours "51". Hours value must be between 0 and 23.' ,
577
+ ) ;
578
+ } ) ;
579
+
580
+ it ( 'should throw when passing in invalid minutes to setTime' , ( ) => {
581
+ expect ( ( ) => adapter . setTime ( adapter . today ( ) , 0 , - 1 , 0 ) ) . toThrowError (
582
+ 'Invalid minutes "-1". Minutes value must be between 0 and 59.' ,
583
+ ) ;
584
+ expect ( ( ) => adapter . setTime ( adapter . today ( ) , 0 , 65 , 0 ) ) . toThrowError (
585
+ 'Invalid minutes "65". Minutes value must be between 0 and 59.' ,
586
+ ) ;
587
+ } ) ;
588
+
589
+ it ( 'should throw when passing in invalid seconds to setTime' , ( ) => {
590
+ expect ( ( ) => adapter . setTime ( adapter . today ( ) , 0 , 0 , - 1 ) ) . toThrowError (
591
+ 'Invalid seconds "-1". Seconds value must be between 0 and 59.' ,
592
+ ) ;
593
+ expect ( ( ) => adapter . setTime ( adapter . today ( ) , 0 , 0 , 65 ) ) . toThrowError (
594
+ 'Invalid seconds "65". Seconds value must be between 0 and 59.' ,
595
+ ) ;
596
+ } ) ;
597
+
598
+ it ( 'should parse a 24-hour time string' , ( ) => {
599
+ const result = adapter . parseTime ( '14:52' , 't' ) ! ;
600
+ expect ( result ) . toBeTruthy ( ) ;
601
+ expect ( adapter . isValid ( result ) ) . toBe ( true ) ;
602
+ expect ( adapter . getHours ( result ) ) . toBe ( 14 ) ;
603
+ expect ( adapter . getMinutes ( result ) ) . toBe ( 52 ) ;
604
+ expect ( adapter . getSeconds ( result ) ) . toBe ( 0 ) ;
605
+ } ) ;
606
+
607
+ it ( 'should parse a 12-hour time string' , ( ) => {
608
+ const result = adapter . parseTime ( '2:52 PM' , 't' ) ! ;
609
+ expect ( result ) . toBeTruthy ( ) ;
610
+ expect ( adapter . isValid ( result ) ) . toBe ( true ) ;
611
+ expect ( adapter . getHours ( result ) ) . toBe ( 14 ) ;
612
+ expect ( adapter . getMinutes ( result ) ) . toBe ( 52 ) ;
613
+ expect ( adapter . getSeconds ( result ) ) . toBe ( 0 ) ;
614
+ } ) ;
615
+
616
+ it ( 'should parse a padded time string' , ( ) => {
617
+ const result = adapter . parseTime ( '03:04:05' , 'tt' ) ! ;
618
+ expect ( result ) . toBeTruthy ( ) ;
619
+ expect ( adapter . isValid ( result ) ) . toBe ( true ) ;
620
+ expect ( adapter . getHours ( result ) ) . toBe ( 3 ) ;
621
+ expect ( adapter . getMinutes ( result ) ) . toBe ( 4 ) ;
622
+ expect ( adapter . getSeconds ( result ) ) . toBe ( 5 ) ;
623
+ } ) ;
624
+
625
+ it ( 'should parse a time string that uses dot as a separator' , ( ) => {
626
+ adapter . setLocale ( 'fi-FI' ) ;
627
+ const result = adapter . parseTime ( '14.52' , 't' ) ! ;
628
+ expect ( result ) . toBeTruthy ( ) ;
629
+ expect ( adapter . isValid ( result ) ) . toBe ( true ) ;
630
+ expect ( adapter . getHours ( result ) ) . toBe ( 14 ) ;
631
+ expect ( adapter . getMinutes ( result ) ) . toBe ( 52 ) ;
632
+ expect ( adapter . getSeconds ( result ) ) . toBe ( 0 ) ;
633
+ } ) ;
634
+
635
+ it ( 'should parse a time string with characters around the time' , ( ) => {
636
+ adapter . setLocale ( 'bg-BG' ) ;
637
+ const result = adapter . parseTime ( '14:52 ч.' , 't' ) ! ;
638
+ expect ( result ) . toBeTruthy ( ) ;
639
+ expect ( adapter . isValid ( result ) ) . toBe ( true ) ;
640
+ expect ( adapter . getHours ( result ) ) . toBe ( 14 ) ;
641
+ expect ( adapter . getMinutes ( result ) ) . toBe ( 52 ) ;
642
+ expect ( adapter . getSeconds ( result ) ) . toBe ( 0 ) ;
643
+ } ) ;
644
+
645
+ it ( 'should return an invalid date when parsing invalid time string' , ( ) => {
646
+ expect ( adapter . isValid ( adapter . parseTime ( 'abc' , 't' ) ! ) ) . toBeFalse ( ) ;
647
+ expect ( adapter . isValid ( adapter . parseTime ( ' ' , 't' ) ! ) ) . toBeFalse ( ) ;
648
+ expect ( adapter . isValid ( adapter . parseTime ( '24:05' , 't' ) ! ) ) . toBeFalse ( ) ;
649
+ expect ( adapter . isValid ( adapter . parseTime ( '00:61:05' , 'tt' ) ! ) ) . toBeFalse ( ) ;
650
+ expect ( adapter . isValid ( adapter . parseTime ( '14:52:78' , 'tt' ) ! ) ) . toBeFalse ( ) ;
651
+ } ) ;
652
+
653
+ it ( 'should return null when parsing unsupported time values' , ( ) => {
654
+ expect ( adapter . parseTime ( true , 't' ) ) . toBeNull ( ) ;
655
+ expect ( adapter . parseTime ( undefined , 't' ) ) . toBeNull ( ) ;
656
+ expect ( adapter . parseTime ( '' , 't' ) ) . toBeNull ( ) ;
657
+ } ) ;
658
+
659
+ it ( 'should compare times' , ( ) => {
660
+ const base = [ 2024 , JAN , 1 ] as const ;
661
+
662
+ expect (
663
+ adapter . compareTime ( DateTime . local ( ...base , 12 , 0 , 0 ) , DateTime . local ( ...base , 13 , 0 , 0 ) ) ,
664
+ ) . toBeLessThan ( 0 ) ;
665
+ expect (
666
+ adapter . compareTime ( DateTime . local ( ...base , 12 , 50 , 0 ) , DateTime . local ( ...base , 12 , 51 , 0 ) ) ,
667
+ ) . toBeLessThan ( 0 ) ;
668
+ expect (
669
+ adapter . compareTime ( DateTime . local ( ...base , 1 , 2 , 3 ) , DateTime . local ( ...base , 1 , 2 , 3 ) ) ,
670
+ ) . toBe ( 0 ) ;
671
+ expect (
672
+ adapter . compareTime ( DateTime . local ( ...base , 13 , 0 , 0 ) , DateTime . local ( ...base , 12 , 0 , 0 ) ) ,
673
+ ) . toBeGreaterThan ( 0 ) ;
674
+ expect (
675
+ adapter . compareTime ( DateTime . local ( ...base , 12 , 50 , 11 ) , DateTime . local ( ...base , 12 , 50 , 10 ) ) ,
676
+ ) . toBeGreaterThan ( 0 ) ;
677
+ expect (
678
+ adapter . compareTime ( DateTime . local ( ...base , 13 , 0 , 0 ) , DateTime . local ( ...base , 10 , 59 , 59 ) ) ,
679
+ ) . toBeGreaterThan ( 0 ) ;
680
+ } ) ;
681
+
682
+ it ( 'should add milliseconds to a date' , ( ) => {
683
+ const amount = 1234567 ;
684
+ const initial = DateTime . local ( 2024 , JAN , 1 , 12 , 34 , 56 ) ;
685
+ const result = adapter . addMilliseconds ( initial , amount ) ;
686
+ expect ( result ) . not . toBe ( initial ) ;
687
+ expect ( result . toMillis ( ) - initial . toMillis ( ) ) . toBe ( amount ) ;
688
+ } ) ;
553
689
} ) ;
554
690
555
691
describe ( 'LuxonDateAdapter with MAT_DATE_LOCALE override' , ( ) => {
556
692
let adapter : DateAdapter < DateTime > ;
557
693
558
- beforeEach ( waitForAsync ( ( ) => {
694
+ beforeEach ( ( ) => {
559
695
TestBed . configureTestingModule ( {
560
696
imports : [ LuxonDateModule ] ,
561
697
providers : [ { provide : MAT_DATE_LOCALE , useValue : 'da-DK' } ] ,
562
698
} ) ;
563
699
564
700
adapter = TestBed . inject ( DateAdapter ) ;
565
- } ) ) ;
701
+ } ) ;
566
702
567
703
it ( 'should take the default locale id from the MAT_DATE_LOCALE injection token' , ( ) => {
568
704
const date = adapter . format ( DateTime . local ( 2017 , JAN , 2 ) , 'DD' ) ;
@@ -573,14 +709,14 @@ describe('LuxonDateAdapter with MAT_DATE_LOCALE override', () => {
573
709
describe ( 'LuxonDateAdapter with LOCALE_ID override' , ( ) => {
574
710
let adapter : DateAdapter < DateTime > ;
575
711
576
- beforeEach ( waitForAsync ( ( ) => {
712
+ beforeEach ( ( ) => {
577
713
TestBed . configureTestingModule ( {
578
714
imports : [ LuxonDateModule ] ,
579
715
providers : [ { provide : LOCALE_ID , useValue : 'fr-FR' } ] ,
580
716
} ) ;
581
717
582
718
adapter = TestBed . inject ( DateAdapter ) ;
583
- } ) ) ;
719
+ } ) ;
584
720
585
721
it ( 'should take the default locale id from the LOCALE_ID injection token' , ( ) => {
586
722
const date = adapter . format ( DateTime . local ( 2017 , JAN , 2 ) , 'DD' ) ;
@@ -591,7 +727,7 @@ describe('LuxonDateAdapter with LOCALE_ID override', () => {
591
727
describe ( 'LuxonDateAdapter with MAT_LUXON_DATE_ADAPTER_OPTIONS override' , ( ) => {
592
728
let adapter : DateAdapter < DateTime > ;
593
729
594
- beforeEach ( waitForAsync ( ( ) => {
730
+ beforeEach ( ( ) => {
595
731
TestBed . configureTestingModule ( {
596
732
imports : [ LuxonDateModule ] ,
597
733
providers : [
@@ -603,7 +739,7 @@ describe('LuxonDateAdapter with MAT_LUXON_DATE_ADAPTER_OPTIONS override', () =>
603
739
} ) ;
604
740
605
741
adapter = TestBed . inject ( DateAdapter ) ;
606
- } ) ) ;
742
+ } ) ;
607
743
608
744
describe ( 'use UTC' , ( ) => {
609
745
it ( 'should create Luxon date in UTC' , ( ) => {
@@ -637,7 +773,7 @@ describe('LuxonDateAdapter with MAT_LUXON_DATE_ADAPTER_OPTIONS override for defa
637
773
638
774
const calendarExample : CalendarSystem = 'islamic' ;
639
775
640
- beforeEach ( waitForAsync ( ( ) => {
776
+ beforeEach ( ( ) => {
641
777
TestBed . configureTestingModule ( {
642
778
imports : [ LuxonDateModule ] ,
643
779
providers : [
@@ -649,7 +785,7 @@ describe('LuxonDateAdapter with MAT_LUXON_DATE_ADAPTER_OPTIONS override for defa
649
785
} ) ;
650
786
651
787
adapter = TestBed . inject ( DateAdapter ) ;
652
- } ) ) ;
788
+ } ) ;
653
789
654
790
describe ( `use ${ calendarExample } calendar` , ( ) => {
655
791
it ( `should create Luxon date in ${ calendarExample } calendar` , ( ) => {
0 commit comments