@@ -11,7 +11,7 @@ import {
11
11
import { FormsModule , FormControl , ReactiveFormsModule } from '@angular/forms' ;
12
12
import { Component , ViewChild , ViewChildren , QueryList , ElementRef } from '@angular/core' ;
13
13
import { ThySelectModule } from './module' ;
14
- import { ThySelectCustomComponent } from './custom-select.component' ;
14
+ import { ThySelectCustomComponent , SelectMode } from './custom-select.component' ;
15
15
import { ThyOptionComponent } from './option.component' ;
16
16
import { By } from '@angular/platform-browser' ;
17
17
import { UpdateHostClassService } from '../shared' ;
@@ -576,7 +576,7 @@ describe('ThyCustomSelect', () => {
576
576
expect ( fixture . componentInstance . select . panelOpen ) . toBe ( true ) ;
577
577
expect ( overlayContainerElement . textContent ) . toContain ( 'Pizza' ) ;
578
578
} ) ) ;
579
- it ( 'shoule close select when mouse leave select-container' , fakeAsync ( ( ) => {
579
+ it ( 'should close select when mouse leave select-container' , fakeAsync ( ( ) => {
580
580
const fixture = TestBed . createComponent ( SelectWithHoverTriggerComponent ) ;
581
581
fixture . detectChanges ( ) ;
582
582
const select = fixture . debugElement . nativeElement . querySelector ( 'thy-custom-select' ) ;
@@ -595,7 +595,180 @@ describe('ThyCustomSelect', () => {
595
595
} ) ) ;
596
596
} ) ;
597
597
598
- describe ( 'cselect expand status change' , ( ) => { } ) ;
598
+ describe ( 'thyMode logic' , ( ) => {
599
+ beforeEach ( async ( ( ) => {
600
+ configureThyCustomSelectTestingModule ( [ SelectWithThyModeComponent ] ) ;
601
+ } ) ) ;
602
+ it ( 'should not close the panel when an item is clicked and thyMode is multiple' , fakeAsync ( ( ) => {
603
+ const fixture = TestBed . createComponent ( SelectWithThyModeComponent ) ;
604
+ fixture . detectChanges ( ) ;
605
+ const trigger = fixture . debugElement . query ( By . css ( '.form-control-custom' ) ) . nativeElement ;
606
+
607
+ trigger . click ( ) ;
608
+ fixture . detectChanges ( ) ;
609
+ flush ( ) ;
610
+
611
+ const option = overlayContainerElement . querySelector ( 'thy-option' ) as HTMLElement ;
612
+ option . click ( ) ;
613
+ fixture . detectChanges ( ) ;
614
+ flush ( ) ;
615
+
616
+ expect ( fixture . componentInstance . select . panelOpen ) . toBe ( true ) ;
617
+ } ) ) ;
618
+
619
+ it ( 'should close expand when thyMode change to empty' , fakeAsync ( ( ) => {
620
+ const fixture = TestBed . createComponent ( SelectWithThyModeComponent ) ;
621
+ fixture . detectChanges ( ) ;
622
+ fixture . componentInstance . selectMode = '' ;
623
+ fixture . detectChanges ( ) ;
624
+ flush ( ) ;
625
+
626
+ const trigger = fixture . debugElement . query ( By . css ( '.form-control-custom' ) ) . nativeElement ;
627
+ trigger . click ( ) ;
628
+ fixture . detectChanges ( ) ;
629
+ flush ( ) ;
630
+
631
+ const option = overlayContainerElement . querySelector ( 'thy-option' ) as HTMLElement ;
632
+ option . click ( ) ;
633
+ fixture . detectChanges ( ) ;
634
+ flush ( ) ;
635
+
636
+ expect ( fixture . componentInstance . select . panelOpen ) . toBe ( false ) ;
637
+ } ) ) ;
638
+
639
+ it ( 'should clear selected status when thyMode change' , fakeAsync ( ( ) => {
640
+ const fixture = TestBed . createComponent ( SelectWithThyModeComponent ) ;
641
+ fixture . detectChanges ( ) ;
642
+
643
+ const trigger = fixture . debugElement . query ( By . css ( '.form-control-custom' ) ) . nativeElement ;
644
+ trigger . click ( ) ;
645
+ fixture . detectChanges ( ) ;
646
+ flush ( ) ;
647
+
648
+ const optionComponents = fixture . componentInstance . options . toArray ( ) ;
649
+ const options = overlayContainerElement . querySelectorAll ( 'thy-option' ) ;
650
+
651
+ ( options . item ( 0 ) as HTMLElement ) . click ( ) ;
652
+ ( options . item ( 1 ) as HTMLElement ) . click ( ) ;
653
+
654
+ const backdrop = overlayContainerElement . querySelector ( '.cdk-overlay-backdrop' ) as HTMLElement ;
655
+ backdrop . click ( ) ;
656
+
657
+ fixture . detectChanges ( ) ;
658
+ flush ( ) ;
659
+
660
+ expect ( optionComponents [ 0 ] . selected ) . toBe ( true ) ;
661
+ expect ( optionComponents [ 1 ] . selected ) . toBe ( true ) ;
662
+
663
+ fixture . componentInstance . selectMode = '' ;
664
+ fixture . detectChanges ( ) ;
665
+ flush ( ) ;
666
+
667
+ expect ( optionComponents [ 0 ] . selected ) . toBe ( false ) ;
668
+ expect ( optionComponents [ 1 ] . selected ) . toBe ( false ) ;
669
+
670
+ trigger . click ( ) ;
671
+ fixture . detectChanges ( ) ;
672
+ flush ( ) ;
673
+ ( options . item ( 0 ) as HTMLElement ) . click ( ) ;
674
+ expect ( optionComponents [ 0 ] . selected ) . toBe ( true ) ;
675
+
676
+ fixture . componentInstance . selectMode = 'multiple' ;
677
+ fixture . detectChanges ( ) ;
678
+ flush ( ) ;
679
+
680
+ expect ( optionComponents [ 0 ] . selected ) . toBe ( false ) ;
681
+ } ) ) ;
682
+
683
+ it ( 'should not clear status when the thyMode value is not change' , fakeAsync ( ( ) => {
684
+ const fixture = TestBed . createComponent ( SelectWithThyModeComponent ) ;
685
+ fixture . detectChanges ( ) ;
686
+
687
+ const trigger = fixture . debugElement . query ( By . css ( '.form-control-custom' ) ) . nativeElement ;
688
+ trigger . click ( ) ;
689
+ fixture . detectChanges ( ) ;
690
+ flush ( ) ;
691
+
692
+ const optionComponents = fixture . componentInstance . options . toArray ( ) ;
693
+ const options = overlayContainerElement . querySelectorAll ( 'thy-option' ) ;
694
+
695
+ ( options . item ( 0 ) as HTMLElement ) . click ( ) ;
696
+ ( options . item ( 1 ) as HTMLElement ) . click ( ) ;
697
+
698
+ const backdrop = overlayContainerElement . querySelector ( '.cdk-overlay-backdrop' ) as HTMLElement ;
699
+ backdrop . click ( ) ;
700
+
701
+ fixture . detectChanges ( ) ;
702
+ flush ( ) ;
703
+
704
+ expect ( optionComponents [ 0 ] . selected ) . toBe ( true ) ;
705
+ expect ( optionComponents [ 1 ] . selected ) . toBe ( true ) ;
706
+
707
+ fixture . componentInstance . selectMode = 'multiple' ;
708
+ fixture . detectChanges ( ) ;
709
+ flush ( ) ;
710
+
711
+ expect ( optionComponents [ 0 ] . selected ) . toBe ( true ) ;
712
+ expect ( optionComponents [ 1 ] . selected ) . toBe ( true ) ;
713
+ } ) ) ;
714
+
715
+ it ( 'should apply default mode when thyMode change to empty' , fakeAsync ( ( ) => {
716
+ const fixture = TestBed . createComponent ( SelectWithThyModeComponent ) ;
717
+ fixture . detectChanges ( ) ;
718
+ fixture . componentInstance . selectMode = '' ;
719
+ fixture . detectChanges ( ) ;
720
+ flush ( ) ;
721
+
722
+ const trigger = fixture . debugElement . query ( By . css ( '.form-control-custom' ) ) . nativeElement ;
723
+ trigger . click ( ) ;
724
+ fixture . detectChanges ( ) ;
725
+ flush ( ) ;
726
+
727
+ const options = overlayContainerElement . querySelectorAll ( 'thy-option' ) ;
728
+ ( options . item ( 0 ) as HTMLElement ) . click ( ) ;
729
+ fixture . detectChanges ( ) ;
730
+ flush ( ) ;
731
+ const optionComponents = fixture . componentInstance . options . toArray ( ) ;
732
+ expect ( optionComponents [ 0 ] . selected ) . toBe ( true ) ;
733
+ ( options . item ( 1 ) as HTMLElement ) . click ( ) ;
734
+ fixture . detectChanges ( ) ;
735
+ flush ( ) ;
736
+ expect ( optionComponents [ 0 ] . selected ) . toBe ( false ) ;
737
+ expect ( optionComponents [ 1 ] . selected ) . toBe ( true ) ;
738
+ } ) ) ;
739
+
740
+ it ( 'should apply multiple mode when thyMode change to multiple' , fakeAsync ( ( ) => {
741
+ const fixture = TestBed . createComponent ( SelectWithThyModeComponent ) ;
742
+ fixture . detectChanges ( ) ;
743
+ fixture . componentInstance . selectMode = '' ;
744
+ fixture . detectChanges ( ) ;
745
+ flush ( ) ;
746
+
747
+ const trigger = fixture . debugElement . query ( By . css ( '.form-control-custom' ) ) . nativeElement ;
748
+ trigger . click ( ) ;
749
+ fixture . detectChanges ( ) ;
750
+ flush ( ) ;
751
+
752
+ const options = overlayContainerElement . querySelectorAll ( 'thy-option' ) ;
753
+ ( options . item ( 0 ) as HTMLElement ) . click ( ) ;
754
+ fixture . detectChanges ( ) ;
755
+ flush ( ) ;
756
+
757
+ fixture . componentInstance . selectMode = 'multiple' ;
758
+ fixture . detectChanges ( ) ;
759
+ flush ( ) ;
760
+
761
+ const optionComponents = fixture . componentInstance . options . toArray ( ) ;
762
+
763
+ ( options . item ( 0 ) as HTMLElement ) . click ( ) ;
764
+ ( options . item ( 1 ) as HTMLElement ) . click ( ) ;
765
+ fixture . detectChanges ( ) ;
766
+ flush ( ) ;
767
+
768
+ expect ( optionComponents [ 0 ] . selected ) . toBe ( true ) ;
769
+ expect ( optionComponents [ 1 ] . selected ) . toBe ( true ) ;
770
+ } ) ) ;
771
+ } ) ;
599
772
} ) ;
600
773
601
774
@Component ( {
@@ -961,3 +1134,31 @@ class SelectWithExpandStatusComponent {
961
1134
thyOnExpandStatusChange = jasmine . createSpy ( 'thyOnExpandStatusChange callback' ) ;
962
1135
@ViewChild ( ThySelectCustomComponent ) select : ThySelectCustomComponent ;
963
1136
}
1137
+
1138
+ @Component ( {
1139
+ template : `
1140
+ <form thyForm name="demoForm" #demoForm="ngForm">
1141
+ <thy-custom-select placeholder="Food" [(ngModel)]="selectedFoods" name="food" [thyMode]="selectMode">
1142
+ <thy-option
1143
+ *ngFor="let food of foods"
1144
+ [thyValue]="food.value"
1145
+ [thyLabelText]="food.viewValue"
1146
+ ></thy-option>
1147
+ </thy-custom-select>
1148
+ </form>
1149
+ `
1150
+ } )
1151
+ class SelectWithThyModeComponent {
1152
+ foods : any [ ] = [
1153
+ { value : [ 'steak-0' , 'steak-1' ] , viewValue : 'Steak' } ,
1154
+ { value : [ 'pizza-1' , 'pizza-2' ] , viewValue : 'Pizza' } ,
1155
+ { value : [ 'tacos-2' , 'tacos-3' ] , viewValue : 'Tacos' }
1156
+ ] ;
1157
+
1158
+ selectMode : SelectMode = 'multiple' ;
1159
+
1160
+ selectedFoods = null ;
1161
+
1162
+ @ViewChild ( ThySelectCustomComponent ) select : ThySelectCustomComponent ;
1163
+ @ViewChildren ( ThyOptionComponent ) options : QueryList < ThyOptionComponent > ;
1164
+ }
0 commit comments