1
- import { TestBed , async , ComponentFixture , fakeAsync , tick , inject } from '@angular/core/testing' ;
2
- import { By } from '@angular/platform-browser' ;
3
1
import {
4
2
Component ,
5
3
DebugElement ,
@@ -9,17 +7,27 @@ import {
9
7
ChangeDetectionStrategy ,
10
8
OnInit ,
11
9
} from '@angular/core' ;
10
+ import {
11
+ ControlValueAccessor ,
12
+ FormControl ,
13
+ FormsModule ,
14
+ NG_VALUE_ACCESSOR ,
15
+ ReactiveFormsModule ,
16
+ FormGroup ,
17
+ FormGroupDirective ,
18
+ NgForm ,
19
+ Validators ,
20
+ } from '@angular/forms' ;
21
+ import { By } from '@angular/platform-browser' ;
12
22
import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
23
+ import { TestBed , async , ComponentFixture , fakeAsync , tick , inject } from '@angular/core/testing' ;
13
24
import { MdSelectModule } from './index' ;
14
25
import { OverlayContainer } from '../core/overlay/overlay-container' ;
15
26
import { MdSelect , MdSelectFloatPlaceholderType } from './select' ;
16
27
import { getMdSelectDynamicMultipleError , getMdSelectNonArrayValueError } from './select-errors' ;
17
28
import { MdOption } from '../core/option/option' ;
18
29
import { Dir } from '../core/rtl/dir' ;
19
30
import { DOWN_ARROW , UP_ARROW , ENTER , SPACE , HOME , END , TAB } from '../core/keyboard/keycodes' ;
20
- import {
21
- ControlValueAccessor , FormControl , FormsModule , NG_VALUE_ACCESSOR , ReactiveFormsModule
22
- } from '@angular/forms' ;
23
31
import { Subject } from 'rxjs/Subject' ;
24
32
import { ViewportRuler } from '../core/overlay/position/viewport-ruler' ;
25
33
import { dispatchFakeEvent , dispatchKeyboardEvent } from '../core/testing/dispatch-events' ;
@@ -56,7 +64,8 @@ describe('MdSelect', () => {
56
64
BasicSelectInitiallyHidden ,
57
65
BasicSelectNoPlaceholder ,
58
66
BasicSelectWithTheming ,
59
- ResetValuesSelect
67
+ ResetValuesSelect ,
68
+ SelectInsideFormGroup
60
69
] ,
61
70
providers : [
62
71
{ provide : OverlayContainer , useFactory : ( ) => {
@@ -1387,11 +1396,12 @@ describe('MdSelect', () => {
1387
1396
. toEqual ( 'true' , `Expected aria-required attr to be true for required selects.` ) ;
1388
1397
} ) ;
1389
1398
1390
- it ( 'should set aria-invalid for selects that are invalid' , ( ) => {
1399
+ it ( 'should set aria-invalid for selects that are invalid and touched ' , ( ) => {
1391
1400
expect ( select . getAttribute ( 'aria-invalid' ) )
1392
1401
. toEqual ( 'false' , `Expected aria-invalid attr to be false for valid selects.` ) ;
1393
1402
1394
1403
fixture . componentInstance . isRequired = true ;
1404
+ fixture . componentInstance . control . markAsTouched ( ) ;
1395
1405
fixture . detectChanges ( ) ;
1396
1406
1397
1407
expect ( select . getAttribute ( 'aria-invalid' ) )
@@ -2144,6 +2154,77 @@ describe('MdSelect', () => {
2144
2154
2145
2155
} ) ;
2146
2156
2157
+ describe ( 'error state' , ( ) => {
2158
+ let fixture : ComponentFixture < SelectInsideFormGroup > ;
2159
+ let testComponent : SelectInsideFormGroup ;
2160
+ let select : HTMLElement ;
2161
+
2162
+ beforeEach ( ( ) => {
2163
+ fixture = TestBed . createComponent ( SelectInsideFormGroup ) ;
2164
+ fixture . detectChanges ( ) ;
2165
+ testComponent = fixture . componentInstance ;
2166
+ select = fixture . debugElement . query ( By . css ( 'md-select' ) ) . nativeElement ;
2167
+ } ) ;
2168
+
2169
+ it ( 'should not set the invalid class on a clean select' , ( ) => {
2170
+ expect ( testComponent . formGroup . untouched ) . toBe ( true , 'Expected the form to be untouched.' ) ;
2171
+ expect ( testComponent . formControl . invalid ) . toBe ( true , 'Expected form control to be invalid.' ) ;
2172
+ expect ( select . classList )
2173
+ . not . toContain ( 'mat-select-invalid' , 'Expected select not to appear invalid.' ) ;
2174
+ expect ( select . getAttribute ( 'aria-invalid' ) )
2175
+ . toBe ( 'false' , 'Expected aria-invalid to be set to false.' ) ;
2176
+ } ) ;
2177
+
2178
+ it ( 'should appear as invalid if it becomes touched' , ( ) => {
2179
+ expect ( select . classList )
2180
+ . not . toContain ( 'mat-select-invalid' , 'Expected select not to appear invalid.' ) ;
2181
+ expect ( select . getAttribute ( 'aria-invalid' ) )
2182
+ . toBe ( 'false' , 'Expected aria-invalid to be set to false.' ) ;
2183
+
2184
+ testComponent . formControl . markAsTouched ( ) ;
2185
+ fixture . detectChanges ( ) ;
2186
+
2187
+ expect ( select . classList )
2188
+ . toContain ( 'mat-select-invalid' , 'Expected select to appear invalid.' ) ;
2189
+ expect ( select . getAttribute ( 'aria-invalid' ) )
2190
+ . toBe ( 'true' , 'Expected aria-invalid to be set to true.' ) ;
2191
+ } ) ;
2192
+
2193
+ it ( 'should not have the invalid class when the select becomes valid' , ( ) => {
2194
+ testComponent . formControl . markAsTouched ( ) ;
2195
+ fixture . detectChanges ( ) ;
2196
+
2197
+ expect ( select . classList )
2198
+ . toContain ( 'mat-select-invalid' , 'Expected select to appear invalid.' ) ;
2199
+ expect ( select . getAttribute ( 'aria-invalid' ) )
2200
+ . toBe ( 'true' , 'Expected aria-invalid to be set to true.' ) ;
2201
+
2202
+ testComponent . formControl . setValue ( 'pizza-1' ) ;
2203
+ fixture . detectChanges ( ) ;
2204
+
2205
+ expect ( select . classList )
2206
+ . not . toContain ( 'mat-select-invalid' , 'Expected select not to appear invalid.' ) ;
2207
+ expect ( select . getAttribute ( 'aria-invalid' ) )
2208
+ . toBe ( 'false' , 'Expected aria-invalid to be set to false.' ) ;
2209
+ } ) ;
2210
+
2211
+ it ( 'should appear as invalid when the parent form group is submitted' , ( ) => {
2212
+ expect ( select . classList )
2213
+ . not . toContain ( 'mat-select-invalid' , 'Expected select not to appear invalid.' ) ;
2214
+ expect ( select . getAttribute ( 'aria-invalid' ) )
2215
+ . toBe ( 'false' , 'Expected aria-invalid to be set to false.' ) ;
2216
+
2217
+ dispatchFakeEvent ( fixture . debugElement . query ( By . css ( 'form' ) ) . nativeElement , 'submit' ) ;
2218
+ fixture . detectChanges ( ) ;
2219
+
2220
+ expect ( select . classList )
2221
+ . toContain ( 'mat-select-invalid' , 'Expected select to appear invalid.' ) ;
2222
+ expect ( select . getAttribute ( 'aria-invalid' ) )
2223
+ . toBe ( 'true' , 'Expected aria-invalid to be set to true.' ) ;
2224
+ } ) ;
2225
+
2226
+ } ) ;
2227
+
2147
2228
} ) ;
2148
2229
2149
2230
@@ -2491,6 +2572,7 @@ class BasicSelectWithTheming {
2491
2572
theme : string ;
2492
2573
}
2493
2574
2575
+
2494
2576
@Component ( {
2495
2577
selector : 'reset-values-select' ,
2496
2578
template : `
@@ -2516,3 +2598,22 @@ class ResetValuesSelect {
2516
2598
2517
2599
@ViewChild ( MdSelect ) select : MdSelect ;
2518
2600
}
2601
+
2602
+
2603
+ @Component ( {
2604
+ template : `
2605
+ <form [formGroup]="formGroup">
2606
+ <md-select placeholder="Food" formControlName="food">
2607
+ <md-option value="steak-0">Steak</md-option>
2608
+ <md-option value="pizza-1">Pizza</md-option>
2609
+ </md-select>
2610
+ </form>
2611
+ `
2612
+ } )
2613
+ class SelectInsideFormGroup {
2614
+ @ViewChild ( FormGroupDirective ) formGroupDirective : FormGroupDirective ;
2615
+ formControl = new FormControl ( '' , Validators . required ) ;
2616
+ formGroup = new FormGroup ( {
2617
+ food : this . formControl
2618
+ } ) ;
2619
+ }
0 commit comments