@@ -6,7 +6,9 @@ import {OverlayContainer} from '../core/overlay/overlay-container';
6
6
import { MdSelect } from './select' ;
7
7
import { MdOption } from '../core/option/option' ;
8
8
import { Dir } from '../core/rtl/dir' ;
9
- import { FormControl , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
9
+ import {
10
+ ControlValueAccessor , FormControl , FormsModule , NG_VALUE_ACCESSOR , ReactiveFormsModule
11
+ } from '@angular/forms' ;
10
12
import { ViewportRuler } from '../core/overlay/position/viewport-ruler' ;
11
13
12
14
describe ( 'MdSelect' , ( ) => {
@@ -22,7 +24,9 @@ describe('MdSelect', () => {
22
24
ManySelects ,
23
25
NgIfSelect ,
24
26
SelectInitWithoutOptions ,
25
- SelectWithChangeEvent
27
+ SelectWithChangeEvent ,
28
+ CustomSelectAccessor ,
29
+ CompWithCustomSelect
26
30
] ,
27
31
providers : [
28
32
{ provide : OverlayContainer , useFactory : ( ) => {
@@ -539,6 +543,20 @@ describe('MdSelect', () => {
539
543
540
544
} ) ;
541
545
546
+ describe ( 'misc forms' , ( ) => {
547
+ it ( 'should support use inside a custom value accessor' , ( ) => {
548
+ const fixture = TestBed . createComponent ( CompWithCustomSelect ) ;
549
+ spyOn ( fixture . componentInstance . customAccessor , 'writeValue' ) ;
550
+ fixture . detectChanges ( ) ;
551
+
552
+ expect ( fixture . componentInstance . customAccessor . select . _control )
553
+ . toBe ( null , 'Expected md-select NOT to inherit control from parent value accessor.' ) ;
554
+ expect ( fixture . componentInstance . customAccessor . writeValue ) . toHaveBeenCalled ( ) ;
555
+ } ) ;
556
+
557
+ } ) ;
558
+
559
+
542
560
describe ( 'animations' , ( ) => {
543
561
let fixture : ComponentFixture < BasicSelect > ;
544
562
let trigger : HTMLElement ;
@@ -1379,6 +1397,43 @@ class SelectInitWithoutOptions {
1379
1397
}
1380
1398
}
1381
1399
1400
+ @Component ( {
1401
+ selector : 'custom-select-accessor' ,
1402
+ template : `
1403
+ <md-select></md-select>
1404
+ ` ,
1405
+ providers : [ {
1406
+ provide : NG_VALUE_ACCESSOR ,
1407
+ useExisting : CustomSelectAccessor ,
1408
+ multi : true
1409
+ } ]
1410
+ } )
1411
+ class CustomSelectAccessor implements ControlValueAccessor {
1412
+ @ViewChild ( MdSelect ) select : MdSelect ;
1413
+
1414
+ writeValue ( val : any ) : void { }
1415
+ registerOnChange ( fn : ( val : any ) => void ) : void { }
1416
+ registerOnTouched ( fn : Function ) : void { }
1417
+ }
1418
+
1419
+ @Component ( {
1420
+ selector : 'comp-with-custom-select' ,
1421
+ template : `
1422
+ <custom-select-accessor [formControl]="ctrl">
1423
+ </custom-select-accessor>
1424
+ ` ,
1425
+ providers : [ {
1426
+ provide : NG_VALUE_ACCESSOR ,
1427
+ useExisting : CustomSelectAccessor ,
1428
+ multi : true
1429
+ } ]
1430
+ } )
1431
+ class CompWithCustomSelect {
1432
+ ctrl = new FormControl ( 'initial value' ) ;
1433
+ @ViewChild ( CustomSelectAccessor ) customAccessor : CustomSelectAccessor ;
1434
+ }
1435
+
1436
+
1382
1437
/**
1383
1438
* TODO: Move this to core testing utility until Angular has event faking
1384
1439
* support.
0 commit comments