angular-cn/modules/@angular/common/test/forms/validators_spec.ts

165 lines
6.1 KiB
TypeScript
Raw Normal View History

import {AbstractControl, Control, ControlArray, ControlGroup, Validators} from '@angular/common';
import {Log, fakeAsync, flushMicrotasks, tick} from '@angular/core/testing';
import {afterEach, beforeEach, ddescribe, describe, expect, iit, it, xit} from '@angular/core/testing/testing_internal';
2016-06-08 18:36:24 -04:00
import {EventEmitter, ObservableWrapper, TimerWrapper} from '../../src/facade/async';
import {PromiseWrapper} from '../../src/facade/promise';
2016-06-08 18:36:24 -04:00
export function main() {
function validator(key: string, error: any) {
return function(c: AbstractControl) {
var r = {};
2016-06-08 20:08:59 -04:00
(r as any /** TODO #9100 */)[key] = error;
2016-06-08 18:36:24 -04:00
return r;
}
}
describe('Validators', () => {
describe('required', () => {
it('should error on an empty string',
() => { expect(Validators.required(new Control(''))).toEqual({'required': true}); });
2016-06-08 18:36:24 -04:00
it('should error on null',
() => { expect(Validators.required(new Control(null))).toEqual({'required': true}); });
2016-06-08 18:36:24 -04:00
it('should not error on a non-empty string',
() => { expect(Validators.required(new Control('not empty'))).toEqual(null); });
2016-06-08 18:36:24 -04:00
it('should accept zero as valid',
2016-06-08 18:36:24 -04:00
() => { expect(Validators.required(new Control(0))).toEqual(null); });
});
describe('minLength', () => {
it('should not error on an empty string',
() => { expect(Validators.minLength(2)(new Control(''))).toEqual(null); });
2016-06-08 18:36:24 -04:00
it('should not error on null',
2016-06-08 18:36:24 -04:00
() => { expect(Validators.minLength(2)(new Control(null))).toEqual(null); });
it('should not error on valid strings',
() => { expect(Validators.minLength(2)(new Control('aa'))).toEqual(null); });
2016-06-08 18:36:24 -04:00
it('should error on short strings', () => {
expect(Validators.minLength(2)(new Control('a'))).toEqual({
'minlength': {'requiredLength': 2, 'actualLength': 1}
});
2016-06-08 18:36:24 -04:00
});
});
describe('maxLength', () => {
it('should not error on an empty string',
() => { expect(Validators.maxLength(2)(new Control(''))).toEqual(null); });
2016-06-08 18:36:24 -04:00
it('should not error on null',
2016-06-08 18:36:24 -04:00
() => { expect(Validators.maxLength(2)(new Control(null))).toEqual(null); });
it('should not error on valid strings',
() => { expect(Validators.maxLength(2)(new Control('aa'))).toEqual(null); });
2016-06-08 18:36:24 -04:00
it('should error on long strings', () => {
expect(Validators.maxLength(2)(new Control('aaa'))).toEqual({
'maxlength': {'requiredLength': 2, 'actualLength': 3}
});
2016-06-08 18:36:24 -04:00
});
});
describe('pattern', () => {
it('should not error on an empty string',
() => { expect(Validators.pattern('[a-zA-Z ]*')(new Control(''))).toEqual(null); });
2016-06-08 18:36:24 -04:00
it('should not error on null',
() => { expect(Validators.pattern('[a-zA-Z ]*')(new Control(null))).toEqual(null); });
2016-06-08 18:36:24 -04:00
it('should not error on valid strings',
() => { expect(Validators.pattern('[a-zA-Z ]*')(new Control('aaAA'))).toEqual(null); });
2016-06-08 18:36:24 -04:00
it('should error on failure to match string', () => {
expect(Validators.pattern('[a-zA-Z ]*')(new Control('aaa0'))).toEqual({
'pattern': {'requiredPattern': '^[a-zA-Z ]*$', 'actualValue': 'aaa0'}
});
2016-06-08 18:36:24 -04:00
});
});
describe('compose', () => {
it('should return null when given null',
2016-06-08 18:36:24 -04:00
() => { expect(Validators.compose(null)).toBe(null); });
it('should collect errors from all the validators', () => {
var c = Validators.compose([validator('a', true), validator('b', true)]);
expect(c(new Control(''))).toEqual({'a': true, 'b': true});
2016-06-08 18:36:24 -04:00
});
it('should run validators left to right', () => {
var c = Validators.compose([validator('a', 1), validator('a', 2)]);
expect(c(new Control(''))).toEqual({'a': 2});
2016-06-08 18:36:24 -04:00
});
it('should return null when no errors', () => {
2016-06-08 18:36:24 -04:00
var c = Validators.compose([Validators.nullValidator, Validators.nullValidator]);
expect(c(new Control(''))).toEqual(null);
2016-06-08 18:36:24 -04:00
});
it('should ignore nulls', () => {
2016-06-08 18:36:24 -04:00
var c = Validators.compose([null, Validators.required]);
expect(c(new Control(''))).toEqual({'required': true});
2016-06-08 18:36:24 -04:00
});
});
describe('composeAsync', () => {
2016-06-08 20:08:59 -04:00
function asyncValidator(expected: any /** TODO #9100 */, response: any /** TODO #9100 */) {
return (c: any /** TODO #9100 */) => {
2016-06-08 18:36:24 -04:00
var emitter = new EventEmitter();
var res = c.value != expected ? response : null;
PromiseWrapper.scheduleMicrotask(() => {
ObservableWrapper.callEmit(emitter, res);
// this is required because of a bug in ObservableWrapper
// where callComplete can fire before callEmit
// remove this one the bug is fixed
TimerWrapper.setTimeout(() => { ObservableWrapper.callComplete(emitter); }, 0);
});
return emitter;
};
}
it('should return null when given null',
2016-06-08 18:36:24 -04:00
() => { expect(Validators.composeAsync(null)).toEqual(null); });
it('should collect errors from all the validators', fakeAsync(() => {
2016-06-08 18:36:24 -04:00
var c = Validators.composeAsync([
asyncValidator('expected', {'one': true}), asyncValidator('expected', {'two': true})
2016-06-08 18:36:24 -04:00
]);
2016-06-08 20:08:59 -04:00
var value: any /** TODO #9100 */ = null;
(<Promise<any>>c(new Control('invalid'))).then(v => value = v);
2016-06-08 18:36:24 -04:00
tick(1);
expect(value).toEqual({'one': true, 'two': true});
2016-06-08 18:36:24 -04:00
}));
it('should return null when no errors', fakeAsync(() => {
var c = Validators.composeAsync([asyncValidator('expected', {'one': true})]);
2016-06-08 18:36:24 -04:00
2016-06-08 20:08:59 -04:00
var value: any /** TODO #9100 */ = null;
(<Promise<any>>c(new Control('expected'))).then(v => value = v);
2016-06-08 18:36:24 -04:00
tick(1);
expect(value).toEqual(null);
}));
it('should ignore nulls', fakeAsync(() => {
var c = Validators.composeAsync([asyncValidator('expected', {'one': true}), null]);
2016-06-08 18:36:24 -04:00
2016-06-08 20:08:59 -04:00
var value: any /** TODO #9100 */ = null;
(<Promise<any>>c(new Control('invalid'))).then(v => value = v);
2016-06-08 18:36:24 -04:00
tick(1);
expect(value).toEqual({'one': true});
2016-06-08 18:36:24 -04:00
}));
});
});
}