feat(forms): add equalsTo validator (#14052)

PR Close: #14052
This commit is contained in:
Toxicable
2017-01-22 16:13:47 +13:00
committed by Miško Hevery
parent 94b62c963d
commit 7b7ae5fe56
3 changed files with 65 additions and 2 deletions

View File

@ -8,7 +8,7 @@
import {fakeAsync, tick} from '@angular/core/testing';
import {describe, expect, it} from '@angular/core/testing/testing_internal';
import {AbstractControl, FormArray, FormControl, Validators} from '@angular/forms';
import {AbstractControl, FormArray, FormControl, FormGroup, Validators} from '@angular/forms';
import {Observable} from 'rxjs/Observable';
import {normalizeAsyncValidator} from '../src/directives/normalize_validator';
@ -36,6 +36,44 @@ export function main() {
}
describe('Validators', () => {
describe('equalsTo', () => {
it('should not error when equal', () => {
let group = new FormGroup({f1: new FormControl('a'), f2: new FormControl('a')});
let validator = Validators.equalsTo('f2');
expect(validator(group.controls['f1'])).toBeNull();
});
it('should error when not equal', () => {
let group = new FormGroup({f1: new FormControl('a'), f2: new FormControl('b')});
let validator = Validators.equalsTo('f2');
expect(validator(group.controls['f1'])).toEqual({equalsTo: {unequalField: 'f2'}});
});
it('should throw if passed a form control', () => {
let validator = Validators.equalsTo('f1', 'f2');
// cast it to any so we don't get TS errors
expect(() => validator(<any>new FormGroup({f1: new FormControl('')}))).toThrow();
});
it('should throw if passed a form array', () => {
let validator = Validators.equalsTo('f1', 'f2');
// cast it to any so we don't get TS errors
expect(() => validator(<any>new FormArray([]))).toThrow();
});
it('should throw if not passed any field to compare', () => {
let validator = Validators.equalsTo();
expect(() => validator(new FormControl('a'))).toThrow();
});
it('should throw if field passed does not exist in the group', () => {
let group = new FormGroup({f1: new FormControl('a'), f2: new FormControl('b')});
let validator = Validators.equalsTo('f3', 'f4');
// cast it to any so we don't get TS errors
expect(() => validator(new FormControl('a'))).toThrow();
});
});
describe('required', () => {
it('should error on an empty string',
() => { expect(Validators.required(new FormControl(''))).toEqual({'required': true}); });