/** * @license * Copyright Google Inc. All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import {Component, Input} from '@angular/core'; import {TestBed, async, fakeAsync, tick} from '@angular/core/testing'; import {ControlValueAccessor, FormsModule, NG_VALUE_ACCESSOR, NgForm} from '@angular/forms'; import {By} from '@angular/platform-browser/src/dom/debug/by'; import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter'; import {dispatchEvent} from '@angular/platform-browser/testing/browser_util'; export function main() { describe('template-driven forms integration tests', () => { beforeEach(() => { TestBed.configureTestingModule({ declarations: [ StandaloneNgModel, NgModelForm, NgModelGroupForm, NgModelValidBinding, NgModelNgIfForm, NgModelRadioForm, NgModelSelectForm, NgNoFormComp, InvalidNgModelNoName, NgModelOptionsStandalone, NgModelCustomComp, NgModelCustomWrapper, NgModelValidationBindings ], imports: [FormsModule] }); }); describe('basic functionality', () => { it('should support ngModel for standalone fields', fakeAsync(() => { const fixture = TestBed.createComponent(StandaloneNgModel); fixture.componentInstance.name = 'oldValue'; fixture.detectChanges(); tick(); // model -> view const input = fixture.debugElement.query(By.css('input')).nativeElement; expect(input.value).toEqual('oldValue'); input.value = 'updatedValue'; dispatchEvent(input, 'input'); tick(); // view -> model expect(fixture.componentInstance.name).toEqual('updatedValue'); })); it('should support ngModel registration with a parent form', fakeAsync(() => { const fixture = TestBed.createComponent(NgModelForm); fixture.componentInstance.name = 'Nancy'; fixture.detectChanges(); tick(); const form = fixture.debugElement.children[0].injector.get(NgForm); expect(form.value).toEqual({name: 'Nancy'}); expect(form.valid).toBe(false); })); it('should support ngModelGroup', fakeAsync(() => { const fixture = TestBed.createComponent(NgModelGroupForm); fixture.componentInstance.first = 'Nancy'; fixture.componentInstance.last = 'Drew'; fixture.componentInstance.email = 'some email'; fixture.detectChanges(); tick(); // model -> view const inputs = fixture.debugElement.queryAll(By.css('input')); expect(inputs[0].nativeElement.value).toEqual('Nancy'); expect(inputs[1].nativeElement.value).toEqual('Drew'); inputs[0].nativeElement.value = 'Carson'; dispatchEvent(inputs[0].nativeElement, 'input'); tick(); // view -> model const form = fixture.debugElement.children[0].injector.get(NgForm); expect(form.value).toEqual({name: {first: 'Carson', last: 'Drew'}, email: 'some email'}); })); it('should add controls and control groups to form control model', fakeAsync(() => { const fixture = TestBed.createComponent(NgModelGroupForm); fixture.componentInstance.first = 'Nancy'; fixture.componentInstance.last = 'Drew'; fixture.componentInstance.email = 'some email'; fixture.detectChanges(); tick(); const form = fixture.debugElement.children[0].injector.get(NgForm); expect(form.control.get('name').value).toEqual({first: 'Nancy', last: 'Drew'}); expect(form.control.get('name.first').value).toEqual('Nancy'); expect(form.control.get('email').value).toEqual('some email'); })); it('should remove controls and control groups from form control model', fakeAsync(() => { const fixture = TestBed.createComponent(NgModelNgIfForm); fixture.componentInstance.emailShowing = true; fixture.componentInstance.first = 'Nancy'; fixture.componentInstance.email = 'some email'; fixture.detectChanges(); tick(); const form = fixture.debugElement.children[0].injector.get(NgForm); expect(form.control.get('email').value).toEqual('some email'); expect(form.value).toEqual({name: {first: 'Nancy'}, email: 'some email'}); // should remove individual control successfully fixture.componentInstance.emailShowing = false; fixture.detectChanges(); tick(); expect(form.control.get('email')).toBe(null); expect(form.value).toEqual({name: {first: 'Nancy'}}); expect(form.control.get('name').value).toEqual({first: 'Nancy'}); expect(form.control.get('name.first').value).toEqual('Nancy'); // should remove form group successfully fixture.componentInstance.groupShowing = false; fixture.detectChanges(); tick(); expect(form.control.get('name')).toBe(null); expect(form.control.get('name.first')).toBe(null); expect(form.value).toEqual({}); })); it('should set status classes with ngModel', async(() => { const fixture = TestBed.createComponent(NgModelForm); fixture.componentInstance.name = 'aa'; fixture.detectChanges(); fixture.whenStable().then(() => { fixture.detectChanges(); const input = fixture.debugElement.query(By.css('input')).nativeElement; const form = fixture.debugElement.children[0].injector.get(NgForm); expect(sortedClassList(input)).toEqual(['ng-invalid', 'ng-pristine', 'ng-untouched']); dispatchEvent(input, 'blur'); fixture.detectChanges(); expect(sortedClassList(input)).toEqual(['ng-invalid', 'ng-pristine', 'ng-touched']); input.value = 'updatedValue'; dispatchEvent(input, 'input'); fixture.detectChanges(); expect(sortedClassList(input)).toEqual(['ng-dirty', 'ng-touched', 'ng-valid']); }); })); it('should set status classes with ngModelGroup and ngForm', async(() => { const fixture = TestBed.createComponent(NgModelGroupForm); fixture.componentInstance.first = ''; fixture.detectChanges(); const form = fixture.debugElement.query(By.css('form')).nativeElement; const modelGroup = fixture.debugElement.query(By.css('[ngModelGroup]')).nativeElement; const input = fixture.debugElement.query(By.css('input')).nativeElement; // ngModelGroup creates its control asynchronously fixture.whenStable().then(() => { fixture.detectChanges(); expect(sortedClassList(modelGroup)).toEqual([ 'ng-invalid', 'ng-pristine', 'ng-untouched' ]); expect(sortedClassList(form)).toEqual(['ng-invalid', 'ng-pristine', 'ng-untouched']); dispatchEvent(input, 'blur'); fixture.detectChanges(); expect(sortedClassList(modelGroup)).toEqual([ 'ng-invalid', 'ng-pristine', 'ng-touched' ]); expect(sortedClassList(form)).toEqual(['ng-invalid', 'ng-pristine', 'ng-touched']); input.value = 'updatedValue'; dispatchEvent(input, 'input'); fixture.detectChanges(); expect(sortedClassList(modelGroup)).toEqual(['ng-dirty', 'ng-touched', 'ng-valid']); expect(sortedClassList(form)).toEqual(['ng-dirty', 'ng-touched', 'ng-valid']); }); })); it('should not create a template-driven form when ngNoForm is used', () => { const fixture = TestBed.createComponent(NgNoFormComp); fixture.detectChanges(); expect(fixture.debugElement.children[0].providerTokens.length).toEqual(0); }); }); describe('name and ngModelOptions', () => { it('should throw if ngModel has a parent form but no name attr or standalone label', () => { const fixture = TestBed.createComponent(InvalidNgModelNoName); expect(() => fixture.detectChanges()) .toThrowError(new RegExp(`name attribute must be set`)); }); it('should not throw if ngModel has a parent form, no name attr, and a standalone label', () => { const fixture = TestBed.createComponent(NgModelOptionsStandalone); expect(() => fixture.detectChanges()).not.toThrow(); }); it('should not register standalone ngModels with parent form', fakeAsync(() => { const fixture = TestBed.createComponent(NgModelOptionsStandalone); fixture.componentInstance.one = 'some data'; fixture.componentInstance.two = 'should not show'; fixture.detectChanges(); tick(); const form = fixture.debugElement.children[0].injector.get(NgForm); const inputs = fixture.debugElement.queryAll(By.css('input')); tick(); expect(form.value).toEqual({one: 'some data'}); expect(inputs[1].nativeElement.value).toEqual('should not show'); })); it('should override name attribute with ngModelOptions name if provided', fakeAsync(() => { const fixture = TestBed.createComponent(NgModelForm); fixture.componentInstance.options = {name: 'override'}; fixture.componentInstance.name = 'some data'; fixture.detectChanges(); tick(); const form = fixture.debugElement.children[0].injector.get(NgForm); expect(form.value).toEqual({override: 'some data'}); })); }); describe('submit and reset events', () => { it('should emit ngSubmit event on submit', fakeAsync(() => { const fixture = TestBed.createComponent(NgModelForm); fixture.componentInstance.name = 'old'; const form = fixture.debugElement.query(By.css('form')); dispatchEvent(form.nativeElement, 'submit'); tick(); expect(fixture.componentInstance.name).toEqual('submitted'); })); it('should mark NgForm as submitted on submit event', fakeAsync(() => { const fixture = TestBed.createComponent(NgModelForm); tick(); const form = fixture.debugElement.children[0].injector.get(NgForm); expect(form.submitted).toBe(false); const formEl = fixture.debugElement.query(By.css('form')).nativeElement; dispatchEvent(formEl, 'submit'); tick(); expect(form.submitted).toBe(true); })); it('should reset the form to empty when reset event is fired', fakeAsync(() => { const fixture = TestBed.createComponent(NgModelForm); fixture.componentInstance.name = 'should be cleared'; fixture.detectChanges(); tick(); const form = fixture.debugElement.children[0].injector.get(NgForm); const formEl = fixture.debugElement.query(By.css('form')); const input = fixture.debugElement.query(By.css('input')); expect(input.nativeElement.value).toBe('should be cleared'); // view value expect(fixture.componentInstance.name).toBe('should be cleared'); // ngModel value expect(form.value.name).toEqual('should be cleared'); // control value dispatchEvent(formEl.nativeElement, 'reset'); fixture.detectChanges(); tick(); expect(input.nativeElement.value).toBe(''); // view value expect(fixture.componentInstance.name).toBe(null); // ngModel value expect(form.value.name).toEqual(null); // control value })); it('should reset the form submit state when reset button is clicked', fakeAsync(() => { const fixture = TestBed.createComponent(NgModelForm); const form = fixture.debugElement.children[0].injector.get(NgForm); const formEl = fixture.debugElement.query(By.css('form')); dispatchEvent(formEl.nativeElement, 'submit'); fixture.detectChanges(); tick(); expect(form.submitted).toBe(true); dispatchEvent(formEl.nativeElement, 'reset'); fixture.detectChanges(); tick(); expect(form.submitted).toBe(false); })); }); describe('valueChange and statusChange events', () => { it('should emit valueChanges and statusChanges on init', fakeAsync(() => { const fixture = TestBed.createComponent(NgModelForm); const form = fixture.debugElement.children[0].injector.get(NgForm); fixture.componentInstance.name = 'aa'; fixture.detectChanges(); expect(form.valid).toEqual(true); expect(form.value).toEqual({}); let formValidity: string; let formValue: Object; form.statusChanges.subscribe((status: string) => formValidity = status); form.valueChanges.subscribe((value: string) => formValue = value); tick(); expect(formValidity).toEqual('INVALID'); expect(formValue).toEqual({name: 'aa'}); })); it('should mark controls dirty before emitting the value change event', fakeAsync(() => { const fixture = TestBed.createComponent(NgModelForm); const form = fixture.debugElement.children[0].injector.get(NgForm).form; fixture.detectChanges(); tick(); form.get('name').valueChanges.subscribe( () => { expect(form.get('name').dirty).toBe(true); }); const inputEl = fixture.debugElement.query(By.css('input')).nativeElement; inputEl.value = 'newValue'; dispatchEvent(inputEl, 'input'); })); it('should mark controls pristine before emitting the value change event when resetting ', fakeAsync(() => { const fixture = TestBed.createComponent(NgModelForm); fixture.detectChanges(); tick(); const form = fixture.debugElement.children[0].injector.get(NgForm).form; const formEl = fixture.debugElement.query(By.css('form')).nativeElement; const inputEl = fixture.debugElement.query(By.css('input')).nativeElement; inputEl.value = 'newValue'; dispatchEvent(inputEl, 'input'); expect(form.get('name').pristine).toBe(false); form.get('name').valueChanges.subscribe( () => { expect(form.get('name').pristine).toBe(true); }); dispatchEvent(formEl, 'reset'); })); }); describe('disabled controls', () => { it('should not consider disabled controls in value or validation', fakeAsync(() => { const fixture = TestBed.createComponent(NgModelGroupForm); fixture.componentInstance.isDisabled = false; fixture.componentInstance.first = ''; fixture.componentInstance.last = 'Drew'; fixture.componentInstance.email = 'some email'; fixture.detectChanges(); tick(); const form = fixture.debugElement.children[0].injector.get(NgForm); expect(form.value).toEqual({name: {first: '', last: 'Drew'}, email: 'some email'}); expect(form.valid).toBe(false); expect(form.control.get('name.first').disabled).toBe(false); fixture.componentInstance.isDisabled = true; fixture.detectChanges(); tick(); expect(form.value).toEqual({name: {last: 'Drew'}, email: 'some email'}); expect(form.valid).toBe(true); expect(form.control.get('name.first').disabled).toBe(true); })); it('should add disabled attribute in the UI if disable() is called programmatically', fakeAsync(() => { const fixture = TestBed.createComponent(NgModelGroupForm); fixture.componentInstance.isDisabled = false; fixture.componentInstance.first = 'Nancy'; fixture.detectChanges(); tick(); const form = fixture.debugElement.children[0].injector.get(NgForm); form.control.get('name.first').disable(); fixture.detectChanges(); tick(); const input = fixture.debugElement.query(By.css(`[name="first"]`)); expect(input.nativeElement.disabled).toBe(true); })); it('should disable a custom control if disabled attr is added', async(() => { const fixture = TestBed.createComponent(NgModelCustomWrapper); fixture.componentInstance.name = 'Nancy'; fixture.componentInstance.isDisabled = true; fixture.detectChanges(); fixture.whenStable().then(() => { fixture.detectChanges(); fixture.whenStable().then(() => { const form = fixture.debugElement.children[0].injector.get(NgForm); expect(form.control.get('name').disabled).toBe(true); const customInput = fixture.debugElement.query(By.css('[name="custom"]')); expect(customInput.nativeElement.disabled).toEqual(true); }); }); })); it('should disable a control with unbound disabled attr', fakeAsync(() => { TestBed.overrideComponent(NgModelForm, { set: { template: `
`, } }); const fixture = TestBed.createComponent(NgModelForm); fixture.detectChanges(); tick(); const form = fixture.debugElement.children[0].injector.get(NgForm); expect(form.control.get('name').disabled).toBe(true); const input = fixture.debugElement.query(By.css('input')); expect(input.nativeElement.disabled).toEqual(true); form.control.enable(); fixture.detectChanges(); tick(); expect(input.nativeElement.disabled).toEqual(false); })); it('should disable radio controls properly with programmatic call', fakeAsync(() => { const fixture = TestBed.createComponent(NgModelRadioForm); fixture.componentInstance.food = 'fish'; fixture.detectChanges(); tick(); const form = fixture.debugElement.children[0].injector.get(NgForm); form.control.get('food').disable(); tick(); const inputs = fixture.debugElement.queryAll(By.css('input')); expect(inputs[0].nativeElement.disabled).toBe(true); expect(inputs[1].nativeElement.disabled).toBe(true); expect(inputs[2].nativeElement.disabled).toBe(false); expect(inputs[3].nativeElement.disabled).toBe(false); form.control.disable(); tick(); expect(inputs[0].nativeElement.disabled).toBe(true); expect(inputs[1].nativeElement.disabled).toBe(true); expect(inputs[2].nativeElement.disabled).toBe(true); expect(inputs[3].nativeElement.disabled).toBe(true); form.control.enable(); tick(); expect(inputs[0].nativeElement.disabled).toBe(false); expect(inputs[1].nativeElement.disabled).toBe(false); expect(inputs[2].nativeElement.disabled).toBe(false); expect(inputs[3].nativeElement.disabled).toBe(false); })); }); describe('radio controls', () => { it('should support