11import { async , TestBed , inject } from '@angular/core/testing' ;
22import { Component } from '@angular/core' ;
3- import { FormsModule , ReactiveFormsModule } from '@angular/forms' ;
3+ import { FormsModule , ReactiveFormsModule , FormControl } from '@angular/forms' ;
44import { By } from '@angular/platform-browser' ;
55import { MdInputModule } from './input' ;
6- import { MdInputContainer } from './input-container' ;
6+ import { MdInputContainer , MdInputDirective } from './input-container' ;
77import { Platform } from '../core/platform/platform' ;
88import { PlatformModule } from '../core/platform/index' ;
99import {
@@ -43,6 +43,9 @@ describe('MdInputContainer', function () {
4343 MdInputContainerWithDisabled ,
4444 MdInputContainerWithRequired ,
4545 MdInputContainerWithType ,
46+ MdInputContainerWithValueBinding ,
47+ MdInputContainerWithFormControl ,
48+ MdInputContainerWithStaticPlaceholder ,
4649 MdInputContainerMissingMdInputTestController
4750 ] ,
4851 } ) ;
@@ -130,6 +133,40 @@ describe('MdInputContainer', function () {
130133 expect ( el . classList . contains ( 'md-empty' ) ) . toBe ( false , 'should not be empty' ) ;
131134 } ) ) ;
132135
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+
133170 it ( 'should not treat the number 0 as empty' , async ( ( ) => {
134171 let fixture = TestBed . createComponent ( MdInputContainerZeroTestController ) ;
135172 fixture . detectChanges ( ) ;
@@ -143,6 +180,20 @@ describe('MdInputContainer', function () {
143180 } ) ;
144181 } ) ) ;
145182
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+
146197 it ( 'should add id' , ( ) => {
147198 let fixture = TestBed . createComponent ( MdInputContainerTextTestController ) ;
148199 fixture . detectChanges ( ) ;
@@ -379,6 +430,13 @@ class MdInputContainerPlaceholderElementTestComponent {
379430 placeholder : string = 'Default Placeholder' ;
380431}
381432
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+
382440@Component ( {
383441 template : `<md-input-container><input mdInput [placeholder]="placeholder"></md-input-container>`
384442} )
@@ -482,6 +540,25 @@ class MdInputContainerZeroTestController {
482540 value = 0 ;
483541}
484542
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+
485562@Component ( {
486563 template : `
487564 <md-input-container>
0 commit comments