2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2016-07-21 20:12:00 -04:00
|
|
|
import {fakeAsync, tick} from '@angular/core/testing';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {describe, expect, it} from '@angular/core/testing/src/testing_internal';
|
2017-02-23 12:53:29 -05:00
|
|
|
import {AbstractControl, AsyncValidatorFn, FormArray, FormControl, Validators} from '@angular/forms';
|
2016-06-26 18:52:50 -04:00
|
|
|
import {Observable} from 'rxjs/Observable';
|
2017-03-16 13:15:17 -04:00
|
|
|
import {of } from 'rxjs/observable/of';
|
|
|
|
import {timer} from 'rxjs/observable/timer';
|
|
|
|
import {first} from 'rxjs/operator/first';
|
|
|
|
import {map} from 'rxjs/operator/map';
|
2016-06-26 18:52:50 -04:00
|
|
|
import {normalizeAsyncValidator} from '../src/directives/normalize_validator';
|
2017-02-23 12:53:29 -05:00
|
|
|
import {AsyncValidator, ValidationErrors, ValidatorFn} from '../src/directives/validators';
|
2016-06-08 18:36:24 -04:00
|
|
|
|
|
|
|
export function main() {
|
2017-02-23 12:53:29 -05:00
|
|
|
function validator(key: string, error: any): ValidatorFn {
|
|
|
|
return (c: AbstractControl) => {
|
|
|
|
const r: ValidationErrors = {};
|
2016-09-09 15:04:38 -04:00
|
|
|
r[key] = error;
|
2016-06-08 18:36:24 -04:00
|
|
|
return r;
|
2016-07-21 20:12:00 -04:00
|
|
|
};
|
2016-06-08 18:36:24 -04:00
|
|
|
}
|
|
|
|
|
2017-02-20 19:26:51 -05:00
|
|
|
class AsyncValidatorDirective implements AsyncValidator {
|
2016-06-26 18:52:50 -04:00
|
|
|
constructor(private expected: string, private error: any) {}
|
|
|
|
|
2017-02-23 12:53:29 -05:00
|
|
|
validate(c: any): Observable<ValidationErrors> {
|
2016-06-26 18:52:50 -04:00
|
|
|
return Observable.create((obs: any) => {
|
|
|
|
const error = this.expected !== c.value ? this.error : null;
|
|
|
|
obs.next(error);
|
|
|
|
obs.complete();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('Validators', () => {
|
2017-04-06 11:41:10 -04:00
|
|
|
describe('min', () => {
|
|
|
|
it('should not error on an empty string',
|
|
|
|
() => { expect(Validators.min(2)(new FormControl(''))).toBeNull(); });
|
|
|
|
|
|
|
|
it('should not error on null',
|
|
|
|
() => { expect(Validators.min(2)(new FormControl(null))).toBeNull(); });
|
|
|
|
|
|
|
|
it('should not error on undefined',
|
|
|
|
() => { expect(Validators.min(2)(new FormControl(undefined))).toBeNull(); });
|
|
|
|
|
|
|
|
it('should error on non numbers', () => {
|
|
|
|
expect(Validators.min(2)(new FormControl('a'))).toEqual({'min': {'min': 2, 'actual': 'a'}});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should error on small values', () => {
|
|
|
|
expect(Validators.min(2)(new FormControl(1))).toEqual({'min': {'min': 2, 'actual': 1}});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not error on big values',
|
|
|
|
() => { expect(Validators.min(2)(new FormControl(3))).toBeNull(); });
|
|
|
|
|
|
|
|
it('should not error on equal values',
|
|
|
|
() => { expect(Validators.min(2)(new FormControl(2))).toBeNull(); });
|
|
|
|
|
|
|
|
it('should not error on equal values when value is string',
|
|
|
|
() => { expect(Validators.min(2)(new FormControl('2'))).toBeNull(); });
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('max', () => {
|
|
|
|
it('should not error on an empty string',
|
|
|
|
() => { expect(Validators.max(2)(new FormControl(''))).toBeNull(); });
|
|
|
|
|
|
|
|
it('should not error on null',
|
|
|
|
() => { expect(Validators.max(2)(new FormControl(null))).toBeNull(); });
|
|
|
|
|
|
|
|
it('should not error on undefined',
|
|
|
|
() => { expect(Validators.max(2)(new FormControl(undefined))).toBeNull(); });
|
|
|
|
|
|
|
|
it('should error on non numbers', () => {
|
|
|
|
expect(Validators.max(2)(new FormControl('aaa'))).toEqual({
|
|
|
|
'max': {'max': 2, 'actual': 'aaa'}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should error on big values', () => {
|
|
|
|
expect(Validators.max(2)(new FormControl(3))).toEqual({'max': {'max': 2, 'actual': 3}});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not error on small values',
|
|
|
|
() => { expect(Validators.max(2)(new FormControl(1))).toBeNull(); });
|
|
|
|
|
|
|
|
it('should not error on equal values',
|
|
|
|
() => { expect(Validators.max(2)(new FormControl(2))).toBeNull(); });
|
|
|
|
|
|
|
|
it('should not error on equal values when value is string',
|
|
|
|
() => { expect(Validators.max(2)(new FormControl('2'))).toBeNull(); });
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('required', () => {
|
|
|
|
it('should error on an empty string',
|
2016-06-10 14:15:59 -04:00
|
|
|
() => { expect(Validators.required(new FormControl(''))).toEqual({'required': true}); });
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should error on null',
|
2016-06-10 14:15:59 -04:00
|
|
|
() => { expect(Validators.required(new FormControl(null))).toEqual({'required': true}); });
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-12-30 07:28:38 -05:00
|
|
|
it('should not error on undefined', () => {
|
|
|
|
expect(Validators.required(new FormControl(undefined))).toEqual({'required': true});
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should not error on a non-empty string',
|
2016-10-10 12:17:45 -04:00
|
|
|
() => { expect(Validators.required(new FormControl('not empty'))).toBeNull(); });
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should accept zero as valid',
|
2016-10-10 12:17:45 -04:00
|
|
|
() => { expect(Validators.required(new FormControl(0))).toBeNull(); });
|
2017-01-05 12:25:20 -05:00
|
|
|
|
|
|
|
it('should error on an empty array',
|
|
|
|
() => expect(Validators.required(new FormControl([]))).toEqual({'required': true}));
|
|
|
|
|
|
|
|
it('should not error on a non-empty array',
|
|
|
|
() => expect(Validators.required(new FormControl([1, 2]))).toBeNull());
|
2016-06-08 18:36:24 -04:00
|
|
|
});
|
|
|
|
|
2016-12-10 05:44:04 -05:00
|
|
|
describe('requiredTrue', () => {
|
|
|
|
it('should error on false',
|
|
|
|
() => expect(Validators.requiredTrue(new FormControl(false))).toEqual({'required': true}));
|
|
|
|
|
|
|
|
it('should not error on true',
|
|
|
|
() => expect(Validators.requiredTrue(new FormControl(true))).toBeNull());
|
|
|
|
});
|
|
|
|
|
2016-12-29 12:07:02 -05:00
|
|
|
describe('email', () => {
|
|
|
|
it('should error on invalid email',
|
|
|
|
() => expect(Validators.email(new FormControl('some text'))).toEqual({'email': true}));
|
|
|
|
|
|
|
|
it('should not error on valid email',
|
|
|
|
() => expect(Validators.email(new FormControl('test@gmail.com'))).toBeNull());
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('minLength', () => {
|
2016-10-10 12:17:45 -04:00
|
|
|
it('should not error on an empty string',
|
|
|
|
() => { expect(Validators.minLength(2)(new FormControl(''))).toBeNull(); });
|
2016-10-06 18:12:09 -04:00
|
|
|
|
2016-10-10 12:17:45 -04:00
|
|
|
it('should not error on null',
|
|
|
|
() => { expect(Validators.minLength(2)(new FormControl(null))).toBeNull(); });
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-10-10 12:17:45 -04:00
|
|
|
it('should not error on undefined',
|
2016-12-30 07:28:38 -05:00
|
|
|
() => { expect(Validators.minLength(2)(new FormControl(undefined))).toBeNull(); });
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should not error on valid strings',
|
2016-10-10 12:17:45 -04:00
|
|
|
() => { expect(Validators.minLength(2)(new FormControl('aa'))).toBeNull(); });
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should error on short strings', () => {
|
2016-06-10 14:15:59 -04:00
|
|
|
expect(Validators.minLength(2)(new FormControl('a'))).toEqual({
|
2016-06-08 19:38:52 -04:00
|
|
|
'minlength': {'requiredLength': 2, 'actualLength': 1}
|
|
|
|
});
|
2016-06-08 18:36:24 -04:00
|
|
|
});
|
2016-12-12 14:17:12 -05:00
|
|
|
|
|
|
|
it('should not error when FormArray has valid length', () => {
|
|
|
|
const fa = new FormArray([new FormControl(''), new FormControl('')]);
|
|
|
|
expect(Validators.minLength(2)(fa)).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should error when FormArray has invalid length', () => {
|
|
|
|
const fa = new FormArray([new FormControl('')]);
|
|
|
|
expect(Validators.minLength(2)(fa)).toEqual({
|
|
|
|
'minlength': {'requiredLength': 2, 'actualLength': 1}
|
|
|
|
});
|
|
|
|
});
|
2016-06-08 18:36:24 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('maxLength', () => {
|
|
|
|
it('should not error on an empty string',
|
2016-10-10 12:17:45 -04:00
|
|
|
() => { expect(Validators.maxLength(2)(new FormControl(''))).toBeNull(); });
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should not error on null',
|
2016-10-10 12:17:45 -04:00
|
|
|
() => { expect(Validators.maxLength(2)(new FormControl(null))).toBeNull(); });
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-12-30 07:28:38 -05:00
|
|
|
it('should not error on undefined',
|
|
|
|
() => { expect(Validators.maxLength(2)(new FormControl(undefined))).toBeNull(); });
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should not error on valid strings',
|
2016-10-10 12:17:45 -04:00
|
|
|
() => { expect(Validators.maxLength(2)(new FormControl('aa'))).toBeNull(); });
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should error on long strings', () => {
|
2016-06-10 14:15:59 -04:00
|
|
|
expect(Validators.maxLength(2)(new FormControl('aaa'))).toEqual({
|
2016-06-08 19:38:52 -04:00
|
|
|
'maxlength': {'requiredLength': 2, 'actualLength': 3}
|
|
|
|
});
|
2016-06-08 18:36:24 -04:00
|
|
|
});
|
2016-12-12 14:17:12 -05:00
|
|
|
|
|
|
|
it('should not error when FormArray has valid length', () => {
|
|
|
|
const fa = new FormArray([new FormControl(''), new FormControl('')]);
|
|
|
|
expect(Validators.maxLength(2)(fa)).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should error when FormArray has invalid length', () => {
|
|
|
|
const fa = new FormArray([new FormControl(''), new FormControl('')]);
|
|
|
|
expect(Validators.maxLength(1)(fa)).toEqual({
|
|
|
|
'maxlength': {'requiredLength': 1, 'actualLength': 2}
|
|
|
|
});
|
|
|
|
});
|
2016-06-08 18:36:24 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('pattern', () => {
|
|
|
|
it('should not error on an empty string',
|
2016-10-10 12:17:45 -04:00
|
|
|
() => { expect(Validators.pattern('[a-zA-Z ]+')(new FormControl(''))).toBeNull(); });
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should not error on null',
|
2016-10-10 12:17:45 -04:00
|
|
|
() => { expect(Validators.pattern('[a-zA-Z ]+')(new FormControl(null))).toBeNull(); });
|
|
|
|
|
2016-12-30 07:28:38 -05:00
|
|
|
it('should not error on undefined', () => {
|
|
|
|
expect(Validators.pattern('[a-zA-Z ]+')(new FormControl(undefined))).toBeNull();
|
|
|
|
});
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-10-04 19:14:23 -04:00
|
|
|
it('should not error on null value and "null" pattern',
|
2016-10-10 12:17:45 -04:00
|
|
|
() => { expect(Validators.pattern('null')(new FormControl(null))).toBeNull(); });
|
2016-10-04 19:14:23 -04:00
|
|
|
|
2016-10-10 12:17:45 -04:00
|
|
|
it('should not error on valid strings',
|
2016-10-19 12:37:54 -04:00
|
|
|
() => expect(Validators.pattern('[a-zA-Z ]*')(new FormControl('aaAA'))).toBeNull());
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should error on failure to match string', () => {
|
2016-06-10 14:15:59 -04:00
|
|
|
expect(Validators.pattern('[a-zA-Z ]*')(new FormControl('aaa0'))).toEqual({
|
2016-06-08 19:38:52 -04:00
|
|
|
'pattern': {'requiredPattern': '^[a-zA-Z ]*$', 'actualValue': 'aaa0'}
|
|
|
|
});
|
2016-06-08 18:36:24 -04:00
|
|
|
});
|
2016-10-19 12:37:54 -04:00
|
|
|
|
|
|
|
it('should accept RegExp object', () => {
|
|
|
|
const pattern: RegExp = new RegExp('[a-zA-Z ]+');
|
|
|
|
expect(Validators.pattern(pattern)(new FormControl('aaAA'))).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should error on failure to match RegExp object', () => {
|
|
|
|
const pattern: RegExp = new RegExp('^[a-zA-Z ]*$');
|
|
|
|
expect(Validators.pattern(pattern)(new FormControl('aaa0'))).toEqual({
|
|
|
|
'pattern': {'requiredPattern': '/^[a-zA-Z ]*$/', 'actualValue': 'aaa0'}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not error on "null" pattern',
|
2017-04-17 14:13:30 -04:00
|
|
|
() => expect(Validators.pattern(null !)(new FormControl('aaAA'))).toBeNull());
|
2016-10-19 12:37:54 -04:00
|
|
|
|
|
|
|
it('should not error on "undefined" pattern',
|
2017-04-17 14:13:30 -04:00
|
|
|
() => expect(Validators.pattern(undefined !)(new FormControl('aaAA'))).toBeNull());
|
2016-06-08 18:36:24 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('compose', () => {
|
|
|
|
it('should return null when given null',
|
2017-04-17 14:13:30 -04:00
|
|
|
() => { expect(Validators.compose(null !)).toBe(null); });
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should collect errors from all the validators', () => {
|
2017-04-17 14:13:30 -04:00
|
|
|
const c = Validators.compose([validator('a', true), validator('b', true)]) !;
|
2016-06-10 14:15:59 -04:00
|
|
|
expect(c(new FormControl(''))).toEqual({'a': true, 'b': true});
|
2016-06-08 18:36:24 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should run validators left to right', () => {
|
2017-04-17 14:13:30 -04:00
|
|
|
const c = Validators.compose([validator('a', 1), validator('a', 2)]) !;
|
2016-06-10 14:15:59 -04:00
|
|
|
expect(c(new FormControl(''))).toEqual({'a': 2});
|
2016-06-08 18:36:24 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should return null when no errors', () => {
|
2017-04-17 14:13:30 -04:00
|
|
|
const c = Validators.compose([Validators.nullValidator, Validators.nullValidator]) !;
|
2016-10-10 12:17:45 -04:00
|
|
|
expect(c(new FormControl(''))).toBeNull();
|
2016-06-08 18:36:24 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should ignore nulls', () => {
|
2017-04-17 14:13:30 -04:00
|
|
|
const c = Validators.compose([null !, Validators.required]) !;
|
2016-06-10 14:15:59 -04:00
|
|
|
expect(c(new FormControl(''))).toEqual({'required': true});
|
2016-06-08 18:36:24 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('composeAsync', () => {
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2017-03-16 13:15:17 -04:00
|
|
|
describe('promises', () => {
|
|
|
|
function promiseValidator(response: {[key: string]: any}): AsyncValidatorFn {
|
|
|
|
return (c: AbstractControl) => {
|
|
|
|
const res = c.value != 'expected' ? response : null;
|
|
|
|
return Promise.resolve(res);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should return null when given null',
|
2017-04-17 14:13:30 -04:00
|
|
|
() => { expect(Validators.composeAsync(null !)).toBeNull(); });
|
2017-03-16 13:15:17 -04:00
|
|
|
|
|
|
|
it('should collect errors from all the validators', fakeAsync(() => {
|
|
|
|
const v = Validators.composeAsync(
|
2017-04-17 14:13:30 -04:00
|
|
|
[promiseValidator({'one': true}), promiseValidator({'two': true})]) !;
|
2017-03-16 13:15:17 -04:00
|
|
|
|
2017-04-17 14:13:30 -04:00
|
|
|
let errorMap: {[key: string]: any} = undefined !;
|
2017-03-16 13:15:17 -04:00
|
|
|
first.call(v(new FormControl('invalid')))
|
|
|
|
.subscribe((errors: {[key: string]: any}) => errorMap = errors);
|
|
|
|
tick();
|
|
|
|
|
|
|
|
expect(errorMap).toEqual({'one': true, 'two': true});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should normalize and evaluate async validator-directives correctly', fakeAsync(() => {
|
2017-04-17 14:13:30 -04:00
|
|
|
const v = Validators.composeAsync([normalizeAsyncValidator(
|
|
|
|
new AsyncValidatorDirective('expected', {'one': true}))]) !;
|
2017-03-16 13:15:17 -04:00
|
|
|
|
2017-04-17 14:13:30 -04:00
|
|
|
let errorMap: {[key: string]: any} = undefined !;
|
2017-03-16 13:15:17 -04:00
|
|
|
first.call(v(new FormControl('invalid')))
|
|
|
|
.subscribe((errors: {[key: string]: any}) => errorMap = errors);
|
|
|
|
tick();
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2017-03-16 13:15:17 -04:00
|
|
|
expect(errorMap).toEqual({'one': true});
|
|
|
|
}));
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2017-03-16 13:15:17 -04:00
|
|
|
it('should return null when no errors', fakeAsync(() => {
|
2017-04-17 14:13:30 -04:00
|
|
|
const v = Validators.composeAsync([promiseValidator({'one': true})]) !;
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2017-04-17 14:13:30 -04:00
|
|
|
let errorMap: {[key: string]: any} = undefined !;
|
2017-03-16 13:15:17 -04:00
|
|
|
first.call(v(new FormControl('expected')))
|
|
|
|
.subscribe((errors: {[key: string]: any}) => errorMap = errors);
|
|
|
|
tick();
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2017-03-16 13:15:17 -04:00
|
|
|
expect(errorMap).toBeNull();
|
|
|
|
}));
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2017-03-16 13:15:17 -04:00
|
|
|
it('should ignore nulls', fakeAsync(() => {
|
2017-04-17 14:13:30 -04:00
|
|
|
const v = Validators.composeAsync([promiseValidator({'one': true}), null !]) !;
|
2016-06-26 18:52:50 -04:00
|
|
|
|
2017-04-17 14:13:30 -04:00
|
|
|
let errorMap: {[key: string]: any} = undefined !;
|
2017-03-16 13:15:17 -04:00
|
|
|
first.call(v(new FormControl('invalid')))
|
|
|
|
.subscribe((errors: {[key: string]: any}) => errorMap = errors);
|
|
|
|
tick();
|
2016-06-26 18:52:50 -04:00
|
|
|
|
2017-03-16 13:15:17 -04:00
|
|
|
expect(errorMap).toEqual({'one': true});
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('observables', () => {
|
|
|
|
function observableValidator(response: {[key: string]: any}): AsyncValidatorFn {
|
|
|
|
return (c: AbstractControl) => {
|
|
|
|
const res = c.value != 'expected' ? response : null;
|
|
|
|
return of (res);
|
|
|
|
};
|
|
|
|
}
|
2016-06-26 18:52:50 -04:00
|
|
|
|
2017-03-16 13:15:17 -04:00
|
|
|
it('should return null when given null',
|
2017-04-17 14:13:30 -04:00
|
|
|
() => { expect(Validators.composeAsync(null !)).toBeNull(); });
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2017-03-16 13:15:17 -04:00
|
|
|
it('should collect errors from all the validators', () => {
|
|
|
|
const v = Validators.composeAsync(
|
2017-04-17 14:13:30 -04:00
|
|
|
[observableValidator({'one': true}), observableValidator({'two': true})]) !;
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2017-04-17 14:13:30 -04:00
|
|
|
let errorMap: {[key: string]: any} = undefined !;
|
2017-03-16 13:15:17 -04:00
|
|
|
first.call(v(new FormControl('invalid')))
|
|
|
|
.subscribe((errors: {[key: string]: any}) => errorMap = errors);
|
|
|
|
|
|
|
|
expect(errorMap).toEqual({'one': true, 'two': true});
|
|
|
|
});
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2017-03-16 13:15:17 -04:00
|
|
|
it('should normalize and evaluate async validator-directives correctly', () => {
|
|
|
|
const v = Validators.composeAsync(
|
2017-04-17 14:13:30 -04:00
|
|
|
[normalizeAsyncValidator(new AsyncValidatorDirective('expected', {'one': true}))]) !;
|
2017-03-16 13:15:17 -04:00
|
|
|
|
2017-04-17 14:13:30 -04:00
|
|
|
let errorMap: {[key: string]: any} = undefined !;
|
2017-03-16 13:15:17 -04:00
|
|
|
first.call(v(new FormControl('invalid')))
|
2017-04-17 14:13:30 -04:00
|
|
|
.subscribe((errors: {[key: string]: any}) => errorMap = errors) !;
|
2017-03-16 13:15:17 -04:00
|
|
|
|
|
|
|
expect(errorMap).toEqual({'one': true});
|
|
|
|
});
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2017-03-16 13:15:17 -04:00
|
|
|
it('should return null when no errors', () => {
|
2017-04-17 14:13:30 -04:00
|
|
|
const v = Validators.composeAsync([observableValidator({'one': true})]) !;
|
2016-06-08 18:36:24 -04:00
|
|
|
|
2017-04-17 14:13:30 -04:00
|
|
|
let errorMap: {[key: string]: any} = undefined !;
|
2017-03-16 13:15:17 -04:00
|
|
|
first.call(v(new FormControl('expected')))
|
|
|
|
.subscribe((errors: {[key: string]: any}) => errorMap = errors);
|
|
|
|
|
|
|
|
expect(errorMap).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should ignore nulls', () => {
|
2017-04-17 14:13:30 -04:00
|
|
|
const v = Validators.composeAsync([observableValidator({'one': true}), null !]) !;
|
2017-03-16 13:15:17 -04:00
|
|
|
|
2017-04-17 14:13:30 -04:00
|
|
|
let errorMap: {[key: string]: any} = undefined !;
|
2017-03-16 13:15:17 -04:00
|
|
|
first.call(v(new FormControl('invalid')))
|
|
|
|
.subscribe((errors: {[key: string]: any}) => errorMap = errors);
|
|
|
|
|
|
|
|
expect(errorMap).toEqual({'one': true});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should wait for all validators before setting errors', fakeAsync(() => {
|
|
|
|
function getTimerObs(time: number, errorMap: {[key: string]: any}): AsyncValidatorFn {
|
|
|
|
return (c: AbstractControl) => { return map.call(timer(time), () => errorMap); };
|
|
|
|
}
|
|
|
|
|
|
|
|
const v = Validators.composeAsync(
|
2017-04-17 14:13:30 -04:00
|
|
|
[getTimerObs(100, {one: true}), getTimerObs(200, {two: true})]) !;
|
2017-03-16 13:15:17 -04:00
|
|
|
|
2017-04-17 14:13:30 -04:00
|
|
|
let errorMap: {[key: string]: any} = undefined !;
|
2017-03-16 13:15:17 -04:00
|
|
|
first.call(v(new FormControl('invalid')))
|
|
|
|
.subscribe((errors: {[key: string]: any}) => errorMap = errors);
|
|
|
|
|
|
|
|
tick(100);
|
|
|
|
expect(errorMap).not.toBeDefined(
|
|
|
|
`Expected errors not to be set until all validators came back.`);
|
|
|
|
|
|
|
|
tick(100);
|
|
|
|
expect(errorMap).toEqual(
|
|
|
|
{one: true, two: true}, `Expected errors to merge once all validators resolved.`);
|
|
|
|
}));
|
|
|
|
|
|
|
|
});
|
2016-06-08 18:36:24 -04:00
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|