11import { IgxDateTimeEditorDirective , IgxDateTimeEditorModule } from './date-time-editor.directive' ;
22import { DatePart } from './date-time-editor.common' ;
33import { DOCUMENT } from '@angular/common' ;
4- import { Component , ViewChild , DebugElement , LOCALE_ID , EventEmitter , Output , SimpleChange , SimpleChanges } from '@angular/core' ;
5- import { async , fakeAsync , TestBed , tick , flush , ComponentFixture } from '@angular/core/testing' ;
6- import { FormsModule , FormGroup , FormBuilder , FormControl , ReactiveFormsModule , NgModel , Validators } from '@angular/forms' ;
4+ import { Component , ViewChild , DebugElement , EventEmitter , Output , SimpleChange , SimpleChanges } from '@angular/core' ;
5+ import { async , fakeAsync , TestBed , tick , } from '@angular/core/testing' ;
6+ import { FormsModule , FormGroup , FormBuilder , ReactiveFormsModule , Validators , NgControl } from '@angular/forms' ;
77import { By } from '@angular/platform-browser' ;
88import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
99import { IgxInputGroupModule , IgxInputGroupComponent , IgxInputDirective } from '../../input-group' ;
@@ -17,11 +17,17 @@ describe('IgxDateTimeEditor', () => {
1717 [ 'parseMask' , 'restoreValueFromMask' , 'parseMaskValue' , 'applyMask' ] ) ;
1818 const renderer2 = jasmine . createSpyObj ( 'Renderer2' , [ 'setAttribute' ] ) ;
1919 const locale = 'en' ;
20+ const ngControl = {
21+ control : { touched : false , dirty : false , validator : null , setValue : ( value : any ) => { } } ,
22+ valid : false ,
23+ statusChanges : new EventEmitter ( ) ,
24+ } ;
2025 let elementRef = { nativeElement : null } ;
2126 let inputFormat : string ;
2227 let inputDate : string ;
23- function initializeDateTimeEditor ( ) {
24- dateTimeEditor = new IgxDateTimeEditorDirective ( renderer2 , elementRef , maskParsingService , DOCUMENT , locale ) ;
28+ function initializeDateTimeEditor ( control ?: NgControl ) {
29+ const injector = { get : ( ) => control } ;
30+ dateTimeEditor = new IgxDateTimeEditorDirective ( renderer2 , elementRef , maskParsingService , DOCUMENT , locale , injector ) ;
2531 dateTimeEditor . inputFormat = inputFormat ;
2632 dateTimeEditor . ngOnInit ( ) ;
2733
@@ -30,7 +36,7 @@ describe('IgxDateTimeEditor', () => {
3036 dateTimeEditor . ngOnChanges ( changes ) ;
3137 }
3238 describe ( 'Properties & Events' , ( ) => {
33- it ( 'should emit valueChanged event on clear()' , ( ) => {
39+ it ( 'should emit valueChange event on clear()' , ( ) => {
3440 inputFormat = 'dd/M/yy' ;
3541 inputDate = '6/6/2000' ;
3642 elementRef = { nativeElement : { value : inputDate } } ;
@@ -305,6 +311,22 @@ describe('IgxDateTimeEditor', () => {
305311 dateTimeEditor . decrement ( DatePart . Seconds ) ;
306312 expect ( dateTimeEditor . value . getSeconds ( ) ) . toEqual ( 59 ) ;
307313 } ) ;
314+
315+ it ( 'should properly parse AM/PM no matter where it is in the format' , ( ) => {
316+ inputFormat = 'dd tt yyyy-MM mm-ss-hh' ;
317+ inputDate = '12 AM 2020-06 14-15-11' ;
318+ elementRef = { nativeElement : { value : inputDate } } ;
319+ initializeDateTimeEditor ( ) ;
320+
321+ dateTimeEditor . inputFormat = inputFormat ;
322+ expect ( dateTimeEditor . mask ) . toEqual ( '00 LL 0000-00 00-00-00' ) ;
323+
324+ dateTimeEditor . value = new Date ( 2020 , 5 , 12 , 11 , 15 , 14 ) ;
325+ spyOnProperty ( ( dateTimeEditor as any ) , 'inputValue' , 'get' ) . and . returnValue ( inputDate ) ;
326+
327+ dateTimeEditor . increment ( DatePart . AmPm ) ;
328+ expect ( dateTimeEditor . value ) . toEqual ( new Date ( 2020 , 5 , 12 , 23 , 15 , 14 ) ) ;
329+ } ) ;
308330 } ) ;
309331 } ) ;
310332
@@ -831,7 +853,7 @@ describe('IgxDateTimeEditor', () => {
831853 expect ( dateTimeEditorDirective . setDisabledState ) . toHaveBeenCalledTimes ( 2 ) ;
832854 expect ( dateTimeEditorDirective . setDisabledState ) . toHaveBeenCalledWith ( false ) ;
833855 } ) ) ;
834- it ( 'should emit valueChanged event on blur' , ( ) => {
856+ it ( 'should emit valueChange event on blur' , ( ) => {
835857 const newDate = new Date ( 2004 , 11 , 18 ) ;
836858 fixture . componentInstance . dateTimeFormat = 'dd/MM/yy' ;
837859 fixture . detectChanges ( ) ;
@@ -849,6 +871,18 @@ describe('IgxDateTimeEditor', () => {
849871 expect ( dateTimeEditorDirective . valueChange . emit ) . toHaveBeenCalledTimes ( 1 ) ;
850872 expect ( dateTimeEditorDirective . valueChange . emit ) . toHaveBeenCalledWith ( newDate ) ;
851873 } ) ;
874+ it ( 'should emit valueChange event after input is complete' , ( ) => {
875+ const newDate = new Date ( 2012 , 11 , 12 ) ;
876+ fixture . componentInstance . dateTimeFormat = 'dd/MM/yy' ;
877+ fixture . detectChanges ( ) ;
878+ spyOn ( dateTimeEditorDirective . valueChange , 'emit' ) ;
879+ inputElement . triggerEventHandler ( 'focus' , { } ) ;
880+ fixture . detectChanges ( ) ;
881+ UIInteractions . simulateTyping ( '121212' , inputElement ) ;
882+ fixture . detectChanges ( ) ;
883+ expect ( dateTimeEditorDirective . valueChange . emit ) . toHaveBeenCalledTimes ( 1 ) ;
884+ expect ( dateTimeEditorDirective . valueChange . emit ) . toHaveBeenCalledWith ( newDate ) ;
885+ } ) ;
852886 it ( 'should fire validationFailed when input date is outside date range.' , ( ) => {
853887 fixture . componentInstance . dateTimeFormat = 'dd-MM-yyyy' ;
854888 fixture . componentInstance . minDate = new Date ( 2020 , 1 , 20 ) ;
0 commit comments