-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathinput.spec.ts
2429 lines (2000 loc) · 79.9 KB
/
input.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {getSupportedInputTypes} from '@angular/cdk/platform';
import {dispatchFakeEvent, wrappedErrorMessage} from '@angular/cdk/testing/private';
import {
ChangeDetectionStrategy,
Component,
Directive,
ErrorHandler,
Provider,
Type,
ViewChild,
} from '@angular/core';
import {ComponentFixture, TestBed, fakeAsync, flush, tick} from '@angular/core/testing';
import {
FormControl,
FormGroup,
FormGroupDirective,
FormsModule,
NgForm,
ReactiveFormsModule,
Validators,
} from '@angular/forms';
import {ErrorStateMatcher, ShowOnDirtyErrorStateMatcher, ThemePalette} from '../core';
import {
FloatLabelType,
MAT_FORM_FIELD_DEFAULT_OPTIONS,
MatFormField,
MatFormFieldAppearance,
MatFormFieldModule,
SubscriptSizing,
getMatFormFieldDuplicatedHintError,
getMatFormFieldMissingControlError,
} from '../form-field';
import {MatIconModule} from '../icon';
import {By} from '@angular/platform-browser';
import {MAT_INPUT_VALUE_ACCESSOR, MatInput, MatInputModule} from './index';
import {MatFormFieldNotchedOutline} from '../form-field/directives/notched-outline';
describe('MatMdcInput without forms', () => {
it('should default to floating labels', fakeAsync(() => {
let fixture = createComponent(MatInputWithLabel);
fixture.detectChanges();
let formField = fixture.debugElement.query(By.directive(MatFormField))!
.componentInstance as MatFormField;
expect(formField.floatLabel)
.withContext('Expected MatInput to set floatingLabel to auto by default.')
.toBe('auto');
}));
it('should default to global floating label type', fakeAsync(() => {
let fixture = createComponent(MatInputWithLabel, [
{
provide: MAT_FORM_FIELD_DEFAULT_OPTIONS,
useValue: {floatLabel: 'always'},
},
]);
fixture.detectChanges();
let formField = fixture.debugElement.query(By.directive(MatFormField))!
.componentInstance as MatFormField;
expect(formField.floatLabel)
.withContext('Expected MatInput to set floatingLabel to always from global option.')
.toBe('always');
}));
it('should not be treated as empty if type is date', fakeAsync(() => {
const fixture = createComponent(MatInputDateTestController);
fixture.detectChanges();
if (getSupportedInputTypes().has('date')) {
const formField = fixture.debugElement.query(By.directive(MatFormField))!
.componentInstance as MatFormField;
expect(formField).toBeTruthy();
expect(formField!._control.empty).toBe(false);
}
}));
it('should be treated as empty if type is date on unsupported browser', fakeAsync(() => {
const fixture = createComponent(MatInputDateTestController);
fixture.detectChanges();
if (!getSupportedInputTypes().has('date')) {
const formField = fixture.debugElement.query(By.directive(MatFormField))!
.componentInstance as MatFormField;
expect(formField).toBeTruthy();
expect(formField!._control.empty).toBe(true);
}
}));
it('should treat text input type as empty at init', fakeAsync(() => {
let fixture = createComponent(MatInputTextTestController);
fixture.detectChanges();
const formField = fixture.debugElement.query(By.directive(MatFormField))!
.componentInstance as MatFormField;
expect(formField).toBeTruthy();
expect(formField!._control.empty).toBe(true);
}));
it('should treat password input type as empty at init', fakeAsync(() => {
let fixture = createComponent(MatInputPasswordTestController);
fixture.detectChanges();
const formField = fixture.debugElement.query(By.directive(MatFormField))!
.componentInstance as MatFormField;
expect(formField).toBeTruthy();
expect(formField!._control.empty).toBe(true);
}));
it('should treat number input type as empty at init', fakeAsync(() => {
let fixture = createComponent(MatInputNumberTestController);
fixture.detectChanges();
const formField = fixture.debugElement.query(By.directive(MatFormField))!
.componentInstance as MatFormField;
expect(formField).toBeTruthy();
expect(formField!._control.empty).toBe(true);
}));
it('should not be empty after input entered', fakeAsync(() => {
let fixture = createComponent(MatInputTextTestController);
fixture.detectChanges();
let inputEl = fixture.debugElement.query(By.css('input'))!;
const formField = fixture.debugElement.query(By.directive(MatFormField))!
.componentInstance as MatFormField;
expect(formField).toBeTruthy();
expect(formField!._control.empty).toBe(true);
inputEl.nativeElement.value = 'hello';
// Simulate input event.
inputEl.triggerEventHandler('input', {target: inputEl.nativeElement});
fixture.detectChanges();
expect(formField!._control.empty).toBe(false);
}));
it('should update the placeholder when input entered', fakeAsync(() => {
let fixture = createComponent(MatInputWithStaticLabel);
fixture.detectChanges();
const inputEl = fixture.debugElement.query(By.css('input'))!;
const formField = fixture.debugElement.query(By.directive(MatFormField))!
.componentInstance as MatFormField;
expect(formField).toBeTruthy();
expect(formField._control.empty).toBe(true);
expect(formField._shouldLabelFloat()).toBe(false);
// Update the value of the input.
inputEl.nativeElement.value = 'Text';
// Fake behavior of the `(input)` event which should trigger a change detection.
fixture.detectChanges();
expect(formField._control.empty).toBe(false);
// should not float label if there is no label
expect(formField._shouldLabelFloat()).toBe(false);
}));
it('should not be empty when the value set before view init', fakeAsync(() => {
let fixture = createComponent(MatInputWithValueBinding);
fixture.detectChanges();
const formField = fixture.debugElement.query(By.directive(MatFormField))!
.componentInstance as MatFormField;
expect(formField._control.empty).toBe(false);
fixture.componentInstance.value = '';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(formField._control.empty).toBe(true);
}));
it('should add id', fakeAsync(() => {
let fixture = createComponent(MatInputTextTestController);
fixture.detectChanges();
const inputElement: HTMLInputElement = fixture.debugElement.query(
By.css('input'),
)!.nativeElement;
const labelElement: HTMLInputElement = fixture.debugElement.query(
By.css('label'),
)!.nativeElement;
expect(inputElement.id).toBeTruthy();
expect(inputElement.id).toEqual(labelElement.getAttribute('for')!);
}));
it('should add aria-required reflecting the required state', fakeAsync(() => {
const fixture = createComponent(MatInputWithRequired);
fixture.detectChanges();
const inputElement: HTMLInputElement = fixture.debugElement.query(
By.css('input'),
)!.nativeElement;
expect(inputElement.getAttribute('aria-required'))
.withContext('Expected aria-required to reflect required state of false')
.toBe('false');
fixture.componentInstance.required = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(inputElement.getAttribute('aria-required'))
.withContext('Expected aria-required to reflect required state of true')
.toBe('true');
}));
it('should not overwrite existing id', fakeAsync(() => {
let fixture = createComponent(MatInputWithId);
fixture.detectChanges();
const inputElement: HTMLInputElement = fixture.debugElement.query(
By.css('input'),
)!.nativeElement;
const labelElement: HTMLInputElement = fixture.debugElement.query(
By.css('label'),
)!.nativeElement;
expect(inputElement.id).toBe('test-id');
expect(labelElement.getAttribute('for')).toBe('test-id');
}));
it("validates there's only one hint label per side", () => {
let fixture = createComponent(MatInputInvalidHintTestController);
expect(() => fixture.detectChanges()).toThrowError(
wrappedErrorMessage(getMatFormFieldDuplicatedHintError('start')),
);
});
it("validates there's only one hint label per side (attribute)", () => {
let fixture = createComponent(MatInputInvalidHint2TestController);
expect(() => fixture.detectChanges()).toThrowError(
wrappedErrorMessage(getMatFormFieldDuplicatedHintError('start')),
);
});
it('validates that matInput child is present', fakeAsync(() => {
let fixture = createComponent(MatInputMissingMatInputTestController);
expect(() => fixture.detectChanges()).toThrowError(
wrappedErrorMessage(getMatFormFieldMissingControlError()),
);
}));
it('validates that matInput child is present after initialization', fakeAsync(() => {
let fixture = createComponent(MatInputWithNgIf);
expect(() => fixture.detectChanges()).not.toThrowError(
wrappedErrorMessage(getMatFormFieldMissingControlError()),
);
fixture.componentInstance.renderInput = false;
fixture.changeDetectorRef.markForCheck();
expect(() => fixture.detectChanges()).toThrowError(
wrappedErrorMessage(getMatFormFieldMissingControlError()),
);
}));
it('validates the type', fakeAsync(() => {
let fixture = createComponent(MatInputInvalidTypeTestController);
// Technically this throws during the OnChanges detection phase,
// so the error is really a ChangeDetectionError and it becomes
// hard to build a full exception to compare with.
// We just check for any exception in this case.
expect(() => fixture.detectChanges())
.toThrow
/* new MatInputUnsupportedTypeError('file') */
();
}));
it('supports hint labels attribute', fakeAsync(() => {
let fixture = createComponent(MatInputHintLabelTestController);
fixture.detectChanges();
// If the hint label is empty, expect no label.
expect(fixture.debugElement.query(By.css('.mat-mdc-form-field-hint'))).toBeNull();
fixture.componentInstance.label = 'label';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(fixture.debugElement.query(By.css('.mat-mdc-form-field-hint'))).not.toBeNull();
}));
it('sets an id on hint labels', fakeAsync(() => {
let fixture = createComponent(MatInputHintLabelTestController);
fixture.componentInstance.label = 'label';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
let hint = fixture.debugElement.query(By.css('.mat-mdc-form-field-hint'))!.nativeElement;
expect(hint.getAttribute('id')).toBeTruthy();
}));
it('supports hint labels elements', fakeAsync(() => {
let fixture = createComponent(MatInputHintLabel2TestController);
fixture.detectChanges();
// In this case, we should have an empty <mat-hint>.
let el = fixture.debugElement.query(By.css('mat-hint'))!.nativeElement;
expect(el.textContent).toBeFalsy();
fixture.componentInstance.label = 'label';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
el = fixture.debugElement.query(By.css('mat-hint'))!.nativeElement;
expect(el.textContent).toBe('label');
}));
it('sets an id on the hint element', fakeAsync(() => {
let fixture = createComponent(MatInputHintLabel2TestController);
fixture.componentInstance.label = 'label';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
let hint = fixture.debugElement.query(By.css('mat-hint'))!.nativeElement;
expect(hint.getAttribute('id')).toBeTruthy();
}));
it('supports label required star', fakeAsync(() => {
const fixture = createComponent(MatInputLabelRequiredTestComponent);
fixture.detectChanges();
const label = fixture.debugElement.query(By.css('label'))!;
expect(label).not.toBeNull();
expect(label.nativeElement.textContent).toBe('hello');
expect(label.nativeElement.querySelector('.mat-mdc-form-field-required-marker')).toBeTruthy();
}));
it('should show the required star when using a FormControl', fakeAsync(() => {
const fixture = createComponent(MatInputWithRequiredFormControl);
fixture.detectChanges();
const label = fixture.debugElement.query(By.css('label'))!;
expect(label).not.toBeNull();
expect(label.nativeElement.textContent).toBe('Hello');
expect(label.nativeElement.querySelector('.mat-mdc-form-field-required-marker')).toBeTruthy();
}));
it('should show the required star when FormControl is reassigned', fakeAsync(() => {
const fixture = createComponent(MatInputWithRequiredAssignableFormControl);
fixture.detectChanges();
// should have star by default
let label = fixture.debugElement.query(By.css('label'))!;
expect(label.nativeElement.querySelector('.mat-mdc-form-field-required-marker')).toBeTruthy();
fixture.componentInstance.reassignFormControl();
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
// should be removed as form was reassigned with no required validators
label = fixture.debugElement.query(By.css('label'))!;
expect(label.nativeElement.querySelector('.mat-mdc-form-field-required-marker')).toBeFalsy();
}));
it('should show the required star when required validator is toggled', fakeAsync(() => {
const fixture = createComponent(MatInputWithRequiredAssignableFormControl);
fixture.detectChanges();
// should have star by default
let label = fixture.debugElement.query(By.css('label'))!;
expect(label.nativeElement.querySelector('.mat-mdc-form-field-required-marker')).toBeTruthy();
fixture.componentInstance.removeRequiredValidtor();
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
// should be removed as control validator was removed
label = fixture.debugElement.query(By.css('label'))!;
expect(label.nativeElement.querySelector('.mat-mdc-form-field-required-marker')).toBeFalsy();
fixture.componentInstance.addRequiredValidator();
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
// should contain star as control validator was readded
label = fixture.debugElement.query(By.css('label'))!;
expect(label.nativeElement.querySelector('.mat-mdc-form-field-required-marker')).toBeTruthy();
}));
it('should not hide the required star if input is disabled', () => {
const fixture = createComponent(MatInputLabelRequiredTestComponent);
fixture.componentInstance.disabled = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
const label = fixture.debugElement.query(By.css('label'))!;
expect(label).not.toBeNull();
expect(label.nativeElement.textContent).toBe('hello');
expect(label.nativeElement.querySelector('.mat-mdc-form-field-required-marker')).toBeTruthy();
});
it('hide label required star when set to hide the required marker', fakeAsync(() => {
const fixture = createComponent(MatInputLabelRequiredTestComponent);
fixture.detectChanges();
const label = fixture.debugElement.query(By.css('label'))!;
expect(label).not.toBeNull();
expect(label.nativeElement.querySelector('.mat-mdc-form-field-required-marker')).toBeTruthy();
expect(label.nativeElement.textContent).toBe('hello');
fixture.componentInstance.hideRequiredMarker = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(label.nativeElement.querySelector('.mat-mdc-form-field-required-marker')).toBeFalsy();
expect(label.nativeElement.textContent).toBe('hello');
}));
it('supports the disabled attribute as binding', fakeAsync(() => {
const fixture = createComponent(MatInputWithDisabled);
fixture.detectChanges();
const wrapperEl = fixture.debugElement.query(
By.css('.mat-mdc-text-field-wrapper'),
)!.nativeElement;
const inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
expect(wrapperEl.classList.contains('mdc-text-field--disabled'))
.withContext(`Expected form field not to start out disabled.`)
.toBe(false);
expect(inputEl.disabled).toBe(false);
fixture.componentInstance.disabled = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(wrapperEl.classList.contains('mdc-text-field--disabled'))
.withContext(`Expected form field to look disabled after property is set.`)
.toBe(true);
expect(inputEl.disabled).toBe(true);
}));
it('should be able to set an input as being disabled and interactive', fakeAsync(() => {
const fixture = createComponent(MatInputWithDisabled);
fixture.componentInstance.disabled = true;
fixture.detectChanges();
const input = fixture.nativeElement.querySelector('input') as HTMLInputElement;
expect(input.disabled).toBe(true);
expect(input.readOnly).toBe(false);
expect(input.hasAttribute('aria-disabled')).toBe(false);
expect(input.classList).not.toContain('mat-mdc-input-disabled-interactive');
fixture.componentInstance.disabledInteractive = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(input.disabled).toBe(false);
expect(input.readOnly).toBe(true);
expect(input.getAttribute('aria-disabled')).toBe('true');
expect(input.classList).toContain('mat-mdc-input-disabled-interactive');
}));
it('should not float the label when disabled and disabledInteractive are set', fakeAsync(() => {
const fixture = createComponent(MatInputTextTestController);
fixture.componentInstance.disabled = fixture.componentInstance.disabledInteractive = true;
fixture.detectChanges();
const label = fixture.nativeElement.querySelector('label');
const input = fixture.debugElement
.query(By.directive(MatInput))!
.injector.get<MatInput>(MatInput);
expect(label.classList).not.toContain('mdc-floating-label--float-above');
// Call the focus handler directly to avoid flakyness where
// browsers don't focus elements if the window is minimized.
input._focusChanged(true);
fixture.detectChanges();
expect(label.classList).not.toContain('mdc-floating-label--float-above');
}));
it('should float the label when disabledInteractive is set and the input has a value', fakeAsync(() => {
const fixture = createComponent(MatInputWithDynamicLabel);
fixture.componentInstance.shouldFloat = 'auto';
fixture.componentInstance.disabled = fixture.componentInstance.disabledInteractive = true;
fixture.detectChanges();
const input = fixture.nativeElement.querySelector('input');
const label = fixture.nativeElement.querySelector('label');
expect(label.classList).not.toContain('mdc-floating-label--float-above');
input.value = 'Text';
dispatchFakeEvent(input, 'input');
fixture.detectChanges();
expect(label.classList).toContain('mdc-floating-label--float-above');
}));
it('supports the disabled attribute as binding for select', fakeAsync(() => {
const fixture = createComponent(MatInputSelect);
fixture.detectChanges();
const wrapperEl = fixture.debugElement.query(
By.css('.mat-mdc-text-field-wrapper'),
)!.nativeElement;
const selectEl = fixture.debugElement.query(By.css('select'))!.nativeElement;
expect(wrapperEl.classList.contains('mdc-text-field--disabled'))
.withContext(`Expected form field not to start out disabled.`)
.toBe(false);
expect(selectEl.disabled).toBe(false);
fixture.componentInstance.disabled = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(wrapperEl.classList.contains('mdc-text-field--disabled'))
.withContext(`Expected form field to look disabled after property is set.`)
.toBe(true);
expect(selectEl.disabled).toBe(true);
}));
it('should add a class to the form field if it has a native select', fakeAsync(() => {
const fixture = createComponent(MatInputSelect);
fixture.detectChanges();
const formField = fixture.debugElement.query(By.css('.mat-mdc-form-field'))!.nativeElement;
expect(formField.classList).toContain('mat-mdc-form-field-type-mat-native-select');
}));
it('supports the required attribute as binding', fakeAsync(() => {
let fixture = createComponent(MatInputWithRequired);
fixture.detectChanges();
let inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
expect(inputEl.required).toBe(false);
fixture.componentInstance.required = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(inputEl.required).toBe(true);
}));
it('supports the required attribute as binding for select', fakeAsync(() => {
const fixture = createComponent(MatInputSelect);
fixture.detectChanges();
const selectEl = fixture.debugElement.query(By.css('select'))!.nativeElement;
expect(selectEl.required).toBe(false);
fixture.componentInstance.required = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(selectEl.required).toBe(true);
}));
it('supports the type attribute as binding', fakeAsync(() => {
let fixture = createComponent(MatInputWithType);
fixture.detectChanges();
let inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
expect(inputEl.type).toBe('text');
fixture.componentInstance.type = 'password';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(inputEl.type).toBe('password');
}));
it('supports textarea', fakeAsync(() => {
let fixture = createComponent(MatInputTextareaWithBindings);
fixture.detectChanges();
const textarea: HTMLTextAreaElement = fixture.nativeElement.querySelector('textarea');
expect(textarea).not.toBeNull();
}));
it('supports select', fakeAsync(() => {
const fixture = createComponent(MatInputSelect);
fixture.detectChanges();
const nativeSelect: HTMLTextAreaElement = fixture.nativeElement.querySelector('select');
expect(nativeSelect).not.toBeNull();
}));
it('sets the aria-describedby when a hintLabel is set', fakeAsync(() => {
let fixture = createComponent(MatInputHintLabelTestController);
fixture.componentInstance.label = 'label';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
const hint = fixture.debugElement.query(By.css('.mat-mdc-form-field-hint'))!.nativeElement;
const input = fixture.debugElement.query(By.css('input'))!.nativeElement;
const hintId = hint.getAttribute('id');
expect(input.getAttribute('aria-describedby')).toBe(`initial ${hintId}`);
}));
it('should show outline label correctly based on initial condition to false', fakeAsync(() => {
const fixture = createComponent(MatInputOutlineWithConditionalLabel);
fixture.detectChanges();
tick(16);
const notchedOutline: HTMLElement = fixture.debugElement.query(
By.directive(MatFormFieldNotchedOutline),
).nativeElement;
console.log('notchedOutline', notchedOutline.classList);
expect(notchedOutline.classList).toContain('mdc-notched-outline--no-label');
expect(notchedOutline.classList).not.toContain('mdc-notched-outline--upgraded');
fixture.componentInstance.showLabel = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
tick(16);
expect(notchedOutline.classList).not.toContain('mdc-notched-outline--no-label');
expect(notchedOutline.classList).toContain('mdc-notched-outline--upgraded');
}));
it('supports user binding to aria-describedby', fakeAsync(() => {
let fixture = createComponent(MatInputWithSubscriptAndAriaDescribedBy);
fixture.componentInstance.label = 'label';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
const hint = fixture.debugElement.query(By.css('.mat-mdc-form-field-hint'))!.nativeElement;
const input = fixture.debugElement.query(By.css('input'))!.nativeElement;
const hintId = hint.getAttribute('id');
expect(input.getAttribute('aria-describedby')).toBe(hintId);
fixture.componentInstance.userDescribedByValue = 'custom-error custom-error-two';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(input.getAttribute('aria-describedby')).toBe(`custom-error custom-error-two ${hintId}`);
fixture.componentInstance.userDescribedByValue = 'custom-error';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(input.getAttribute('aria-describedby')).toBe(`custom-error ${hintId}`);
fixture.componentInstance.showError = true;
fixture.changeDetectorRef.markForCheck();
fixture.componentInstance.formControl.markAsTouched();
fixture.componentInstance.formControl.setErrors({invalid: true});
fixture.detectChanges();
expect(input.getAttribute('aria-describedby')).toMatch(/^custom-error mat-mdc-error-\w+\d+$/);
fixture.componentInstance.label = '';
fixture.componentInstance.userDescribedByValue = '';
fixture.componentInstance.showError = false;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(input.hasAttribute('aria-describedby')).toBe(false);
}));
it('sets the aria-describedby to the id of the mat-hint', fakeAsync(() => {
let fixture = createComponent(MatInputHintLabel2TestController);
fixture.componentInstance.label = 'label';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
let hint = fixture.debugElement.query(By.css('.mat-mdc-form-field-hint'))!.nativeElement;
let input = fixture.debugElement.query(By.css('input'))!.nativeElement;
expect(input.getAttribute('aria-describedby')).toBe(hint.getAttribute('id'));
}));
it('sets the aria-describedby with multiple mat-hint instances', fakeAsync(() => {
let fixture = createComponent(MatInputMultipleHintTestController);
fixture.componentInstance.startId = 'start';
fixture.componentInstance.endId = 'end';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
let input = fixture.debugElement.query(By.css('input'))!.nativeElement;
expect(input.getAttribute('aria-describedby')).toBe('start end');
}));
it('should preserve aria-describedby set directly in the DOM', fakeAsync(() => {
const fixture = createComponent(MatInputHintLabel2TestController);
const input = fixture.nativeElement.querySelector('input');
input.setAttribute('aria-describedby', 'custom');
fixture.componentInstance.label = 'label';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
const hint = fixture.nativeElement.querySelector('.mat-mdc-form-field-hint');
expect(input.getAttribute('aria-describedby')).toBe(`${hint.getAttribute('id')} custom`);
}));
it('should set a class on the hint element based on its alignment', fakeAsync(() => {
const fixture = createComponent(MatInputMultipleHintTestController);
fixture.componentInstance.startId = 'start';
fixture.componentInstance.endId = 'end';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
const start = fixture.nativeElement.querySelector('#start');
const end = fixture.nativeElement.querySelector('#end');
expect(start.classList).not.toContain('mat-mdc-form-field-hint-end');
expect(end.classList).toContain('mat-mdc-form-field-hint-end');
}));
it('sets the aria-describedby when a hintLabel is set, in addition to a mat-hint', fakeAsync(() => {
let fixture = createComponent(MatInputMultipleHintMixedTestController);
fixture.detectChanges();
let hintLabel = fixture.debugElement.query(
By.css('.mat-mdc-form-field-hint:not(.mat-mdc-form-field-hint-end)'),
)!.nativeElement;
let endLabel = fixture.debugElement.query(
By.css('.mat-mdc-form-field-hint.mat-mdc-form-field-hint-end'),
)!.nativeElement;
let input = fixture.debugElement.query(By.css('input'))!.nativeElement;
let ariaValue = input.getAttribute('aria-describedby');
expect(ariaValue).toBe(`${hintLabel.getAttribute('id')} ${endLabel.getAttribute('id')}`);
}));
it('should float when floatLabel is set to default and text is entered', fakeAsync(() => {
let fixture = createComponent(MatInputWithDynamicLabel);
fixture.detectChanges();
let inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
let labelEl = fixture.debugElement.query(By.css('label'))!.nativeElement;
expect(labelEl.classList).toContain('mdc-floating-label--float-above');
fixture.componentInstance.shouldFloat = 'auto';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(labelEl.classList).not.toContain('mdc-floating-label--float-above');
// Update the value of the input.
inputEl.value = 'Text';
// Fake behavior of the `(input)` event which should trigger a change detection.
dispatchFakeEvent(inputEl, 'input');
fixture.detectChanges();
expect(labelEl.classList).toContain('mdc-floating-label--float-above');
}));
it('should always float the label when floatLabel is set to always', fakeAsync(() => {
let fixture = createComponent(MatInputWithDynamicLabel);
fixture.detectChanges();
let inputEl = fixture.debugElement.query(By.css('input'))!.nativeElement;
let labelEl = fixture.debugElement.query(By.css('label'))!.nativeElement;
expect(labelEl.classList).toContain('mdc-floating-label--float-above');
fixture.detectChanges();
// Update the value of the input.
inputEl.value = 'Text';
// Fake behavior of the `(input)` event which should trigger a change detection.
fixture.detectChanges();
expect(labelEl.classList).toContain('mdc-floating-label--float-above');
}));
it('should float labels when select has value', fakeAsync(() => {
const fixture = createComponent(MatInputSelect);
fixture.detectChanges();
const labelEl = fixture.debugElement.query(By.css('label'))!.nativeElement;
expect(labelEl.classList).toContain('mdc-floating-label--float-above');
}));
it('should mark a multi-select as being inline', fakeAsync(() => {
const fixture = createComponent(MatInputSelect);
fixture.detectChanges();
const select: HTMLSelectElement = fixture.nativeElement.querySelector('select');
expect(select.classList).not.toContain('mat-mdc-native-select-inline');
select.multiple = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(select.classList).toContain('mat-mdc-native-select-inline');
}));
it('should mark a select with a size as being inline', fakeAsync(() => {
const fixture = createComponent(MatInputSelect);
fixture.detectChanges();
const select: HTMLSelectElement = fixture.nativeElement.querySelector('select');
expect(select.classList).not.toContain('mat-mdc-native-select-inline');
select.size = 3;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(select.classList).toContain('mat-mdc-native-select-inline');
select.size = 1;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(select.classList).not.toContain('mat-mdc-native-select-inline');
}));
it('should not float the label if the selectedIndex is negative', fakeAsync(() => {
const fixture = createComponent(MatInputSelect);
fixture.detectChanges();
const labelEl = fixture.debugElement.query(By.css('label'))!.nativeElement;
const selectEl: HTMLSelectElement = fixture.nativeElement.querySelector('select');
expect(labelEl.classList).toContain('mdc-floating-label--float-above');
selectEl.selectedIndex = -1;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(labelEl.classList).not.toContain('mdc-floating-label--float-above');
}));
it('should not float labels when select has no value, no option label, no option innerHtml', fakeAsync(() => {
const fixture = createComponent(MatInputSelectWithNoLabelNoValue);
fixture.detectChanges();
const labelEl = fixture.debugElement.query(By.css('label'))!.nativeElement;
expect(labelEl.classList).not.toContain('mdc-floating-label--float-above');
}));
it('should floating labels when select has no value but has option label', fakeAsync(() => {
const fixture = createComponent(MatInputSelectWithLabel);
fixture.detectChanges();
const labelEl = fixture.debugElement.query(By.css('label'))!.nativeElement;
expect(labelEl.classList).toContain('mdc-floating-label--float-above');
}));
it('should floating labels when select has no value but has option innerHTML', fakeAsync(() => {
const fixture = createComponent(MatInputSelectWithInnerHtml);
fixture.detectChanges();
const labelEl = fixture.debugElement.query(By.css('label'))!.nativeElement;
expect(labelEl.classList).toContain('mdc-floating-label--float-above');
}));
it('should not throw if a native select does not have options', fakeAsync(() => {
const fixture = createComponent(MatInputSelectWithoutOptions);
expect(() => fixture.detectChanges()).not.toThrow();
}));
it('should be able to toggle the floating label programmatically', fakeAsync(() => {
const fixture = createComponent(MatInputWithId);
fixture.detectChanges();
const formField = fixture.debugElement.query(By.directive(MatFormField))!;
const containerInstance = formField.componentInstance as MatFormField;
const label = formField.nativeElement.querySelector('label');
expect(containerInstance.floatLabel).toBe('auto');
expect(label.classList).not.toContain('mdc-floating-label--float-above');
fixture.componentInstance.floatLabel = 'always';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
expect(containerInstance.floatLabel).toBe('always');
expect(label.classList).toContain('mdc-floating-label--float-above');
}));
it('should not have prefix and suffix elements when none are specified', fakeAsync(() => {
let fixture = createComponent(MatInputWithId);
fixture.detectChanges();
let prefixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-prefix'));
let suffixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-suffix'));
expect(prefixEl).toBeNull();
expect(suffixEl).toBeNull();
}));
it('should add prefix and suffix elements when specified', fakeAsync(() => {
const fixture = createComponent(MatInputWithPrefixAndSuffix);
fixture.detectChanges();
const textPrefixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-text-prefix'))!;
const textSuffixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-text-suffix'))!;
const iconPrefixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-icon-prefix'))!;
const iconSuffixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-icon-suffix'))!;
expect(textPrefixEl).not.toBeNull();
expect(textSuffixEl).not.toBeNull();
expect(iconPrefixEl).not.toBeNull();
expect(iconSuffixEl).not.toBeNull();
expect(textPrefixEl.nativeElement.innerText.trim()).toEqual('Prefix');
expect(textSuffixEl.nativeElement.innerText.trim()).toEqual('Suffix');
expect(iconPrefixEl.nativeElement.innerText.trim()).toEqual('favorite');
expect(iconSuffixEl.nativeElement.innerText.trim()).toEqual('favorite');
}));
it('should allow ng-container as prefix and suffix', () => {
const fixture = createComponent(InputWithNgContainerPrefixAndSuffix);
fixture.detectChanges();
const textPrefixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-text-prefix'))!;
const textSuffixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-text-suffix'))!;
const iconPrefixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-icon-prefix'))!;
const iconSuffixEl = fixture.debugElement.query(By.css('.mat-mdc-form-field-icon-suffix'))!;
expect(textPrefixEl.nativeElement.innerText.trim()).toEqual('text-prefix');
expect(textSuffixEl.nativeElement.innerText.trim()).toEqual('text-suffix');
expect(iconPrefixEl.nativeElement.innerText.trim()).toEqual('icon-prefix');
expect(iconSuffixEl.nativeElement.innerText.trim()).toEqual('icon-suffix');
});
it('should update empty class when value changes programmatically and OnPush', fakeAsync(() => {
let fixture = createComponent(MatInputOnPush);
fixture.detectChanges();
let component = fixture.componentInstance;
let label = fixture.debugElement.query(By.css('label'))!.nativeElement;
expect(label.classList).not.toContain('mdc-floating-label--float-above');
component.formControl.setValue('something');
fixture.detectChanges();
expect(label.classList).toContain('mdc-floating-label--float-above');
}));
it('should set the focused class when the input is focused', fakeAsync(() => {
let fixture = createComponent(MatInputTextTestController);
fixture.detectChanges();
let input = fixture.debugElement
.query(By.directive(MatInput))!
.injector.get<MatInput>(MatInput);
let container = fixture.debugElement.query(By.css('.mat-mdc-form-field'))!.nativeElement;
// Call the focus handler directly to avoid flakyness where
// browsers don't focus elements if the window is minimized.
input._focusChanged(true);
fixture.detectChanges();
expect(container.classList).toContain('mat-focused');
}));
it('should remove the focused class if the input becomes disabled while focused', fakeAsync(() => {
const fixture = createComponent(MatInputTextTestController);
fixture.detectChanges();
const input = fixture.debugElement
.query(By.directive(MatInput))!
.injector.get<MatInput>(MatInput);
const container = fixture.debugElement.query(By.css('.mat-mdc-form-field'))!.nativeElement;
// Call the focus handler directly to avoid flakyness where
// browsers don't focus elements if the window is minimized.
input._focusChanged(true);
fixture.detectChanges();
expect(container.classList).toContain('mat-focused');
input.disabled = true;
fixture.detectChanges();
expect(container.classList).not.toContain('mat-focused');
}));
it('should only show the native control placeholder, when there is a label, on focus', () => {
const fixture = createComponent(MatInputWithLabelAndPlaceholder);
fixture.detectChanges();
const container = fixture.debugElement.query(By.css('mat-form-field'))!.nativeElement;
const label = fixture.debugElement.query(By.css('label'))!.nativeElement;
const input = fixture.debugElement.query(By.css('input'))!.nativeElement;