1
1
import { async , TestBed , inject } from '@angular/core/testing' ;
2
2
import { Component } from '@angular/core' ;
3
- import { FormsModule , ReactiveFormsModule } from '@angular/forms' ;
3
+ import { FormsModule , ReactiveFormsModule , FormControl } from '@angular/forms' ;
4
4
import { By } from '@angular/platform-browser' ;
5
5
import { MdInputModule } from './input' ;
6
- import { MdInputContainer } from './input-container' ;
6
+ import { MdInputContainer , MdInputDirective } from './input-container' ;
7
7
import { Platform } from '../core/platform/platform' ;
8
8
import { PlatformModule } from '../core/platform/index' ;
9
9
import {
@@ -43,6 +43,9 @@ describe('MdInputContainer', function () {
43
43
MdInputContainerWithDisabled ,
44
44
MdInputContainerWithRequired ,
45
45
MdInputContainerWithType ,
46
+ MdInputContainerWithValueBinding ,
47
+ MdInputContainerWithFormControl ,
48
+ MdInputContainerWithStaticPlaceholder ,
46
49
MdInputContainerMissingMdInputTestController
47
50
] ,
48
51
} ) ;
@@ -130,6 +133,40 @@ describe('MdInputContainer', function () {
130
133
expect ( el . classList . contains ( 'md-empty' ) ) . toBe ( false , 'should not be empty' ) ;
131
134
} ) ) ;
132
135
136
+ it ( 'should update the placeholder when input entered' , async ( ( ) => {
137
+ let fixture = TestBed . createComponent ( MdInputContainerWithStaticPlaceholder ) ;
138
+ fixture . detectChanges ( ) ;
139
+
140
+ let inputEl = fixture . debugElement . query ( By . css ( 'input' ) ) ;
141
+ let labelEl = fixture . debugElement . query ( By . css ( 'label' ) ) . nativeElement ;
142
+
143
+ expect ( labelEl . classList ) . toContain ( 'md-empty' ) ;
144
+ expect ( labelEl . classList ) . not . toContain ( 'md-float' ) ;
145
+
146
+ // Update the value of the input.
147
+ inputEl . nativeElement . value = 'Text' ;
148
+
149
+ // Fake behavior of the `(input)` event which should trigger a change detection.
150
+ fixture . detectChanges ( ) ;
151
+
152
+ expect ( labelEl . classList ) . not . toContain ( 'md-empty' ) ;
153
+ expect ( labelEl . classList ) . not . toContain ( 'md-float' ) ;
154
+ } ) ) ;
155
+
156
+ it ( 'should not be empty when the value set before view init' , async ( ( ) => {
157
+ let fixture = TestBed . createComponent ( MdInputContainerWithValueBinding ) ;
158
+ fixture . detectChanges ( ) ;
159
+
160
+ let placeholderEl = fixture . debugElement . query ( By . css ( '.md-input-placeholder' ) ) . nativeElement ;
161
+
162
+ expect ( placeholderEl . classList ) . not . toContain ( 'md-empty' ) ;
163
+
164
+ fixture . componentInstance . value = '' ;
165
+ fixture . detectChanges ( ) ;
166
+
167
+ expect ( placeholderEl . classList ) . toContain ( 'md-empty' ) ;
168
+ } ) ) ;
169
+
133
170
it ( 'should not treat the number 0 as empty' , async ( ( ) => {
134
171
let fixture = TestBed . createComponent ( MdInputContainerZeroTestController ) ;
135
172
fixture . detectChanges ( ) ;
@@ -143,6 +180,20 @@ describe('MdInputContainer', function () {
143
180
} ) ;
144
181
} ) ) ;
145
182
183
+ it ( 'should update the value when using FormControl.setValue' , ( ) => {
184
+ let fixture = TestBed . createComponent ( MdInputContainerWithFormControl ) ;
185
+ fixture . detectChanges ( ) ;
186
+
187
+ let input = fixture . debugElement . query ( By . directive ( MdInputDirective ) )
188
+ . injector . get ( MdInputDirective ) as MdInputDirective ;
189
+
190
+ expect ( input . value ) . toBeFalsy ( ) ;
191
+
192
+ fixture . componentInstance . formControl . setValue ( 'something' ) ;
193
+
194
+ expect ( input . value ) . toBe ( 'something' ) ;
195
+ } ) ;
196
+
146
197
it ( 'should add id' , ( ) => {
147
198
let fixture = TestBed . createComponent ( MdInputContainerTextTestController ) ;
148
199
fixture . detectChanges ( ) ;
@@ -379,6 +430,13 @@ class MdInputContainerPlaceholderElementTestComponent {
379
430
placeholder : string = 'Default Placeholder' ;
380
431
}
381
432
433
+ @Component ( {
434
+ template : `<md-input-container><input md-input [formControl]="formControl"></md-input-container>`
435
+ } )
436
+ class MdInputContainerWithFormControl {
437
+ formControl = new FormControl ( ) ;
438
+ }
439
+
382
440
@Component ( {
383
441
template : `<md-input-container><input mdInput [placeholder]="placeholder"></md-input-container>`
384
442
} )
@@ -482,6 +540,25 @@ class MdInputContainerZeroTestController {
482
540
value = 0 ;
483
541
}
484
542
543
+ @Component ( {
544
+ template : `
545
+ <md-input-container>
546
+ <input mdInput placeholder="Label" [value]="value">
547
+ </md-input-container>`
548
+ } )
549
+ class MdInputContainerWithValueBinding {
550
+ value : string = 'Initial' ;
551
+ }
552
+
553
+ @Component ( {
554
+ template : `
555
+ <md-input-container [floatingPlaceholder]="false">
556
+ <input md-input placeholder="Label">
557
+ </md-input-container>
558
+ `
559
+ } )
560
+ class MdInputContainerWithStaticPlaceholder { }
561
+
485
562
@Component ( {
486
563
template : `
487
564
<md-input-container>
0 commit comments