11import { TestBed , async , ComponentFixture } from '@angular/core/testing' ;
22import { By } from '@angular/platform-browser' ;
3- import { Component , QueryList , ViewChild , ViewChildren } from '@angular/core' ;
3+ import { Component , DebugElement , QueryList , ViewChild , ViewChildren } from '@angular/core' ;
44import { MdSelectModule } from './index' ;
55import { OverlayContainer } from '../core/overlay/overlay-container' ;
66import { MdSelect } from './select' ;
@@ -16,7 +16,7 @@ describe('MdSelect', () => {
1616 beforeEach ( async ( ( ) => {
1717 TestBed . configureTestingModule ( {
1818 imports : [ MdSelectModule . forRoot ( ) , ReactiveFormsModule , FormsModule ] ,
19- declarations : [ BasicSelect , NgModelSelect ] ,
19+ declarations : [ BasicSelect , NgModelSelect , ManySelects ] ,
2020 providers : [
2121 { provide : OverlayContainer , useFactory : ( ) => {
2222 overlayContainerElement = document . createElement ( 'div' ) ;
@@ -547,17 +547,14 @@ describe('MdSelect', () => {
547547 } ) ;
548548
549549 describe ( 'accessibility' , ( ) => {
550- let fixture : ComponentFixture < BasicSelect > ;
551-
552- beforeEach ( ( ) => {
553- fixture = TestBed . createComponent ( BasicSelect ) ;
554- fixture . detectChanges ( ) ;
555- } ) ;
556550
557551 describe ( 'for select' , ( ) => {
552+ let fixture : ComponentFixture < BasicSelect > ;
558553 let select : HTMLElement ;
559554
560555 beforeEach ( ( ) => {
556+ fixture = TestBed . createComponent ( BasicSelect ) ;
557+ fixture . detectChanges ( ) ;
561558 select = fixture . debugElement . query ( By . css ( 'md-select' ) ) . nativeElement ;
562559 } ) ;
563560
@@ -614,14 +611,16 @@ describe('MdSelect', () => {
614611 expect ( select . getAttribute ( 'tabindex' ) ) . toEqual ( '0' ) ;
615612 } ) ;
616613
617-
618614 } ) ;
619615
620616 describe ( 'for options' , ( ) => {
617+ let fixture : ComponentFixture < BasicSelect > ;
621618 let trigger : HTMLElement ;
622619 let options : NodeListOf < HTMLElement > ;
623620
624621 beforeEach ( ( ) => {
622+ fixture = TestBed . createComponent ( BasicSelect ) ;
623+ fixture . detectChanges ( ) ;
625624 trigger = fixture . debugElement . query ( By . css ( '.md-select-trigger' ) ) . nativeElement ;
626625 trigger . click ( ) ;
627626 fixture . detectChanges ( ) ;
@@ -673,6 +672,78 @@ describe('MdSelect', () => {
673672
674673 } ) ;
675674
675+ describe ( 'aria-owns' , ( ) => {
676+ let fixture : ComponentFixture < ManySelects > ;
677+ let triggers : DebugElement [ ] ;
678+ let options : NodeListOf < HTMLElement > ;
679+
680+ beforeEach ( ( ) => {
681+ fixture = TestBed . createComponent ( ManySelects ) ;
682+ fixture . detectChanges ( ) ;
683+ triggers = fixture . debugElement . queryAll ( By . css ( '.md-select-trigger' ) ) ;
684+
685+ triggers [ 0 ] . nativeElement . click ( ) ;
686+ fixture . detectChanges ( ) ;
687+
688+ options =
689+ overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
690+ } ) ;
691+
692+ it ( 'should set aria-owns properly' , async ( ( ) => {
693+ const selects = fixture . debugElement . queryAll ( By . css ( 'md-select' ) ) ;
694+
695+ expect ( selects [ 0 ] . nativeElement . getAttribute ( 'aria-owns' ) )
696+ . toContain ( options [ 0 ] . id , `Expected aria-owns to contain IDs of its child options.` ) ;
697+ expect ( selects [ 0 ] . nativeElement . getAttribute ( 'aria-owns' ) )
698+ . toContain ( options [ 1 ] . id , `Expected aria-owns to contain IDs of its child options.` ) ;
699+
700+ const backdrop =
701+ overlayContainerElement . querySelector ( '.md-overlay-backdrop' ) as HTMLElement ;
702+ backdrop . click ( ) ;
703+ fixture . detectChanges ( ) ;
704+
705+ fixture . whenStable ( ) . then ( ( ) => {
706+ triggers [ 1 ] . nativeElement . click ( ) ;
707+
708+ fixture . detectChanges ( ) ;
709+ options =
710+ overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
711+ expect ( selects [ 1 ] . nativeElement . getAttribute ( 'aria-owns' ) )
712+ . toContain ( options [ 0 ] . id , `Expected aria-owns to contain IDs of its child options.` ) ;
713+ expect ( selects [ 1 ] . nativeElement . getAttribute ( 'aria-owns' ) )
714+ . toContain ( options [ 1 ] . id , `Expected aria-owns to contain IDs of its child options.` ) ;
715+ } ) ;
716+
717+ } ) ) ;
718+
719+ it ( 'should set the option id properly' , async ( ( ) => {
720+ let firstOptionID = options [ 0 ] . id ;
721+
722+ expect ( options [ 0 ] . id )
723+ . toContain ( 'md-select-option' , `Expected option ID to have the correct prefix.` ) ;
724+ expect ( options [ 0 ] . id ) . not . toEqual ( options [ 1 ] . id , `Expected option IDs to be unique.` ) ;
725+
726+ const backdrop =
727+ overlayContainerElement . querySelector ( '.md-overlay-backdrop' ) as HTMLElement ;
728+ backdrop . click ( ) ;
729+ fixture . detectChanges ( ) ;
730+
731+ fixture . whenStable ( ) . then ( ( ) => {
732+ triggers [ 1 ] . nativeElement . click ( ) ;
733+
734+ fixture . detectChanges ( ) ;
735+ options =
736+ overlayContainerElement . querySelectorAll ( 'md-option' ) as NodeListOf < HTMLElement > ;
737+ expect ( options [ 0 ] . id )
738+ . toContain ( 'md-select-option' , `Expected option ID to have the correct prefix.` ) ;
739+ expect ( options [ 0 ] . id ) . not . toEqual ( firstOptionID , `Expected option IDs to be unique.` ) ;
740+ expect ( options [ 0 ] . id ) . not . toEqual ( options [ 1 ] . id , `Expected option IDs to be unique.` ) ;
741+ } ) ;
742+
743+ } ) ) ;
744+
745+ } ) ;
746+
676747 } ) ;
677748
678749} ) ;
@@ -720,6 +791,21 @@ class NgModelSelect {
720791 @ViewChildren ( MdOption ) options : QueryList < MdOption > ;
721792}
722793
794+ @Component ( {
795+ selector : 'many-selects' ,
796+ template : `
797+ <md-select placeholder="First">
798+ <md-option value="one">one</md-option>
799+ <md-option value="two">two</md-option>
800+ </md-select>
801+ <md-select placeholder="Second">
802+ <md-option value="three">three</md-option>
803+ <md-option value="four">four</md-option>
804+ </md-select>
805+ `
806+ } )
807+ class ManySelects { }
808+
723809
724810/**
725811 * TODO: Move this to core testing utility until Angular has event faking
0 commit comments