refactor(forms): refactor common validators used in unit tests (#38020)

A util file is added to forms test package:
- it exposes simpleAsyncValidator, asyncValidator and asyncValidatorReturningObservable validators
- it refactors simpleAsyncValidator and asyncValidator to use common promise creation code
- it exposes currentStateOf allowing to get the validation state of a list of AbstractControl

Closes #37831

PR Close #38020
This commit is contained in:
Oussama Ben Brahim
2020-07-13 01:35:09 +02:00
committed by Misko Hevery
parent 8df888dfb4
commit 062b8d90af
5 changed files with 93 additions and 133 deletions

View File

@ -11,29 +11,9 @@ import {AsyncTestCompleter, beforeEach, describe, inject, it} from '@angular/cor
import {AbstractControl, FormArray, FormControl, FormGroup, ValidationErrors, ValidatorFn} from '@angular/forms';
import {Validators} from '@angular/forms/src/validators';
import {of} from 'rxjs';
import {asyncValidator} from './util';
(function() {
function asyncValidator(expected: string, timeouts = {}) {
return (c: AbstractControl) => {
let resolve: (result: any) => void = undefined!;
const promise = new Promise<ValidationErrors|null>(res => {
resolve = res;
});
const t = (timeouts as any)[c.value] != null ? (timeouts as any)[c.value] : 0;
const res = c.value != expected ? {'async': true} : null;
if (t == 0) {
resolve(res);
} else {
setTimeout(() => {
resolve(res);
}, t);
}
return promise;
};
}
describe('FormArray', () => {
describe('adding/removing', () => {
let a: FormArray;