@@ -63,7 +63,10 @@ describe('MdSelect', () => {
6363 ResetValuesSelect ,
6464 FalsyValueSelect ,
6565 SelectWithGroups ,
66- InvalidSelectInForm
66+ InvalidSelectInForm ,
67+ BasicSelectWithoutForms ,
68+ BasicSelectWithoutFormsPreselected ,
69+ BasicSelectWithoutFormsMultiple
6770 ] ,
6871 providers : [
6972 { provide : OverlayContainer , useFactory : ( ) => {
@@ -706,6 +709,138 @@ describe('MdSelect', () => {
706709
707710 } ) ;
708711
712+ describe ( 'selection without Angular forms' , ( ) => {
713+ it ( 'should set the value when options are clicked' , ( ) => {
714+ const fixture = TestBed . createComponent ( BasicSelectWithoutForms ) ;
715+
716+ fixture . detectChanges ( ) ;
717+ expect ( fixture . componentInstance . selectedFood ) . toBeFalsy ( ) ;
718+
719+ const trigger = fixture . debugElement . query ( By . css ( '.mat-select-trigger' ) ) . nativeElement ;
720+
721+ trigger . click ( ) ;
722+ fixture . detectChanges ( ) ;
723+
724+ ( overlayContainerElement . querySelector ( 'md-option' ) as HTMLElement ) . click ( ) ;
725+ fixture . detectChanges ( ) ;
726+
727+ expect ( fixture . componentInstance . selectedFood ) . toBe ( 'steak-0' ) ;
728+ expect ( fixture . componentInstance . select . value ) . toBe ( 'steak-0' ) ;
729+ expect ( trigger . textContent ) . toContain ( 'Steak' ) ;
730+
731+ trigger . click ( ) ;
732+ fixture . detectChanges ( ) ;
733+
734+ ( overlayContainerElement . querySelectorAll ( 'md-option' ) [ 2 ] as HTMLElement ) . click ( ) ;
735+ fixture . detectChanges ( ) ;
736+
737+ expect ( fixture . componentInstance . selectedFood ) . toBe ( 'sandwich-2' ) ;
738+ expect ( fixture . componentInstance . select . value ) . toBe ( 'sandwich-2' ) ;
739+ expect ( trigger . textContent ) . toContain ( 'Sandwich' ) ;
740+ } ) ;
741+
742+ it ( 'should mark options as selected when the value is set' , ( ) => {
743+ const fixture = TestBed . createComponent ( BasicSelectWithoutForms ) ;
744+
745+ fixture . detectChanges ( ) ;
746+ fixture . componentInstance . selectedFood = 'sandwich-2' ;
747+ fixture . detectChanges ( ) ;
748+
749+ const trigger = fixture . debugElement . query ( By . css ( '.mat-select-trigger' ) ) . nativeElement ;
750+ expect ( trigger . textContent ) . toContain ( 'Sandwich' ) ;
751+
752+ trigger . click ( ) ;
753+ fixture . detectChanges ( ) ;
754+
755+ const option = overlayContainerElement . querySelectorAll ( 'md-option' ) [ 2 ] ;
756+
757+ expect ( option . classList ) . toContain ( 'mat-selected' ) ;
758+ expect ( fixture . componentInstance . select . value ) . toBe ( 'sandwich-2' ) ;
759+ } ) ;
760+
761+ it ( 'should reset the placeholder when a null value is set' , ( ) => {
762+ const fixture = TestBed . createComponent ( BasicSelectWithoutForms ) ;
763+
764+ fixture . detectChanges ( ) ;
765+ expect ( fixture . componentInstance . selectedFood ) . toBeFalsy ( ) ;
766+
767+ const trigger = fixture . debugElement . query ( By . css ( '.mat-select-trigger' ) ) . nativeElement ;
768+
769+ trigger . click ( ) ;
770+ fixture . detectChanges ( ) ;
771+
772+ ( overlayContainerElement . querySelector ( 'md-option' ) as HTMLElement ) . click ( ) ;
773+ fixture . detectChanges ( ) ;
774+
775+ expect ( fixture . componentInstance . selectedFood ) . toBe ( 'steak-0' ) ;
776+ expect ( fixture . componentInstance . select . value ) . toBe ( 'steak-0' ) ;
777+ expect ( trigger . textContent ) . toContain ( 'Steak' ) ;
778+
779+ fixture . componentInstance . selectedFood = null ;
780+ fixture . detectChanges ( ) ;
781+
782+ expect ( fixture . componentInstance . select . value ) . toBeNull ( ) ;
783+ expect ( trigger . textContent ) . not . toContain ( 'Steak' ) ;
784+ } ) ;
785+
786+ it ( 'should reflect the preselected value' , async ( ( ) => {
787+ const fixture = TestBed . createComponent ( BasicSelectWithoutFormsPreselected ) ;
788+
789+ fixture . detectChanges ( ) ;
790+ fixture . whenStable ( ) . then ( ( ) => {
791+ const trigger = fixture . debugElement . query ( By . css ( '.mat-select-trigger' ) ) . nativeElement ;
792+
793+ fixture . detectChanges ( ) ;
794+ expect ( trigger . textContent ) . toContain ( 'Pizza' ) ;
795+
796+ trigger . click ( ) ;
797+ fixture . detectChanges ( ) ;
798+
799+ const option = overlayContainerElement . querySelectorAll ( 'md-option' ) [ 1 ] ;
800+
801+ expect ( option . classList ) . toContain ( 'mat-selected' ) ;
802+ expect ( fixture . componentInstance . select . value ) . toBe ( 'pizza-1' ) ;
803+ } ) ;
804+ } ) ) ;
805+
806+ it ( 'should be able to select multiple values' , ( ) => {
807+ const fixture = TestBed . createComponent ( BasicSelectWithoutFormsMultiple ) ;
808+
809+ fixture . detectChanges ( ) ;
810+ expect ( fixture . componentInstance . selectedFoods ) . toBeFalsy ( ) ;
811+
812+ const trigger = fixture . debugElement . query ( By . css ( '.mat-select-trigger' ) ) . nativeElement ;
813+
814+ trigger . click ( ) ;
815+ fixture . detectChanges ( ) ;
816+
817+ const options =
818+ overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
819+
820+ options [ 0 ] . click ( ) ;
821+ fixture . detectChanges ( ) ;
822+
823+ expect ( fixture . componentInstance . selectedFoods ) . toEqual ( [ 'steak-0' ] ) ;
824+ expect ( fixture . componentInstance . select . value ) . toEqual ( [ 'steak-0' ] ) ;
825+ expect ( trigger . textContent ) . toContain ( 'Steak' ) ;
826+
827+ options [ 2 ] . click ( ) ;
828+ fixture . detectChanges ( ) ;
829+
830+ expect ( fixture . componentInstance . selectedFoods ) . toEqual ( [ 'steak-0' , 'sandwich-2' ] ) ;
831+ expect ( fixture . componentInstance . select . value ) . toEqual ( [ 'steak-0' , 'sandwich-2' ] ) ;
832+ expect ( trigger . textContent ) . toContain ( 'Steak, Sandwich' ) ;
833+
834+ options [ 1 ] . click ( ) ;
835+ fixture . detectChanges ( ) ;
836+
837+ expect ( fixture . componentInstance . selectedFoods ) . toEqual ( [ 'steak-0' , 'pizza-1' , 'sandwich-2' ] ) ;
838+ expect ( fixture . componentInstance . select . value ) . toEqual ( [ 'steak-0' , 'pizza-1' , 'sandwich-2' ] ) ;
839+ expect ( trigger . textContent ) . toContain ( 'Steak, Pizza, Sandwich' ) ;
840+ } ) ;
841+
842+ } ) ;
843+
709844 describe ( 'disabled behavior' , ( ) => {
710845
711846 it ( 'should disable itself when control is disabled programmatically' , ( ) => {
@@ -2361,7 +2496,6 @@ describe('MdSelect', () => {
23612496
23622497 } ) ;
23632498
2364-
23652499 describe ( 'reset values' , ( ) => {
23662500 let fixture : ComponentFixture < ResetValuesSelect > ;
23672501 let trigger : HTMLElement ;
@@ -2892,3 +3026,63 @@ class SelectWithGroups {
28923026class InvalidSelectInForm {
28933027 value : any ;
28943028}
3029+
3030+
3031+ @Component ( {
3032+ template : `
3033+ <md-select placeholder="Food" [(value)]="selectedFood">
3034+ <md-option *ngFor="let food of foods" [value]="food.value">
3035+ {{ food.viewValue }}
3036+ </md-option>
3037+ </md-select>
3038+ `
3039+ } )
3040+ class BasicSelectWithoutForms {
3041+ selectedFood : string | null ;
3042+ foods : any [ ] = [
3043+ { value : 'steak-0' , viewValue : 'Steak' } ,
3044+ { value : 'pizza-1' , viewValue : 'Pizza' } ,
3045+ { value : 'sandwich-2' , viewValue : 'Sandwich' } ,
3046+ ] ;
3047+
3048+ @ViewChild ( MdSelect ) select : MdSelect ;
3049+ }
3050+
3051+ @Component ( {
3052+ template : `
3053+ <md-select placeholder="Food" [(value)]="selectedFood">
3054+ <md-option *ngFor="let food of foods" [value]="food.value">
3055+ {{ food.viewValue }}
3056+ </md-option>
3057+ </md-select>
3058+ `
3059+ } )
3060+ class BasicSelectWithoutFormsPreselected {
3061+ selectedFood = 'pizza-1' ;
3062+ foods : any [ ] = [
3063+ { value : 'steak-0' , viewValue : 'Steak' } ,
3064+ { value : 'pizza-1' , viewValue : 'Pizza' } ,
3065+ ] ;
3066+
3067+ @ViewChild ( MdSelect ) select : MdSelect ;
3068+ }
3069+
3070+ @Component ( {
3071+ template : `
3072+ <md-select placeholder="Food" [(value)]="selectedFoods" multiple>
3073+ <md-option *ngFor="let food of foods" [value]="food.value">
3074+ {{ food.viewValue }}
3075+ </md-option>
3076+ </md-select>
3077+ `
3078+ } )
3079+ class BasicSelectWithoutFormsMultiple {
3080+ selectedFoods : string [ ] ;
3081+ foods : any [ ] = [
3082+ { value : 'steak-0' , viewValue : 'Steak' } ,
3083+ { value : 'pizza-1' , viewValue : 'Pizza' } ,
3084+ { value : 'sandwich-2' , viewValue : 'Sandwich' } ,
3085+ ] ;
3086+
3087+ @ViewChild ( MdSelect ) select : MdSelect ;
3088+ }
0 commit comments