@@ -28,7 +28,8 @@ describe('MatTextareaAutosize', () => {
28
28
AutosizeTextareaInATab ,
29
29
AutosizeTextAreaWithContent ,
30
30
AutosizeTextAreaWithValue ,
31
- AutosizeTextareaWithNgModel
31
+ AutosizeTextareaWithNgModel ,
32
+ AutosizeTextareaWithLongPlaceholder
32
33
] ,
33
34
} ) ;
34
35
@@ -176,6 +177,39 @@ describe('MatTextareaAutosize', () => {
176
177
. toBe ( textarea . scrollHeight , 'Expected textarea height to match its scrollHeight' ) ;
177
178
} ) ;
178
179
180
+ it ( 'should not calculate wrong content height due to long placeholders' , ( ) => {
181
+ const fixtureWithPlaceholder = TestBed . createComponent ( AutosizeTextareaWithLongPlaceholder ) ;
182
+ fixtureWithPlaceholder . detectChanges ( ) ;
183
+
184
+ textarea = fixtureWithPlaceholder . nativeElement . querySelector ( 'textarea' ) ;
185
+ autosize = fixtureWithPlaceholder . debugElement . query (
186
+ By . directive ( MatTextareaAutosize ) ) . injector . get < MatTextareaAutosize > ( MatTextareaAutosize ) ;
187
+
188
+ // To be able to trigger a new calculation of the height with a long placeholder, the textarea
189
+ // value needs to be changed.
190
+ textarea . value = '1' ;
191
+ autosize . resizeToFitContent ( ) ;
192
+
193
+ textarea . value = '' ;
194
+ autosize . resizeToFitContent ( ) ;
195
+
196
+ const heightWithLongPlaceholder = textarea . clientHeight ;
197
+
198
+ fixtureWithPlaceholder . componentInstance . placeholder = 'Short' ;
199
+ fixtureWithPlaceholder . detectChanges ( ) ;
200
+
201
+ // To be able to trigger a new calculation of the height with a short placeholder, the
202
+ // textarea value needs to be changed.
203
+ textarea . value = '1' ;
204
+ autosize . resizeToFitContent ( ) ;
205
+
206
+ textarea . value = '' ;
207
+ autosize . resizeToFitContent ( ) ;
208
+
209
+ expect ( textarea . clientHeight ) . toBe ( heightWithLongPlaceholder ,
210
+ 'Expected the textarea height to be the same with a long placeholder.' ) ;
211
+ } ) ;
212
+
179
213
it ( 'should resize when an associated form control value changes' , fakeAsync ( ( ) => {
180
214
const fixtureWithForms = TestBed . createComponent ( AutosizeTextareaWithNgModel ) ;
181
215
textarea = fixtureWithForms . nativeElement . querySelector ( 'textarea' ) ;
@@ -269,6 +303,17 @@ class AutosizeTextareaWithNgModel {
269
303
model = '' ;
270
304
}
271
305
306
+ @Component ( {
307
+ template : `
308
+ <mat-form-field style="width: 100px">
309
+ <textarea matInput matTextareaAutosize [placeholder]="placeholder"></textarea>
310
+ </mat-form-field>` ,
311
+ styles : [ textareaStyleReset ] ,
312
+ } )
313
+ class AutosizeTextareaWithLongPlaceholder {
314
+ placeholder = 'Long Long Long Long Long Long Long Long Placeholder' ;
315
+ }
316
+
272
317
@Component ( {
273
318
template : `
274
319
<mat-tab-group>
0 commit comments