1
1
import { async , ComponentFixture , TestBed , fakeAsync , tick } from '@angular/core/testing' ;
2
2
import {
3
- ChangeDetectionStrategy ,
4
3
Component ,
5
4
DebugElement ,
6
- OnInit ,
7
5
QueryList ,
8
6
ViewChild ,
9
7
ViewChildren ,
10
8
} from '@angular/core' ;
11
9
import {
12
- ControlValueAccessor ,
13
10
FormControl ,
14
- FormGroup ,
15
- FormGroupDirective ,
16
11
FormsModule ,
17
- NG_VALUE_ACCESSOR ,
18
12
ReactiveFormsModule ,
19
- Validators ,
20
13
} from '@angular/forms' ;
21
14
import { By } from '@angular/platform-browser' ;
22
15
import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
23
16
import { MdChipList , MdChipsModule } from './index' ;
24
17
import { FocusKeyManager } from '@angular/cdk/a11y' ;
25
- import { createKeyboardEvent , dispatchFakeEvent , dispatchKeyboardEvent , wrappedErrorMessage } from '@angular/cdk/testing' ;
18
+ import { createKeyboardEvent , dispatchFakeEvent , dispatchKeyboardEvent } from '@angular/cdk/testing' ;
26
19
27
20
import { MdInputModule } from '../input/index' ;
28
- import { LEFT_ARROW , RIGHT_ARROW , BACKSPACE , DELETE , SPACE , TAB , ENTER } from '../core/keyboard/keycodes' ;
21
+ import { LEFT_ARROW , RIGHT_ARROW , BACKSPACE , DELETE , SPACE , TAB , ENTER }
22
+ from '../core/keyboard/keycodes' ;
29
23
import { Directionality } from '../core' ;
30
- import { extendObject } from '../core/util/object-extend' ;
31
24
import { MdFormFieldModule } from '../form-field/index' ;
32
25
import { MdChip } from './chip' ;
33
26
import { MdChipInputEvent } from './chip-input' ;
@@ -336,7 +329,6 @@ describe('MdChipList', () => {
336
329
} ) ;
337
330
338
331
describe ( 'selection logic' , ( ) => {
339
- let fixture : ComponentFixture < BasicChipList > ;
340
332
let formField : HTMLElement ;
341
333
let nativeChips : HTMLElement [ ] ;
342
334
@@ -398,23 +390,21 @@ describe('MdChipList', () => {
398
390
} ) ;
399
391
400
392
it ( 'should not select disabled chips' , ( ) => {
401
- const chips = fixture . componentInstance . chips . toArray ( ) ;
402
-
393
+ const array = chips . toArray ( ) ;
403
394
const disabledChip = nativeChips [ 2 ] ;
404
395
dispatchKeyboardEvent ( disabledChip , 'keydown' , SPACE ) ;
405
396
fixture . detectChanges ( ) ;
406
397
407
398
expect ( fixture . componentInstance . chipList . value )
408
399
. toBeUndefined ( 'Expect value to be undefined' ) ;
409
- expect ( chips [ 2 ] . selected ) . toBeFalsy ( 'Expect disabled chip not selected' ) ;
400
+ expect ( array [ 2 ] . selected ) . toBeFalsy ( 'Expect disabled chip not selected' ) ;
410
401
expect ( fixture . componentInstance . chipList . selected . length )
411
402
. toBe ( 0 , 'Expect no selected chips' ) ;
412
403
} ) ;
413
404
414
405
} ) ;
415
406
416
407
describe ( 'forms integration' , ( ) => {
417
- let fixture : ComponentFixture < BasicChipList > ;
418
408
let formField : HTMLElement ;
419
409
let nativeChips : HTMLElement [ ] ;
420
410
@@ -425,33 +415,33 @@ describe('MdChipList', () => {
425
415
formField = fixture . debugElement . query ( By . css ( '.mat-form-field' ) ) . nativeElement ;
426
416
nativeChips = fixture . debugElement . queryAll ( By . css ( 'md-chip' ) )
427
417
. map ( ( chip ) => chip . nativeElement ) ;
418
+ chips = fixture . componentInstance . chips ;
428
419
} ) ;
429
420
430
421
it ( 'should take an initial view value with reactive forms' , ( ) => {
431
422
fixture . componentInstance . control = new FormControl ( [ 'pizza-1' ] ) ;
432
423
fixture . detectChanges ( ) ;
433
424
434
- const chipList = fixture . componentInstance . chipList ;
435
- const chips = fixture . componentInstance . chips . toArray ( ) ;
425
+ const array = chips . toArray ( ) ;
436
426
437
- expect ( chips [ 1 ] . selected ) . toBeTruthy ( 'Expect pizza-1 chip to be selected' ) ;
427
+ expect ( array [ 1 ] . selected ) . toBeTruthy ( 'Expect pizza-1 chip to be selected' ) ;
438
428
439
429
dispatchKeyboardEvent ( nativeChips [ 1 ] , 'keydown' , SPACE ) ;
440
430
fixture . detectChanges ( ) ;
441
431
442
- expect ( chips [ 1 ] . selected ) . toBeFalsy ( 'Expect chip to be not selected after toggle selected' ) ;
432
+ expect ( array [ 1 ] . selected ) . toBeFalsy ( 'Expect chip to be not selected after toggle selected' ) ;
443
433
} ) ;
444
434
445
435
it ( 'should set the view value from the form' , ( ) => {
446
436
const chipList = fixture . componentInstance . chipList ;
447
- const chips = fixture . componentInstance . chips . toArray ( ) ;
437
+ const array = chips . toArray ( ) ;
448
438
449
439
expect ( chipList . value ) . toBeFalsy ( 'Expect chip list to have no initial value' ) ;
450
440
451
441
fixture . componentInstance . control . setValue ( [ 'pizza-1' ] ) ;
452
442
fixture . detectChanges ( ) ;
453
443
454
- expect ( chips [ 1 ] . selected ) . toBeTruthy ( 'Expect chip to be selected' ) ;
444
+ expect ( array [ 1 ] . selected ) . toBeTruthy ( 'Expect chip to be selected' ) ;
455
445
} ) ;
456
446
457
447
it ( 'should update the form value when the view changes' , ( ) => {
@@ -467,42 +457,37 @@ describe('MdChipList', () => {
467
457
} ) ;
468
458
469
459
it ( 'should clear the selection when a nonexistent option value is selected' , ( ) => {
470
- const chipList = fixture . componentInstance . chipList ;
471
- const chips = fixture . componentInstance . chips . toArray ( ) ;
460
+ const array = chips . toArray ( ) ;
472
461
473
462
fixture . componentInstance . control . setValue ( [ 'pizza-1' ] ) ;
474
463
fixture . detectChanges ( ) ;
475
464
476
- expect ( chips [ 1 ] . selected )
465
+ expect ( array [ 1 ] . selected )
477
466
. toBeTruthy ( `Expected chip with the value to be selected.` ) ;
478
467
479
468
fixture . componentInstance . control . setValue ( [ 'gibberish' ] ) ;
480
469
481
470
fixture . detectChanges ( ) ;
482
471
483
- expect ( chips [ 1 ] . selected )
472
+ expect ( array [ 1 ] . selected )
484
473
. toBeFalsy ( `Expected chip with the old value not to be selected.` ) ;
485
474
} ) ;
486
475
487
476
488
477
it ( 'should clear the selection when the control is reset' , ( ) => {
489
- const chipList = fixture . componentInstance . chipList ;
490
- const chips = fixture . componentInstance . chips . toArray ( ) ;
478
+ const array = chips . toArray ( ) ;
491
479
492
480
fixture . componentInstance . control . setValue ( [ 'pizza-1' ] ) ;
493
481
fixture . detectChanges ( ) ;
494
482
495
483
fixture . componentInstance . control . reset ( ) ;
496
484
fixture . detectChanges ( ) ;
497
485
498
- expect ( chips [ 1 ] . selected )
486
+ expect ( array [ 1 ] . selected )
499
487
. toBeFalsy ( `Expected chip with the old value not to be selected.` ) ;
500
488
} ) ;
501
489
502
490
it ( 'should set the control to touched when the chip list is touched' , ( ) => {
503
- const chipList = fixture . componentInstance . chipList ;
504
- const chips = fixture . componentInstance . chips . toArray ( ) ;
505
-
506
491
expect ( fixture . componentInstance . control . touched )
507
492
. toBe ( false , 'Expected the control to start off as untouched.' ) ;
508
493
@@ -514,9 +499,6 @@ describe('MdChipList', () => {
514
499
} ) ;
515
500
516
501
it ( 'should not set touched when a disabled chip list is touched' , ( ) => {
517
- const chipList = fixture . componentInstance . chipList ;
518
- const chips = fixture . componentInstance . chips . toArray ( ) ;
519
-
520
502
expect ( fixture . componentInstance . control . touched )
521
503
. toBe ( false , 'Expected the control to start off as untouched.' ) ;
522
504
@@ -579,7 +561,6 @@ describe('MdChipList', () => {
579
561
} ) ;
580
562
581
563
describe ( 'chip list with chip input' , ( ) => {
582
- let fixture : ComponentFixture < InputChipList > ;
583
564
let formField : HTMLElement ;
584
565
let nativeChips : HTMLElement [ ] ;
585
566
@@ -596,27 +577,25 @@ describe('MdChipList', () => {
596
577
fixture . componentInstance . control = new FormControl ( [ 'pizza-1' ] ) ;
597
578
fixture . detectChanges ( ) ;
598
579
599
- const chipList = fixture . componentInstance . chipList ;
600
- const chips = fixture . componentInstance . chips . toArray ( ) ;
580
+ const array = fixture . componentInstance . chips . toArray ( ) ;
601
581
602
- expect ( chips [ 1 ] . selected ) . toBeTruthy ( 'Expect pizza-1 chip to be selected' ) ;
582
+ expect ( array [ 1 ] . selected ) . toBeTruthy ( 'Expect pizza-1 chip to be selected' ) ;
603
583
604
584
dispatchKeyboardEvent ( nativeChips [ 1 ] , 'keydown' , SPACE ) ;
605
585
fixture . detectChanges ( ) ;
606
586
607
- expect ( chips [ 1 ] . selected ) . toBeFalsy ( 'Expect chip to be not selected after toggle selected' ) ;
587
+ expect ( array [ 1 ] . selected ) . toBeFalsy ( 'Expect chip to be not selected after toggle selected' ) ;
608
588
} ) ;
609
589
610
590
it ( 'should set the view value from the form' , ( ) => {
611
- const chipList = fixture . componentInstance . chipList ;
612
- const chips = fixture . componentInstance . chips . toArray ( ) ;
591
+ const array = fixture . componentInstance . chips . toArray ( ) ;
613
592
614
- expect ( chips [ 1 ] . selected ) . toBeFalsy ( 'Expect chip to not be selected' ) ;
593
+ expect ( array [ 1 ] . selected ) . toBeFalsy ( 'Expect chip to not be selected' ) ;
615
594
616
595
fixture . componentInstance . control . setValue ( [ 'pizza-1' ] ) ;
617
596
fixture . detectChanges ( ) ;
618
597
619
- expect ( chips [ 1 ] . selected ) . toBeTruthy ( 'Expect chip to be selected' ) ;
598
+ expect ( array [ 1 ] . selected ) . toBeTruthy ( 'Expect chip to be selected' ) ;
620
599
} ) ;
621
600
622
601
it ( 'should update the form value when the view changes' , ( ) => {
@@ -632,56 +611,52 @@ describe('MdChipList', () => {
632
611
} ) ;
633
612
634
613
it ( 'should clear the selection when a nonexistent option value is selected' , ( ) => {
635
- const chipList = fixture . componentInstance . chipList ;
636
- const chips = fixture . componentInstance . chips . toArray ( ) ;
614
+ const array = fixture . componentInstance . chips . toArray ( ) ;
637
615
638
616
fixture . componentInstance . control . setValue ( [ 'pizza-1' ] ) ;
639
617
fixture . detectChanges ( ) ;
640
618
641
- expect ( chips [ 1 ] . selected )
619
+ expect ( array [ 1 ] . selected )
642
620
. toBeTruthy ( `Expected chip with the value to be selected.` ) ;
643
621
644
622
fixture . componentInstance . control . setValue ( [ 'gibberish' ] ) ;
645
623
646
624
fixture . detectChanges ( ) ;
647
625
648
- expect ( chips [ 1 ] . selected )
626
+ expect ( array [ 1 ] . selected )
649
627
. toBeFalsy ( `Expected chip with the old value not to be selected.` ) ;
650
628
} ) ;
651
629
652
630
653
631
it ( 'should clear the selection when the control is reset' , ( ) => {
654
- const chipList = fixture . componentInstance . chipList ;
655
- const chips = fixture . componentInstance . chips . toArray ( ) ;
632
+ const array = fixture . componentInstance . chips . toArray ( ) ;
656
633
657
634
fixture . componentInstance . control . setValue ( [ 'pizza-1' ] ) ;
658
635
fixture . detectChanges ( ) ;
659
636
660
637
fixture . componentInstance . control . reset ( ) ;
661
638
fixture . detectChanges ( ) ;
662
639
663
- expect ( chips [ 1 ] . selected )
640
+ expect ( array [ 1 ] . selected )
664
641
. toBeFalsy ( `Expected chip with the old value not to be selected.` ) ;
665
642
} ) ;
666
643
667
- it ( 'should set the control to touched when the chip list is touched' , ( ) => {
668
- const chipList = fixture . componentInstance . chipList ;
669
- const chips = fixture . componentInstance . chips . toArray ( ) ;
670
-
644
+ it ( 'should set the control to touched when the chip list is touched' , async ( ( ) => {
671
645
expect ( fixture . componentInstance . control . touched )
672
646
. toBe ( false , 'Expected the control to start off as untouched.' ) ;
673
647
674
648
const nativeChipList = fixture . debugElement . query ( By . css ( '.mat-chip-list' ) ) . nativeElement ;
649
+ const nativeInput = fixture . debugElement . query ( By . css ( '.mat-chip-input' ) ) . nativeElement ;
650
+
675
651
dispatchFakeEvent ( nativeChipList , 'blur' ) ;
676
652
677
- expect ( fixture . componentInstance . control . touched )
678
- . toBe ( true , 'Expected the control to be touched.' ) ;
679
- } ) ;
653
+ fixture . whenStable ( ) . then ( ( ) => {
654
+ expect ( fixture . componentInstance . control . touched )
655
+ . toBe ( true , 'Expected the control to be touched.' ) ;
656
+ } ) ;
657
+ } ) ) ;
680
658
681
659
it ( 'should not set touched when a disabled chip list is touched' , ( ) => {
682
- const chipList = fixture . componentInstance . chipList ;
683
- const chips = fixture . componentInstance . chips . toArray ( ) ;
684
-
685
660
expect ( fixture . componentInstance . control . touched )
686
661
. toBe ( false , 'Expected the control to start off as untouched.' ) ;
687
662
0 commit comments