|
1 | 1 | import {async, TestBed, inject, ComponentFixture} from '@angular/core/testing'; |
2 | 2 | import {Component, ViewChild} from '@angular/core'; |
3 | | -import {FormsModule, ReactiveFormsModule, FormControl, NgForm, Validators} from '@angular/forms'; |
| 3 | +import { |
| 4 | + FormsModule, |
| 5 | + ReactiveFormsModule, |
| 6 | + FormControl, |
| 7 | + NgForm, |
| 8 | + Validators, |
| 9 | + FormGroupDirective, |
| 10 | + FormGroup, |
| 11 | +} from '@angular/forms'; |
4 | 12 | import {By} from '@angular/platform-browser'; |
5 | 13 | import {MdInputModule} from './index'; |
6 | 14 | import {MdInputContainer, MdInputDirective} from './input-container'; |
@@ -51,7 +59,8 @@ describe('MdInputContainer', function () { |
51 | 59 | MdInputContainerMultipleHintTestController, |
52 | 60 | MdInputContainerMultipleHintMixedTestController, |
53 | 61 | MdInputContainerWithDynamicPlaceholder, |
54 | | - MdInputContainerWithErrorMessages |
| 62 | + MdInputContainerWithFormErrorMessages, |
| 63 | + MdInputContainerWithFormGroupErrorMessages |
55 | 64 | ], |
56 | 65 | }); |
57 | 66 |
|
@@ -553,19 +562,19 @@ describe('MdInputContainer', function () { |
553 | 562 | }); |
554 | 563 |
|
555 | 564 | describe('error messages', () => { |
556 | | - let fixture: ComponentFixture<MdInputContainerWithErrorMessages>; |
557 | | - let testComponent: MdInputContainerWithErrorMessages; |
| 565 | + let fixture: ComponentFixture<MdInputContainerWithFormErrorMessages>; |
| 566 | + let testComponent: MdInputContainerWithFormErrorMessages; |
558 | 567 | let containerEl: HTMLElement; |
559 | 568 |
|
560 | 569 | beforeEach(() => { |
561 | | - fixture = TestBed.createComponent(MdInputContainerWithErrorMessages); |
| 570 | + fixture = TestBed.createComponent(MdInputContainerWithFormErrorMessages); |
562 | 571 | fixture.detectChanges(); |
563 | 572 | testComponent = fixture.componentInstance; |
564 | 573 | containerEl = fixture.debugElement.query(By.css('md-input-container')).nativeElement; |
565 | 574 | }); |
566 | 575 |
|
567 | 576 | it('should not show any errors if the user has not interacted', () => { |
568 | | - expect(testComponent.formControl.pristine).toBe(true, 'Expected untouched form control'); |
| 577 | + expect(testComponent.formControl.untouched).toBe(true, 'Expected untouched form control'); |
569 | 578 | expect(containerEl.querySelectorAll('md-error').length).toBe(0, 'Expected no error messages'); |
570 | 579 | }); |
571 | 580 |
|
@@ -601,6 +610,34 @@ describe('MdInputContainer', function () { |
601 | 610 | }); |
602 | 611 | })); |
603 | 612 |
|
| 613 | + it('should display an error message when the parent form group is submitted', async(() => { |
| 614 | + fixture.destroy(); |
| 615 | + |
| 616 | + let groupFixture = TestBed.createComponent(MdInputContainerWithFormGroupErrorMessages); |
| 617 | + let component: MdInputContainerWithFormGroupErrorMessages; |
| 618 | + |
| 619 | + groupFixture.detectChanges(); |
| 620 | + component = groupFixture.componentInstance; |
| 621 | + containerEl = groupFixture.debugElement.query(By.css('md-input-container')).nativeElement; |
| 622 | + |
| 623 | + expect(component.formControl.invalid).toBe(true, 'Expected form control to be invalid'); |
| 624 | + expect(containerEl.querySelectorAll('md-error').length).toBe(0, 'Expected no error messages'); |
| 625 | + expect(component.formGroupDirective.submitted) |
| 626 | + .toBe(false, 'Expected form not to have been submitted'); |
| 627 | + |
| 628 | + dispatchFakeEvent(groupFixture.debugElement.query(By.css('form')).nativeElement, 'submit'); |
| 629 | + groupFixture.detectChanges(); |
| 630 | + |
| 631 | + groupFixture.whenStable().then(() => { |
| 632 | + expect(component.formGroupDirective.submitted) |
| 633 | + .toBe(true, 'Expected form to have been submitted'); |
| 634 | + expect(containerEl.classList) |
| 635 | + .toContain('mat-input-invalid', 'Expected container to have the invalid CSS class.'); |
| 636 | + expect(containerEl.querySelectorAll('md-error').length) |
| 637 | + .toBe(1, 'Expected one error message to have been rendered.'); |
| 638 | + }); |
| 639 | + })); |
| 640 | + |
604 | 641 | it('should hide the error messages once the input becomes valid', async(() => { |
605 | 642 | testComponent.formControl.markAsTouched(); |
606 | 643 | fixture.detectChanges(); |
@@ -906,12 +943,30 @@ class MdInputContainerMissingMdInputTestController {} |
906 | 943 | </form> |
907 | 944 | ` |
908 | 945 | }) |
909 | | -class MdInputContainerWithErrorMessages { |
| 946 | +class MdInputContainerWithFormErrorMessages { |
910 | 947 | @ViewChild('form') form: NgForm; |
911 | 948 | formControl = new FormControl('', Validators.required); |
912 | 949 | renderError = true; |
913 | 950 | } |
914 | 951 |
|
| 952 | +@Component({ |
| 953 | + template: ` |
| 954 | + <form [formGroup]="formGroup" (ngSubmit)="onSubmit()" novalidate> |
| 955 | + <md-input-container> |
| 956 | + <input mdInput [formControl]="formControl"> |
| 957 | + <md-hint>Please type something</md-hint> |
| 958 | + <md-error>This field is required</md-error> |
| 959 | + </md-input-container> |
| 960 | + </form> |
| 961 | + ` |
| 962 | +}) |
| 963 | +class MdInputContainerWithFormGroupErrorMessages { |
| 964 | + @ViewChild(FormGroupDirective) formGroupDirective: FormGroupDirective; |
| 965 | + onSubmit() { } |
| 966 | + formControl = new FormControl('', Validators.required); |
| 967 | + formGroup = new FormGroup({ name: this.formControl }); |
| 968 | +} |
| 969 | + |
915 | 970 | /** |
916 | 971 | * Gets a RegExp used to detect an angular wrapped error message. |
917 | 972 | * See https://github.com/angular/angular/issues/8348 |
|
0 commit comments