@@ -33,7 +33,8 @@ describe('IgxInput', () => {
33
33
RequiredTwoWayDataBoundInputComponent ,
34
34
DataBoundDisabledInputComponent ,
35
35
ReactiveFormComponent ,
36
- InputsWithSameNameAttributesComponent
36
+ InputsWithSameNameAttributesComponent ,
37
+ ToggleRequiredWithNgModelInputComponent
37
38
] ,
38
39
imports : [
39
40
IgxInputGroupModule ,
@@ -247,6 +248,7 @@ describe('IgxInput', () => {
247
248
} ) ;
248
249
249
250
it ( 'When updating two inputs with same attribute names through ngModel, label should responds' , fakeAsync ( ( ) => {
251
+
250
252
const fix = TestBed . createComponent ( InputsWithSameNameAttributesComponent ) ;
251
253
fix . detectChanges ( ) ;
252
254
@@ -319,6 +321,116 @@ describe('IgxInput', () => {
319
321
fix . detectChanges ( ) ;
320
322
expect ( firstInputGroup . nativeElement . classList . contains ( 'igx-input-group--invalid' ) ) . toBe ( true ) ;
321
323
} ) ) ;
324
+
325
+ it ( 'Should style input when required is toggled dynamically.' , ( ) => {
326
+ const fixture = TestBed . createComponent ( ToggleRequiredWithNgModelInputComponent ) ;
327
+ fixture . detectChanges ( ) ;
328
+
329
+ const instance = fixture . componentInstance ;
330
+ const input = instance . igxInputs . toArray ( ) [ 1 ] ;
331
+ const inputGroup = instance . igxInputGroups . toArray ( ) [ 1 ] ;
332
+
333
+ expect ( input . required ) . toBe ( false ) ;
334
+ expect ( inputGroup . isRequired ) . toBeFalsy ( ) ;
335
+ expect ( input . valid ) . toBe ( IgxInputState . INITIAL ) ;
336
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_REQUIRED_CSS_CLASS ) ) . toBe ( false ) ;
337
+
338
+ dispatchInputEvent ( 'focus' , input . nativeElement , fixture ) ;
339
+ expect ( input . valid ) . toBe ( IgxInputState . INITIAL ) ;
340
+
341
+ input . value = '123' ;
342
+ dispatchInputEvent ( 'input' , input . nativeElement , fixture ) ;
343
+ expect ( input . valid ) . toBe ( IgxInputState . INITIAL ) ;
344
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_VALID_CSS_CLASS ) ) . toBe ( false ) ;
345
+
346
+ dispatchInputEvent ( 'blur' , input . nativeElement , fixture ) ;
347
+ expect ( input . valid ) . toBe ( IgxInputState . INITIAL ) ;
348
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_INVALID_CSS_CLASS ) ) . toBe ( false ) ;
349
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_VALID_CSS_CLASS ) ) . toBe ( false ) ;
350
+
351
+ instance . isRequired = true ;
352
+ fixture . detectChanges ( ) ;
353
+
354
+ expect ( input . required ) . toBe ( true ) ;
355
+
356
+ expect ( inputGroup . isRequired ) . toBeTruthy ( ) ;
357
+ expect ( input . valid ) . toBe ( IgxInputState . INITIAL ) ;
358
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_REQUIRED_CSS_CLASS ) ) . toBe ( true ) ;
359
+
360
+ dispatchInputEvent ( 'focus' , input . nativeElement , fixture ) ;
361
+ expect ( input . valid ) . toBe ( IgxInputState . INITIAL ) ;
362
+
363
+ input . value = '' ;
364
+ dispatchInputEvent ( 'input' , input . nativeElement , fixture ) ;
365
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_INVALID_CSS_CLASS ) ) . toBe ( true ) ;
366
+
367
+ dispatchInputEvent ( 'blur' , input . nativeElement , fixture ) ;
368
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_INVALID_CSS_CLASS ) ) . toBe ( true ) ;
369
+
370
+ input . value = '123' ;
371
+ dispatchInputEvent ( 'input' , input . nativeElement , fixture ) ;
372
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_VALID_CSS_CLASS ) ) . toBe ( true ) ;
373
+
374
+ dispatchInputEvent ( 'blur' , input . nativeElement , fixture ) ;
375
+ expect ( input . valid ) . toBe ( IgxInputState . INITIAL ) ;
376
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_INVALID_CSS_CLASS ) ) . toBe ( false ) ;
377
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_VALID_CSS_CLASS ) ) . toBe ( false ) ;
378
+ } ) ;
379
+
380
+ it ( 'Should style input with ngModel when required is toggled dynamically.' , ( ) => {
381
+ const fixture = TestBed . createComponent ( ToggleRequiredWithNgModelInputComponent ) ;
382
+ fixture . detectChanges ( ) ;
383
+
384
+ const instance = fixture . componentInstance ;
385
+ const input = instance . igxInputs . toArray ( ) [ 0 ] ;
386
+ const inputGroup = instance . igxInputGroups . toArray ( ) [ 0 ] ;
387
+
388
+ expect ( input . required ) . toBe ( false ) ;
389
+ expect ( inputGroup . isRequired ) . toBeFalsy ( ) ;
390
+ expect ( input . valid ) . toBe ( IgxInputState . INITIAL ) ;
391
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_REQUIRED_CSS_CLASS ) ) . toBe ( false ) ;
392
+
393
+ dispatchInputEvent ( 'focus' , input . nativeElement , fixture ) ;
394
+ expect ( input . valid ) . toBe ( IgxInputState . INITIAL ) ;
395
+
396
+ input . value = '123' ;
397
+ dispatchInputEvent ( 'input' , input . nativeElement , fixture ) ;
398
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_VALID_CSS_CLASS ) ) . toBe ( true ) ;
399
+
400
+ dispatchInputEvent ( 'blur' , input . nativeElement , fixture ) ;
401
+ expect ( input . valid ) . toBe ( IgxInputState . INITIAL ) ;
402
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_INVALID_CSS_CLASS ) ) . toBe ( false ) ;
403
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_VALID_CSS_CLASS ) ) . toBe ( false ) ;
404
+
405
+ instance . isRequired = true ;
406
+ fixture . detectChanges ( ) ;
407
+
408
+ expect ( input . required ) . toBe ( true ) ;
409
+
410
+ expect ( inputGroup . isRequired ) . toBeTruthy ( ) ;
411
+ expect ( input . valid ) . toBe ( IgxInputState . INITIAL ) ;
412
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_REQUIRED_CSS_CLASS ) ) . toBe ( true ) ;
413
+
414
+ dispatchInputEvent ( 'focus' , input . nativeElement , fixture ) ;
415
+ expect ( input . valid ) . toBe ( IgxInputState . INITIAL ) ;
416
+
417
+ input . value = '' ;
418
+ dispatchInputEvent ( 'input' , input . nativeElement , fixture ) ;
419
+ dispatchInputEvent ( 'blur' , input . nativeElement , fixture ) ;
420
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_INVALID_CSS_CLASS ) ) . toBe ( true ) ;
421
+
422
+ dispatchInputEvent ( 'focus' , input . nativeElement , fixture ) ;
423
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_INVALID_CSS_CLASS ) ) . toBe ( true ) ;
424
+
425
+ input . value = '123' ;
426
+ dispatchInputEvent ( 'input' , input . nativeElement , fixture ) ;
427
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_VALID_CSS_CLASS ) ) . toBe ( true ) ;
428
+
429
+ dispatchInputEvent ( 'blur' , input . nativeElement , fixture ) ;
430
+ expect ( input . valid ) . toBe ( IgxInputState . INITIAL ) ;
431
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_INVALID_CSS_CLASS ) ) . toBe ( false ) ;
432
+ expect ( inputGroup . element . nativeElement . classList . contains ( INPUT_GROUP_VALID_CSS_CLASS ) ) . toBe ( false ) ;
433
+ } ) ;
322
434
} ) ;
323
435
324
436
@Component ( { template : `
@@ -529,6 +641,26 @@ class ReactiveFormComponent {
529
641
}
530
642
}
531
643
644
+ @Component ( { template : `<igx-input-group>
645
+ <label for="test" igxLabel>Test</label>
646
+ <input name="test" type="text" igxInput [(ngModel)]="data" [required]="isRequired"/>
647
+ </igx-input-group>
648
+ <igx-input-group>
649
+ <label for="test" igxLabel>Test</label>
650
+ <input name="test" type="text" igxInput [value]="data1" [required]="isRequired"/>
651
+ </igx-input-group>` } )
652
+ class ToggleRequiredWithNgModelInputComponent {
653
+ @ViewChildren ( IgxInputDirective )
654
+ public igxInputs : QueryList < IgxInputDirective > ;
655
+
656
+ @ViewChildren ( IgxInputGroupComponent )
657
+ public igxInputGroups : QueryList < IgxInputGroupComponent > ;
658
+
659
+ public data = '' ;
660
+ public data1 = '' ;
661
+ public isRequired = false ;
662
+ }
663
+
532
664
function testRequiredValidation ( inputElement , fixture ) {
533
665
dispatchInputEvent ( 'focus' , inputElement , fixture ) ;
534
666
inputElement . value = 'test' ;
0 commit comments