@@ -11,7 +11,7 @@ import {TestBed} from '@angular/core/testing';
1111import { DateAdapter , MAT_DATE_LOCALE } from '@angular/material/core' ;
1212import { CalendarSystem , DateTime , FixedOffsetZone , Settings } from 'luxon' ;
1313import { LuxonDateModule } from './index' ;
14- import { MAT_LUXON_DATE_ADAPTER_OPTIONS } from './luxon-date-adapter' ;
14+ import { LuxonDateAdapter , MAT_LUXON_DATE_ADAPTER_OPTIONS } from './luxon-date-adapter' ;
1515
1616const JAN = 1 ,
1717 FEB = 2 ,
@@ -418,7 +418,7 @@ describe('LuxonDateAdapter', () => {
418418 expect ( clone . toISO ( ) ) . toEqual ( date . toISO ( ) ) ;
419419 } ) ;
420420
421- it ( 'should respect timezone on clone' , ( ) => {
421+ it ( 'should respect timeZone on clone' , ( ) => {
422422 const dateLocal = DateTime . local ( 2017 , JAN , 1 ) ;
423423 const dateInCet = dateLocal . setZone ( 'Europe/Budapest' ) ;
424424 const cloneInCet = adapter . clone ( dateInCet ) ;
@@ -757,7 +757,7 @@ describe('LuxonDateAdapter with LOCALE_ID override', () => {
757757} ) ;
758758
759759describe ( 'LuxonDateAdapter with MAT_LUXON_DATE_ADAPTER_OPTIONS override' , ( ) => {
760- let adapter : DateAdapter < DateTime > ;
760+ let adapter : LuxonDateAdapter ;
761761
762762 beforeEach ( ( ) => {
763763 TestBed . configureTestingModule ( {
@@ -770,7 +770,7 @@ describe('LuxonDateAdapter with MAT_LUXON_DATE_ADAPTER_OPTIONS override', () =>
770770 ] ,
771771 } ) ;
772772
773- adapter = TestBed . inject ( DateAdapter ) ;
773+ adapter = TestBed . inject ( DateAdapter ) as LuxonDateAdapter ;
774774 } ) ;
775775
776776 describe ( 'use UTC' , ( ) => {
@@ -789,14 +789,38 @@ describe('LuxonDateAdapter with MAT_LUXON_DATE_ADAPTER_OPTIONS override', () =>
789789 } ) ;
790790
791791 it ( 'should parse dates to UTC' , ( ) => {
792- const date = adapter . parse ( '1/2/2017' , 'LL/dd /yyyy' ) ! ;
792+ const date = adapter . parse ( '1/2/2017' , 'L/d /yyyy' ) ! ;
793793 expect ( date . toISO ( ) ) . toBe ( date . toUTC ( ) . toISO ( ) ) ;
794794 } ) ;
795795
796796 it ( 'should return UTC date when deserializing' , ( ) => {
797797 const date = adapter . deserialize ( '1985-04-12T23:20:50.52Z' ) ! ;
798798 expect ( date . toISO ( ) ) . toBe ( date . toUTC ( ) . toISO ( ) ) ;
799799 } ) ;
800+
801+ it ( 'setting timeZone should throw an error when useUtc is true' , ( ) => {
802+ expect ( ( ) => adapter . setTimeZone ( 'Europe/Budapest' ) ) . toThrowError (
803+ 'Cannot set timeZone if the useUtc option is set to true.' ,
804+ ) ;
805+ } ) ;
806+ } ) ;
807+ } ) ;
808+
809+ describe ( 'LuxonDateAdapter with MAT_LUXON_DATE_ADAPTER_OPTIONS useUtc and timeZone override same time' , ( ) => {
810+ it ( 'setting useUtc and timeZone same time should throw an Error' , ( ) => {
811+ TestBed . configureTestingModule ( {
812+ imports : [ LuxonDateModule ] ,
813+ providers : [
814+ {
815+ provide : MAT_LUXON_DATE_ADAPTER_OPTIONS ,
816+ useValue : { useUtc : true , firstDayOfWeek : 1 , timeZone : 'Europe/Budapest' } ,
817+ } ,
818+ ] ,
819+ } ) ;
820+
821+ expect ( ( ) => TestBed . inject ( DateAdapter ) ) . toThrowError (
822+ 'Cannot set timeZone if the useUtc option is set to true.' ,
823+ ) ;
800824 } ) ;
801825} ) ;
802826
@@ -835,6 +859,93 @@ describe('LuxonDateAdapter with MAT_LUXON_DATE_ADAPTER_OPTIONS override for defa
835859 } ) ;
836860} ) ;
837861
862+ describe ( 'LuxonDateAdapter with MAT_LUXON_DATE_ADAPTER_OPTIONS timeZone override' , ( ) => {
863+ let adapter : DateAdapter < DateTime > ;
864+
865+ const timeZone = 'UTC-12' ;
866+
867+ beforeEach ( ( ) => {
868+ TestBed . configureTestingModule ( {
869+ imports : [ LuxonDateModule ] ,
870+ providers : [
871+ {
872+ provide : MAT_LUXON_DATE_ADAPTER_OPTIONS ,
873+ useValue : { firstDayOfWeek : 1 , timeZone} ,
874+ } ,
875+ ] ,
876+ } ) ;
877+
878+ adapter = TestBed . inject ( DateAdapter ) ;
879+ } ) ;
880+
881+ describe ( `use ${ timeZone } timeZone` , ( ) => {
882+ it ( 'should create Luxon date in specified timeZone' , ( ) => {
883+ const date = adapter . createDate ( 2017 , 0 , 5 ) ;
884+ expect ( date . zone . name ) . toEqual ( timeZone ) ;
885+ expect ( date . toISO ( ) ) . toEqual ( DateTime . local ( 2017 , JAN , 5 , { zone : timeZone } ) . toISO ( ) ) ;
886+ } ) ;
887+
888+ it ( 'should create today in specified timeZone' , ( ) => {
889+ const today = adapter . today ( ) ;
890+ expect ( today . zone . name ) . toEqual ( timeZone ) ;
891+ } ) ;
892+
893+ it ( 'should parse dates to specified timeZone' , ( ) => {
894+ const date = adapter . parse ( '1/2/2017' , 'L/d/yyyy' ) ! ;
895+ expect ( date . zone . name ) . toEqual ( timeZone ) ;
896+ expect ( date . toISO ( ) ) . toBe ( DateTime . local ( 2017 , JAN , 2 , { zone : timeZone } ) . toISO ( ) ) ;
897+ } ) ;
898+
899+ it ( 'should return date when deserializing in specified timeZone' , ( ) => {
900+ const date = adapter . deserialize ( '1985-04-12T23:20:50.52Z' ) ! ;
901+ expect ( date . zone . name ) . toEqual ( timeZone ) ;
902+ expect ( date . toISO ( ) ) . toBe ( '1985-04-12T11:20:50.520-12:00' ) ;
903+ } ) ;
904+ } ) ;
905+ } ) ;
906+
907+ describe ( 'LuxonDateAdapter with MAT_LUXON_DATE_ADAPTER_OPTIONS timeZone override programmatically' , ( ) => {
908+ let adapter : LuxonDateAdapter ;
909+
910+ const timeZone = 'UTC-12' ;
911+
912+ beforeEach ( ( ) => {
913+ TestBed . configureTestingModule ( {
914+ imports : [ LuxonDateModule ] ,
915+ providers : [
916+ {
917+ provide : MAT_LUXON_DATE_ADAPTER_OPTIONS ,
918+ useValue : { firstDayOfWeek : 1 , timeZone} ,
919+ } ,
920+ ] ,
921+ } ) ;
922+
923+ adapter = TestBed . inject ( DateAdapter ) as LuxonDateAdapter ;
924+ } ) ;
925+
926+ describe ( `use ${ timeZone } timeZone` , ( ) => {
927+ it ( 'should return correct timeZone after setting zone programmatically' , ( ) => {
928+ const date = adapter . createDate ( 2017 , 0 , 5 ) ;
929+ expect ( date . zone . name ) . toEqual ( timeZone ) ;
930+
931+ const newTimeZone = 'UTC-6' ;
932+ adapter . setTimeZone ( newTimeZone ) ;
933+
934+ const dateAfterZoneChange = adapter . createDate ( 2017 , 0 , 5 ) ;
935+ expect ( dateAfterZoneChange . zone . name ) . toEqual ( newTimeZone ) ;
936+
937+ const today = adapter . today ( ) ;
938+ expect ( today . zone . name ) . toEqual ( newTimeZone ) ;
939+
940+ const parsedDate = adapter . parse ( '1/2/2017' , 'L/d/yyyy' ) ! ;
941+ expect ( parsedDate . zone . name ) . toEqual ( newTimeZone ) ;
942+
943+ const deserializedDate = adapter . deserialize ( '1985-04-12T23:20:50.52Z' ) ! ;
944+ expect ( deserializedDate . zone . name ) . toEqual ( newTimeZone ) ;
945+ } ) ;
946+ } ) ;
947+ } ) ;
948+
838949function assertValidDate ( adapter : DateAdapter < DateTime > , d : DateTime | null , valid : boolean ) {
839950 expect ( adapter . isDateInstance ( d ) )
840951 . not . withContext ( `Expected ${ d } to be a date instance` )
0 commit comments