
committed by
Victor Berchet

parent
0b665c0ece
commit
d2ad871279
@ -809,5 +809,39 @@ export function main() {
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('updateTreeValidity()', () => {
|
||||
let c: FormControl, c2: FormControl, c3: FormControl;
|
||||
let nested: FormGroup, form: FormGroup;
|
||||
let logger: string[];
|
||||
|
||||
beforeEach(() => {
|
||||
c = new FormControl('one');
|
||||
c2 = new FormControl('two');
|
||||
c3 = new FormControl('three');
|
||||
nested = new FormGroup({one: c, two: c2});
|
||||
form = new FormGroup({nested: nested, three: c3});
|
||||
logger = [];
|
||||
|
||||
c.statusChanges.subscribe(() => logger.push('one'));
|
||||
c2.statusChanges.subscribe(() => logger.push('two'));
|
||||
c3.statusChanges.subscribe(() => logger.push('three'));
|
||||
nested.statusChanges.subscribe(() => logger.push('nested'));
|
||||
form.statusChanges.subscribe(() => logger.push('form'));
|
||||
});
|
||||
|
||||
it('should update tree validity', () => {
|
||||
form._updateTreeValidity();
|
||||
expect(logger).toEqual(['one', 'two', 'nested', 'three', 'form']);
|
||||
});
|
||||
|
||||
it('should not emit events when turned off', () => {
|
||||
form._updateTreeValidity({emitEvent: false});
|
||||
expect(logger).toEqual([]);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
|
@ -160,6 +160,30 @@ export function main() {
|
||||
expect(inputs[0].nativeElement.value).toEqual('Bess');
|
||||
});
|
||||
|
||||
it('should pick up dir validators from form controls', () => {
|
||||
const fixture = TestBed.createComponent(LoginIsEmptyWrapper);
|
||||
const form = new FormGroup({
|
||||
'login': new FormControl(''),
|
||||
'min': new FormControl(''),
|
||||
'max': new FormControl(''),
|
||||
'pattern': new FormControl('')
|
||||
});
|
||||
fixture.debugElement.componentInstance.form = form;
|
||||
fixture.detectChanges();
|
||||
expect(form.get('login').errors).toEqual({required: true});
|
||||
|
||||
const newForm = new FormGroup({
|
||||
'login': new FormControl(''),
|
||||
'min': new FormControl(''),
|
||||
'max': new FormControl(''),
|
||||
'pattern': new FormControl('')
|
||||
});
|
||||
fixture.debugElement.componentInstance.form = newForm;
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(newForm.get('login').errors).toEqual({required: true});
|
||||
});
|
||||
|
||||
it('should pick up dir validators from nested form groups', () => {
|
||||
const fixture = TestBed.createComponent(NestedFormGroupComp);
|
||||
const form = new FormGroup({
|
||||
@ -1024,7 +1048,7 @@ export function main() {
|
||||
expect(form.valid).toEqual(true);
|
||||
});
|
||||
|
||||
it('changes on binded properties should change the validation state of the form', () => {
|
||||
it('changes on bound properties should change the validation state of the form', () => {
|
||||
const fixture = TestBed.createComponent(ValidationBindingsForm);
|
||||
const form = new FormGroup({
|
||||
'login': new FormControl(''),
|
||||
@ -1087,11 +1111,6 @@ export function main() {
|
||||
fixture.debugElement.componentInstance.pattern = null;
|
||||
fixture.detectChanges();
|
||||
|
||||
dispatchEvent(required.nativeElement, 'input');
|
||||
dispatchEvent(minLength.nativeElement, 'input');
|
||||
dispatchEvent(maxLength.nativeElement, 'input');
|
||||
dispatchEvent(pattern.nativeElement, 'input');
|
||||
|
||||
expect(form.hasError('required', ['login'])).toEqual(false);
|
||||
expect(form.hasError('minlength', ['min'])).toEqual(false);
|
||||
expect(form.hasError('maxlength', ['max'])).toEqual(false);
|
||||
@ -1104,6 +1123,43 @@ export function main() {
|
||||
expect(required.nativeElement.getAttribute('pattern')).toEqual(null);
|
||||
});
|
||||
|
||||
it('should support rebound controls with rebound validators', () => {
|
||||
const fixture = TestBed.createComponent(ValidationBindingsForm);
|
||||
const form = new FormGroup({
|
||||
'login': new FormControl(''),
|
||||
'min': new FormControl(''),
|
||||
'max': new FormControl(''),
|
||||
'pattern': new FormControl('')
|
||||
});
|
||||
fixture.debugElement.componentInstance.form = form;
|
||||
fixture.debugElement.componentInstance.required = true;
|
||||
fixture.debugElement.componentInstance.minLen = 3;
|
||||
fixture.debugElement.componentInstance.maxLen = 3;
|
||||
fixture.debugElement.componentInstance.pattern = '.{3,}';
|
||||
fixture.detectChanges();
|
||||
|
||||
const newForm = new FormGroup({
|
||||
'login': new FormControl(''),
|
||||
'min': new FormControl(''),
|
||||
'max': new FormControl(''),
|
||||
'pattern': new FormControl('')
|
||||
});
|
||||
fixture.debugElement.componentInstance.form = newForm;
|
||||
fixture.detectChanges();
|
||||
|
||||
fixture.debugElement.componentInstance.required = false;
|
||||
fixture.debugElement.componentInstance.minLen = null;
|
||||
fixture.debugElement.componentInstance.maxLen = null;
|
||||
fixture.debugElement.componentInstance.pattern = null;
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(newForm.hasError('required', ['login'])).toEqual(false);
|
||||
expect(newForm.hasError('minlength', ['min'])).toEqual(false);
|
||||
expect(newForm.hasError('maxlength', ['max'])).toEqual(false);
|
||||
expect(newForm.hasError('pattern', ['pattern'])).toEqual(false);
|
||||
expect(newForm.valid).toEqual(true);
|
||||
});
|
||||
|
||||
it('should use async validators defined in the html', fakeAsync(() => {
|
||||
const fixture = TestBed.createComponent(UniqLoginWrapper);
|
||||
const form = new FormGroup({'login': new FormControl('')});
|
||||
|
@ -9,7 +9,6 @@
|
||||
import {NgFor, NgIf} from '@angular/common';
|
||||
import {Component, Input} from '@angular/core';
|
||||
import {TestBed, async, fakeAsync, tick} from '@angular/core/testing';
|
||||
import {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
|
||||
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';
|
||||
@ -25,7 +24,8 @@ export function main() {
|
||||
declarations: [
|
||||
StandaloneNgModel, NgModelForm, NgModelGroupForm, NgModelValidBinding, NgModelNgIfForm,
|
||||
NgModelRadioForm, NgModelSelectForm, NgNoFormComp, InvalidNgModelNoName,
|
||||
NgModelOptionsStandalone, NgModelCustomComp, NgModelCustomWrapper
|
||||
NgModelOptionsStandalone, NgModelCustomComp, NgModelCustomWrapper,
|
||||
NgModelValidationBindings
|
||||
],
|
||||
imports: [FormsModule]
|
||||
});
|
||||
@ -574,6 +574,125 @@ export function main() {
|
||||
|
||||
});
|
||||
|
||||
describe('validation directives', () => {
|
||||
|
||||
it('should support dir validators using bindings', fakeAsync(() => {
|
||||
const fixture = TestBed.createComponent(NgModelValidationBindings);
|
||||
fixture.debugElement.componentInstance.required = true;
|
||||
fixture.debugElement.componentInstance.minLen = 3;
|
||||
fixture.debugElement.componentInstance.maxLen = 3;
|
||||
fixture.debugElement.componentInstance.pattern = '.{3,}';
|
||||
fixture.detectChanges();
|
||||
tick();
|
||||
|
||||
const required = fixture.debugElement.query(By.css('[name=required]'));
|
||||
const minLength = fixture.debugElement.query(By.css('[name=minlength]'));
|
||||
const maxLength = fixture.debugElement.query(By.css('[name=maxlength]'));
|
||||
const pattern = fixture.debugElement.query(By.css('[name=pattern]'));
|
||||
|
||||
required.nativeElement.value = '';
|
||||
minLength.nativeElement.value = '1';
|
||||
maxLength.nativeElement.value = '1234';
|
||||
pattern.nativeElement.value = '12';
|
||||
|
||||
dispatchEvent(required.nativeElement, 'input');
|
||||
dispatchEvent(minLength.nativeElement, 'input');
|
||||
dispatchEvent(maxLength.nativeElement, 'input');
|
||||
dispatchEvent(pattern.nativeElement, 'input');
|
||||
fixture.detectChanges();
|
||||
|
||||
const form = fixture.debugElement.children[0].injector.get(NgForm);
|
||||
expect(form.control.hasError('required', ['required'])).toEqual(true);
|
||||
expect(form.control.hasError('minlength', ['minlength'])).toEqual(true);
|
||||
expect(form.control.hasError('maxlength', ['maxlength'])).toEqual(true);
|
||||
expect(form.control.hasError('pattern', ['pattern'])).toEqual(true);
|
||||
|
||||
required.nativeElement.value = '1';
|
||||
minLength.nativeElement.value = '123';
|
||||
maxLength.nativeElement.value = '123';
|
||||
pattern.nativeElement.value = '123';
|
||||
|
||||
dispatchEvent(required.nativeElement, 'input');
|
||||
dispatchEvent(minLength.nativeElement, 'input');
|
||||
dispatchEvent(maxLength.nativeElement, 'input');
|
||||
dispatchEvent(pattern.nativeElement, 'input');
|
||||
|
||||
expect(form.valid).toEqual(true);
|
||||
}));
|
||||
|
||||
it('changes on bound properties should change the validation state of the form',
|
||||
fakeAsync(() => {
|
||||
const fixture = TestBed.createComponent(NgModelValidationBindings);
|
||||
fixture.detectChanges();
|
||||
tick();
|
||||
|
||||
const required = fixture.debugElement.query(By.css('[name=required]'));
|
||||
const minLength = fixture.debugElement.query(By.css('[name=minlength]'));
|
||||
const maxLength = fixture.debugElement.query(By.css('[name=maxlength]'));
|
||||
const pattern = fixture.debugElement.query(By.css('[name=pattern]'));
|
||||
|
||||
required.nativeElement.value = '';
|
||||
minLength.nativeElement.value = '1';
|
||||
maxLength.nativeElement.value = '1234';
|
||||
pattern.nativeElement.value = '12';
|
||||
|
||||
dispatchEvent(required.nativeElement, 'input');
|
||||
dispatchEvent(minLength.nativeElement, 'input');
|
||||
dispatchEvent(maxLength.nativeElement, 'input');
|
||||
dispatchEvent(pattern.nativeElement, 'input');
|
||||
|
||||
const form = fixture.debugElement.children[0].injector.get(NgForm);
|
||||
expect(form.control.hasError('required', ['required'])).toEqual(false);
|
||||
expect(form.control.hasError('minlength', ['minlength'])).toEqual(false);
|
||||
expect(form.control.hasError('maxlength', ['maxlength'])).toEqual(false);
|
||||
expect(form.control.hasError('pattern', ['pattern'])).toEqual(false);
|
||||
expect(form.valid).toEqual(true);
|
||||
|
||||
fixture.debugElement.componentInstance.required = true;
|
||||
fixture.debugElement.componentInstance.minLen = 3;
|
||||
fixture.debugElement.componentInstance.maxLen = 3;
|
||||
fixture.debugElement.componentInstance.pattern = '.{3,}';
|
||||
fixture.detectChanges();
|
||||
|
||||
dispatchEvent(required.nativeElement, 'input');
|
||||
dispatchEvent(minLength.nativeElement, 'input');
|
||||
dispatchEvent(maxLength.nativeElement, 'input');
|
||||
dispatchEvent(pattern.nativeElement, 'input');
|
||||
|
||||
expect(form.control.hasError('required', ['required'])).toEqual(true);
|
||||
expect(form.control.hasError('minlength', ['minlength'])).toEqual(true);
|
||||
expect(form.control.hasError('maxlength', ['maxlength'])).toEqual(true);
|
||||
expect(form.control.hasError('pattern', ['pattern'])).toEqual(true);
|
||||
expect(form.valid).toEqual(false);
|
||||
|
||||
expect(required.nativeElement.getAttribute('required')).toEqual('');
|
||||
expect(fixture.debugElement.componentInstance.minLen.toString())
|
||||
.toEqual(minLength.nativeElement.getAttribute('minlength'));
|
||||
expect(fixture.debugElement.componentInstance.maxLen.toString())
|
||||
.toEqual(maxLength.nativeElement.getAttribute('maxlength'));
|
||||
expect(fixture.debugElement.componentInstance.pattern.toString())
|
||||
.toEqual(pattern.nativeElement.getAttribute('pattern'));
|
||||
|
||||
fixture.debugElement.componentInstance.required = false;
|
||||
fixture.debugElement.componentInstance.minLen = null;
|
||||
fixture.debugElement.componentInstance.maxLen = null;
|
||||
fixture.debugElement.componentInstance.pattern = null;
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(form.control.hasError('required', ['required'])).toEqual(false);
|
||||
expect(form.control.hasError('minlength', ['minlength'])).toEqual(false);
|
||||
expect(form.control.hasError('maxlength', ['maxlength'])).toEqual(false);
|
||||
expect(form.control.hasError('pattern', ['pattern'])).toEqual(false);
|
||||
expect(form.valid).toEqual(true);
|
||||
|
||||
expect(required.nativeElement.getAttribute('required')).toEqual(null);
|
||||
expect(required.nativeElement.getAttribute('minlength')).toEqual(null);
|
||||
expect(required.nativeElement.getAttribute('maxlength')).toEqual(null);
|
||||
expect(required.nativeElement.getAttribute('pattern')).toEqual(null);
|
||||
}));
|
||||
|
||||
});
|
||||
|
||||
describe('ngModel corner cases', () => {
|
||||
it('should update the view when the model is set back to what used to be in the view',
|
||||
fakeAsync(() => {
|
||||
@ -791,6 +910,24 @@ class NgModelCustomWrapper {
|
||||
isDisabled = false;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'ng-model-validation-bindings',
|
||||
template: `
|
||||
<form>
|
||||
<input name="required" ngModel [required]="required">
|
||||
<input name="minlength" ngModel [minlength]="minLen">
|
||||
<input name="maxlength" ngModel [maxlength]="maxLen">
|
||||
<input name="pattern" ngModel [pattern]="pattern">
|
||||
</form>
|
||||
`
|
||||
})
|
||||
class NgModelValidationBindings {
|
||||
required: boolean;
|
||||
minLen: number;
|
||||
maxLen: number;
|
||||
pattern: string;
|
||||
}
|
||||
|
||||
function sortedClassList(el: HTMLElement) {
|
||||
var l = getDOM().classList(el);
|
||||
ListWrapper.sort(l);
|
||||
|
Reference in New Issue
Block a user