|
6 | 6 | FormGroupDirective, |
7 | 7 | FormsModule, |
8 | 8 | NgForm, |
| 9 | + NgControl, |
9 | 10 | ReactiveFormsModule, |
10 | 11 | Validators |
11 | 12 | } from '@angular/forms'; |
@@ -56,6 +57,7 @@ describe('MdInputContainer', function () { |
56 | 57 | MdInputContainerWithDynamicPlaceholder, |
57 | 58 | MdInputContainerWithFormControl, |
58 | 59 | MdInputContainerWithFormErrorMessages, |
| 60 | + MdInputContainerWithCustomErrorStateMatcher, |
59 | 61 | MdInputContainerWithFormGroupErrorMessages, |
60 | 62 | MdInputContainerWithId, |
61 | 63 | MdInputContainerWithPrefixAndSuffix, |
@@ -682,6 +684,36 @@ describe('MdInputContainer', function () { |
682 | 684 | }); |
683 | 685 | })); |
684 | 686 |
|
| 687 | + it('should display an error message when a custom error matcher returns true', async(() => { |
| 688 | + fixture.destroy(); |
| 689 | + |
| 690 | + let customFixture = TestBed.createComponent(MdInputContainerWithCustomErrorStateMatcher); |
| 691 | + let component: MdInputContainerWithCustomErrorStateMatcher; |
| 692 | + |
| 693 | + customFixture.detectChanges(); |
| 694 | + component = customFixture.componentInstance; |
| 695 | + containerEl = customFixture.debugElement.query(By.css('md-input-container')).nativeElement; |
| 696 | + |
| 697 | + expect(component.formControl.invalid).toBe(true, 'Expected form control to be invalid'); |
| 698 | + expect(containerEl.querySelectorAll('md-error').length).toBe(0, 'Expected no error messages'); |
| 699 | + |
| 700 | + component.formControl.markAsTouched(); |
| 701 | + customFixture.detectChanges(); |
| 702 | + |
| 703 | + customFixture.whenStable().then(() => { |
| 704 | + expect(containerEl.querySelectorAll('md-error').length) |
| 705 | + .toBe(0, 'Expected no error messages after being touched.'); |
| 706 | + |
| 707 | + component.errorState = true; |
| 708 | + customFixture.detectChanges(); |
| 709 | + |
| 710 | + customFixture.whenStable().then(() => { |
| 711 | + expect(containerEl.querySelectorAll('md-error').length) |
| 712 | + .toBe(1, 'Expected one error messages to have been rendered.'); |
| 713 | + }); |
| 714 | + }); |
| 715 | + })); |
| 716 | + |
685 | 717 | it('should hide the errors and show the hints once the input becomes valid', async(() => { |
686 | 718 | testComponent.formControl.markAsTouched(); |
687 | 719 | fixture.detectChanges(); |
@@ -995,6 +1027,29 @@ class MdInputContainerWithFormErrorMessages { |
995 | 1027 | renderError = true; |
996 | 1028 | } |
997 | 1029 |
|
| 1030 | +@Component({ |
| 1031 | + template: ` |
| 1032 | + <form #form="ngForm" novalidate> |
| 1033 | + <md-input-container> |
| 1034 | + <input mdInput |
| 1035 | + [formControl]="formControl" |
| 1036 | + [errorStateMatcher]="customErrorStateMatcher.bind(this)"> |
| 1037 | + <md-hint>Please type something</md-hint> |
| 1038 | + <md-error>This field is required</md-error> |
| 1039 | + </md-input-container> |
| 1040 | + </form> |
| 1041 | + ` |
| 1042 | +}) |
| 1043 | +class MdInputContainerWithCustomErrorStateMatcher { |
| 1044 | + @ViewChild('form') form: NgForm; |
| 1045 | + formControl = new FormControl('', Validators.required); |
| 1046 | + errorState = false; |
| 1047 | + |
| 1048 | + customErrorStateMatcher(c: NgControl): boolean { |
| 1049 | + return this.errorState; |
| 1050 | + } |
| 1051 | +} |
| 1052 | + |
998 | 1053 | @Component({ |
999 | 1054 | template: ` |
1000 | 1055 | <form [formGroup]="formGroup" novalidate> |
|
0 commit comments