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 Michael Prentice
parent e72267bc00
commit 8a05f311b9
5 changed files with 93 additions and 133 deletions

View File

@ -6,43 +6,14 @@
* found in the LICENSE file at https://angular.io/license
*/
import {EventEmitter} from '@angular/core';
import {fakeAsync, tick} from '@angular/core/testing';
import {AsyncTestCompleter, beforeEach, describe, inject, it} from '@angular/core/testing/src/testing_internal';
import {AbstractControl, AsyncValidatorFn, FormControl, FormGroup, ValidationErrors, Validators} from '@angular/forms';
import {FormControl, FormGroup, Validators} from '@angular/forms';
import {FormArray} from '@angular/forms/src/model';
import {asyncValidator, asyncValidatorReturningObservable} from './util';
(function() {
function asyncValidator(expected: string, timeouts = {}): AsyncValidatorFn {
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;
};
}
function asyncValidatorReturningObservable(c: AbstractControl) {
const e = new EventEmitter<Record<string, boolean>>();
Promise.resolve(null).then(() => {
e.emit({'async': true});
});
return e;
}
function otherAsyncValidator() {
return Promise.resolve({'other': true});
}