|
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 | + MdInputContainerWithCustomErrorStateFunction, |
59 | 61 | MdInputContainerWithFormGroupErrorMessages, |
60 | 62 | MdInputContainerWithId, |
61 | 63 | MdInputContainerWithPrefixAndSuffix, |
@@ -669,6 +671,36 @@ describe('MdInputContainer', function () { |
669 | 671 | }); |
670 | 672 | })); |
671 | 673 |
|
| 674 | + it('should display an error message when a custom error function returns true', async(() => { |
| 675 | + fixture.destroy(); |
| 676 | + |
| 677 | + let customFixture = TestBed.createComponent(MdInputContainerWithCustomErrorStateFunction); |
| 678 | + let component: MdInputContainerWithCustomErrorStateFunction; |
| 679 | + |
| 680 | + customFixture.detectChanges(); |
| 681 | + component = customFixture.componentInstance; |
| 682 | + containerEl = customFixture.debugElement.query(By.css('md-input-container')).nativeElement; |
| 683 | + |
| 684 | + expect(component.formControl.invalid).toBe(true, 'Expected form control to be invalid'); |
| 685 | + expect(containerEl.querySelectorAll('md-error').length).toBe(0, 'Expected no error messages'); |
| 686 | + |
| 687 | + component.formControl.markAsTouched(); |
| 688 | + customFixture.detectChanges(); |
| 689 | + |
| 690 | + customFixture.whenStable().then(() => { |
| 691 | + expect(containerEl.querySelectorAll('md-error').length) |
| 692 | + .toBe(0, 'Expected no error messages after being touched.'); |
| 693 | + |
| 694 | + component.errorState = true; |
| 695 | + customFixture.detectChanges(); |
| 696 | + |
| 697 | + customFixture.whenStable().then(() => { |
| 698 | + expect(containerEl.querySelectorAll('md-error').length) |
| 699 | + .toBe(1, 'Expected one error messages to have been rendered.'); |
| 700 | + }); |
| 701 | + }); |
| 702 | + })); |
| 703 | + |
672 | 704 | it('should hide the errors and show the hints once the input becomes valid', async(() => { |
673 | 705 | testComponent.formControl.markAsTouched(); |
674 | 706 | fixture.detectChanges(); |
@@ -982,6 +1014,27 @@ class MdInputContainerWithFormErrorMessages { |
982 | 1014 | renderError = true; |
983 | 1015 | } |
984 | 1016 |
|
| 1017 | +@Component({ |
| 1018 | + template: ` |
| 1019 | + <form #form="ngForm" novalidate> |
| 1020 | + <md-input-container [errorStateFn]="customErrorStateFn.bind(this)"> |
| 1021 | + <input mdInput [formControl]="formControl"> |
| 1022 | + <md-hint>Please type something</md-hint> |
| 1023 | + <md-error>This field is required</md-error> |
| 1024 | + </md-input-container> |
| 1025 | + </form> |
| 1026 | + ` |
| 1027 | +}) |
| 1028 | +class MdInputContainerWithCustomErrorStateFunction { |
| 1029 | + @ViewChild('form') form: NgForm; |
| 1030 | + formControl = new FormControl('', Validators.required); |
| 1031 | + errorState = false; |
| 1032 | + |
| 1033 | + customErrorStateFn(c: NgControl): boolean { |
| 1034 | + return this.errorState; |
| 1035 | + } |
| 1036 | +} |
| 1037 | + |
985 | 1038 | @Component({ |
986 | 1039 | template: ` |
987 | 1040 | <form [formGroup]="formGroup" novalidate> |
|
0 commit comments