@@ -7,10 +7,11 @@ import {
7
7
} from '@angular/core/testing' ;
8
8
import { FormsModule } from '@angular/forms' ;
9
9
import { IgxInputGroupModule } from '../../input-group/input-group.component' ;
10
- import { IgxMaskModule } from './mask.directive' ;
10
+ import { IgxMaskModule , IgxMaskDirective } from './mask.directive' ;
11
11
12
12
import { configureTestSuite } from '../../test-utils/configure-suite' ;
13
13
import { UIInteractions } from '../../test-utils/ui-interactions.spec' ;
14
+ import { Replaced } from './mask-parsing.service' ;
14
15
15
16
describe ( 'igxMask' , ( ) => {
16
17
configureTestSuite ( ) ;
@@ -399,6 +400,53 @@ describe('igxMask', () => {
399
400
} ) ) ;
400
401
} ) ;
401
402
403
+ describe ( 'igxMaskDirective ControlValueAccessor Unit' , ( ) => {
404
+ let mask : IgxMaskDirective ;
405
+ it ( 'Should correctly implement interface methods' , ( ) => {
406
+ const mockNgControl = jasmine . createSpyObj ( 'NgControl' , [ 'registerOnChangeCb' , 'registerOnTouchedCb' ] ) ;
407
+
408
+ const mockParser = jasmine . createSpyObj ( 'MaskParsingService' , {
409
+ applyMask : 'test____' ,
410
+ replaceInMask : { value : 'test_2__' , end : 6 } as Replaced ,
411
+ parseValueFromMask : 'test2'
412
+ } ) ;
413
+ const format = 'CCCCCCCC' ;
414
+
415
+ // init
416
+ mask = new IgxMaskDirective ( null , mockParser , null ) ;
417
+ mask . mask = format ;
418
+ mask . registerOnChange ( mockNgControl . registerOnChangeCb ) ;
419
+ mask . registerOnTouched ( mockNgControl . registerOnTouchedCb ) ;
420
+ spyOn ( mask . onValueChange , 'emit' ) ;
421
+ const inputGet = spyOnProperty ( mask as any , 'inputValue' , 'get' ) ;
422
+ const inputSet = spyOnProperty ( mask as any , 'inputValue' , 'set' ) ;
423
+
424
+ // writeValue
425
+ inputGet . and . returnValue ( 'formatted' ) ;
426
+ mask . writeValue ( 'test' ) ;
427
+ expect ( mockParser . applyMask ) . toHaveBeenCalledWith ( 'test' , jasmine . objectContaining ( { format } ) ) ;
428
+ expect ( inputSet ) . toHaveBeenCalledWith ( 'test____' ) ;
429
+ expect ( mockNgControl . registerOnChangeCb ) . not . toHaveBeenCalled ( ) ;
430
+ expect ( mask . onValueChange . emit ) . toHaveBeenCalledWith ( { rawValue : 'test' , formattedValue : 'formatted' } ) ;
431
+
432
+ // OnChange callback
433
+ inputGet . and . returnValue ( 'test_2___' ) ;
434
+ spyOnProperty ( mask as any , 'selectionEnd' ) . and . returnValue ( 6 ) ;
435
+ const setSelectionSpy = spyOn ( mask as any , 'setSelectionRange' ) ;
436
+ mask . onInputChanged ( ) ;
437
+ expect ( mockParser . replaceInMask ) . toHaveBeenCalledWith ( '' , 'test_2' , jasmine . objectContaining ( { format } ) , 0 , 0 ) ;
438
+ expect ( inputSet ) . toHaveBeenCalledWith ( 'test_2__' ) ;
439
+ expect ( setSelectionSpy ) . toHaveBeenCalledWith ( 6 ) ;
440
+ expect ( mockNgControl . registerOnChangeCb ) . toHaveBeenCalledWith ( 'test2' ) ;
441
+
442
+ // OnTouched callback
443
+ mask . onFocus ( ) ;
444
+ expect ( mockNgControl . registerOnTouchedCb ) . not . toHaveBeenCalled ( ) ;
445
+ mask . onBlur ( '' ) ;
446
+ expect ( mockNgControl . registerOnTouchedCb ) . toHaveBeenCalledTimes ( 1 ) ;
447
+ } ) ;
448
+ } ) ;
449
+
402
450
@Component ( {
403
451
template : `<igx-input-group>
404
452
<input #input type="text" igxInput [(ngModel)]="value" [igxMask]="mask"/>
0 commit comments