@@ -5,7 +5,15 @@ import {
5
5
wrappedErrorMessage ,
6
6
MockNgZone ,
7
7
} from '@angular/cdk/testing' ;
8
- import { ChangeDetectionStrategy , Component , ViewChild , Type , Provider , NgZone } from '@angular/core' ;
8
+ import {
9
+ ChangeDetectionStrategy ,
10
+ Component ,
11
+ ViewChild ,
12
+ Type ,
13
+ Provider ,
14
+ NgZone ,
15
+ Directive ,
16
+ } from '@angular/core' ;
9
17
import { ComponentFixture , fakeAsync , flush , TestBed } from '@angular/core/testing' ;
10
18
import {
11
19
FormControl ,
@@ -35,8 +43,7 @@ import {By} from '@angular/platform-browser';
35
43
import { BrowserAnimationsModule } from '@angular/platform-browser/animations' ;
36
44
import { MatStepperModule } from '@angular/material/stepper' ;
37
45
import { MatTabsModule } from '@angular/material/tabs' ;
38
- import { MatInputModule } from './index' ;
39
- import { MatInput } from './input' ;
46
+ import { MatInputModule , MatInput , MAT_INPUT_VALUE_ACCESSOR } from './index' ;
40
47
import { MatTextareaAutosize } from './autosize' ;
41
48
42
49
describe ( 'MatInput without forms' , ( ) => {
@@ -889,6 +896,20 @@ describe('MatInput without forms', () => {
889
896
expect ( formField . classList ) . not . toContain ( 'mat-form-field-type-mat-native-select' ) ;
890
897
} ) ;
891
898
899
+ it ( 'should use the native input value when determining whether ' +
900
+ 'the element is empty with a custom accessor' , fakeAsync ( ( ) => {
901
+ let fixture = createComponent ( MatInputWithCustomAccessor , [ ] , [ ] , [ CustomMatInputAccessor ] ) ;
902
+ fixture . detectChanges ( ) ;
903
+ let label = fixture . debugElement . query ( By . css ( 'label' ) ) . nativeElement ;
904
+
905
+ expect ( label . classList ) . toContain ( 'mat-form-field-empty' ) ;
906
+
907
+ fixture . nativeElement . querySelector ( 'input' ) . value = 'abc' ;
908
+ fixture . detectChanges ( ) ;
909
+
910
+ expect ( label . classList ) . not . toContain ( 'mat-form-field-empty' ) ;
911
+ } ) ) ;
912
+
892
913
} ) ;
893
914
894
915
describe ( 'MatInput with forms' , ( ) => {
@@ -1368,7 +1389,8 @@ describe('MatInput with textarea autosize', () => {
1368
1389
1369
1390
function createComponent < T > ( component : Type < T > ,
1370
1391
providers : Provider [ ] = [ ] ,
1371
- imports : any [ ] = [ ] ) : ComponentFixture < T > {
1392
+ imports : any [ ] = [ ] ,
1393
+ declarations : any [ ] = [ ] ) : ComponentFixture < T > {
1372
1394
TestBed . configureTestingModule ( {
1373
1395
imports : [
1374
1396
FormsModule ,
@@ -1379,7 +1401,7 @@ function createComponent<T>(component: Type<T>,
1379
1401
ReactiveFormsModule ,
1380
1402
...imports
1381
1403
] ,
1382
- declarations : [ component ] ,
1404
+ declarations : [ component , ... declarations ] ,
1383
1405
providers,
1384
1406
} ) . compileComponents ( ) ;
1385
1407
@@ -1876,3 +1898,26 @@ class MatInputSelectWithLabel {}
1876
1898
</mat-form-field>`
1877
1899
} )
1878
1900
class MatInputSelectWithInnerHtml { }
1901
+
1902
+ @Component ( {
1903
+ template : `
1904
+ <mat-form-field floatLabel="never">
1905
+ <input matInput customInputAccessor placeholder="Placeholder">
1906
+ </mat-form-field>`
1907
+ } )
1908
+ class MatInputWithCustomAccessor { }
1909
+
1910
+
1911
+ /** Custom component that never has a value. Used for testing the `MAT_INPUT_VALUE_ACCESSOR`. */
1912
+ @Directive ( {
1913
+ selector : 'input[customInputAccessor]' ,
1914
+ providers : [ {
1915
+ provide : MAT_INPUT_VALUE_ACCESSOR ,
1916
+ useExisting : CustomMatInputAccessor
1917
+ } ]
1918
+ } )
1919
+ class CustomMatInputAccessor {
1920
+ get value ( ) { return this . _value ; }
1921
+ set value ( _value : any ) { }
1922
+ private _value = null ;
1923
+ }
0 commit comments