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' ;
@@ -55,7 +63,8 @@ describe('MdSelect', () => {
55
63
SelectEarlyAccessSibling ,
56
64
BasicSelectInitiallyHidden ,
57
65
BasicSelectNoPlaceholder ,
58
- BasicSelectWithTheming
66
+ BasicSelectWithTheming ,
67
+ SelectInsideFormGroup
59
68
] ,
60
69
providers : [
61
70
{ provide : OverlayContainer , useFactory : ( ) => {
@@ -1341,11 +1350,12 @@ describe('MdSelect', () => {
1341
1350
. toEqual ( 'true' , `Expected aria-required attr to be true for required selects.` ) ;
1342
1351
} ) ;
1343
1352
1344
- it ( 'should set aria-invalid for selects that are invalid' , ( ) => {
1353
+ it ( 'should set aria-invalid for selects that are invalid and touched ' , ( ) => {
1345
1354
expect ( select . getAttribute ( 'aria-invalid' ) )
1346
1355
. toEqual ( 'false' , `Expected aria-invalid attr to be false for valid selects.` ) ;
1347
1356
1348
1357
fixture . componentInstance . isRequired = true ;
1358
+ fixture . componentInstance . control . markAsTouched ( ) ;
1349
1359
fixture . detectChanges ( ) ;
1350
1360
1351
1361
expect ( select . getAttribute ( 'aria-invalid' ) )
@@ -2022,6 +2032,77 @@ describe('MdSelect', () => {
2022
2032
2023
2033
} ) ;
2024
2034
2035
+ describe ( 'error state' , ( ) => {
2036
+ let fixture : ComponentFixture < SelectInsideFormGroup > ;
2037
+ let testComponent : SelectInsideFormGroup ;
2038
+ let select : HTMLElement ;
2039
+
2040
+ beforeEach ( ( ) => {
2041
+ fixture = TestBed . createComponent ( SelectInsideFormGroup ) ;
2042
+ fixture . detectChanges ( ) ;
2043
+ testComponent = fixture . componentInstance ;
2044
+ select = fixture . debugElement . query ( By . css ( 'md-select' ) ) . nativeElement ;
2045
+ } ) ;
2046
+
2047
+ it ( 'should not set the invalid class on a clean select' , ( ) => {
2048
+ expect ( testComponent . formGroup . untouched ) . toBe ( true , 'Expected the form to be untouched.' ) ;
2049
+ expect ( testComponent . formControl . invalid ) . toBe ( true , 'Expected form control to be invalid.' ) ;
2050
+ expect ( select . classList )
2051
+ . not . toContain ( 'mat-select-invalid' , 'Expected select not to appear invalid.' ) ;
2052
+ expect ( select . getAttribute ( 'aria-invalid' ) )
2053
+ . toBe ( 'false' , 'Expected aria-invalid to be set to false.' ) ;
2054
+ } ) ;
2055
+
2056
+ it ( 'should appear as invalid if it becomes touched' , ( ) => {
2057
+ expect ( select . classList )
2058
+ . not . toContain ( 'mat-select-invalid' , 'Expected select not to appear invalid.' ) ;
2059
+ expect ( select . getAttribute ( 'aria-invalid' ) )
2060
+ . toBe ( 'false' , 'Expected aria-invalid to be set to false.' ) ;
2061
+
2062
+ testComponent . formControl . markAsTouched ( ) ;
2063
+ fixture . detectChanges ( ) ;
2064
+
2065
+ expect ( select . classList )
2066
+ . toContain ( 'mat-select-invalid' , 'Expected select to appear invalid.' ) ;
2067
+ expect ( select . getAttribute ( 'aria-invalid' ) )
2068
+ . toBe ( 'true' , 'Expected aria-invalid to be set to true.' ) ;
2069
+ } ) ;
2070
+
2071
+ it ( 'should not have the invalid class when the select becomes valid' , ( ) => {
2072
+ testComponent . formControl . markAsTouched ( ) ;
2073
+ fixture . detectChanges ( ) ;
2074
+
2075
+ expect ( select . classList )
2076
+ . toContain ( 'mat-select-invalid' , 'Expected select to appear invalid.' ) ;
2077
+ expect ( select . getAttribute ( 'aria-invalid' ) )
2078
+ . toBe ( 'true' , 'Expected aria-invalid to be set to true.' ) ;
2079
+
2080
+ testComponent . formControl . setValue ( 'pizza-1' ) ;
2081
+ fixture . detectChanges ( ) ;
2082
+
2083
+ expect ( select . classList )
2084
+ . not . toContain ( 'mat-select-invalid' , 'Expected select not to appear invalid.' ) ;
2085
+ expect ( select . getAttribute ( 'aria-invalid' ) )
2086
+ . toBe ( 'false' , 'Expected aria-invalid to be set to false.' ) ;
2087
+ } ) ;
2088
+
2089
+ it ( 'should appear as invalid when the parent form group is submitted' , ( ) => {
2090
+ expect ( select . classList )
2091
+ . not . toContain ( 'mat-select-invalid' , 'Expected select not to appear invalid.' ) ;
2092
+ expect ( select . getAttribute ( 'aria-invalid' ) )
2093
+ . toBe ( 'false' , 'Expected aria-invalid to be set to false.' ) ;
2094
+
2095
+ dispatchFakeEvent ( fixture . debugElement . query ( By . css ( 'form' ) ) . nativeElement , 'submit' ) ;
2096
+ fixture . detectChanges ( ) ;
2097
+
2098
+ expect ( select . classList )
2099
+ . toContain ( 'mat-select-invalid' , 'Expected select to appear invalid.' ) ;
2100
+ expect ( select . getAttribute ( 'aria-invalid' ) )
2101
+ . toBe ( 'true' , 'Expected aria-invalid to be set to true.' ) ;
2102
+ } ) ;
2103
+
2104
+ } ) ;
2105
+
2025
2106
} ) ;
2026
2107
2027
2108
@@ -2366,3 +2447,21 @@ class BasicSelectWithTheming {
2366
2447
@ViewChild ( MdSelect ) select : MdSelect ;
2367
2448
theme : string ;
2368
2449
}
2450
+
2451
+ @Component ( {
2452
+ template : `
2453
+ <form [formGroup]="formGroup">
2454
+ <md-select placeholder="Food" formControlName="food">
2455
+ <md-option value="steak-0">Steak</md-option>
2456
+ <md-option value="pizza-1">Pizza</md-option>
2457
+ </md-select>
2458
+ </form>
2459
+ `
2460
+ } )
2461
+ class SelectInsideFormGroup {
2462
+ @ViewChild ( FormGroupDirective ) formGroupDirective : FormGroupDirective ;
2463
+ formControl = new FormControl ( '' , Validators . required ) ;
2464
+ formGroup = new FormGroup ( {
2465
+ food : this . formControl
2466
+ } ) ;
2467
+ }
0 commit comments