2016-04-07 20:17:50 -04:00
|
|
|
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach, fakeAsync, tick, el} from 'angular2/testing_internal';
|
2015-11-18 18:55:43 -05:00
|
|
|
import {ControlGroup, Control, Validators, AbstractControl, ControlArray} from 'angular2/common';
|
2015-11-06 20:34:07 -05:00
|
|
|
import {PromiseWrapper} from 'angular2/src/facade/promise';
|
|
|
|
import {EventEmitter, ObservableWrapper, TimerWrapper} from 'angular2/src/facade/async';
|
|
|
|
import {CONST_EXPR} from 'angular2/src/facade/lang';
|
2015-02-11 14:10:31 -05:00
|
|
|
|
|
|
|
export function main() {
|
2015-05-22 15:32:49 -04:00
|
|
|
function validator(key: string, error: any) {
|
feat(validators): Allow errors at both the group/array level or their children
Allow ControlGroups and ControlArrays to contain errors from their level, and
errors from their children. [Design Doc](https://docs.google.com/document/d/1EnJ3-_iFpVKFz1ifN1LkXSGQ7h3A72OQGry2g8eo7IA/edit?pli=1#heading=h.j53rt81eegm4)
BREAKING CHANGE: errors format has changed from validators. Now errors from
a control or an array's children are prefixed with 'controls' while errors
from the object itself are left at the root level.
Example:
Given a Control group as follows:
var group = new ControlGroup({
login: new Control("", required),
password: new Control("", required),
passwordConfirm: new Control("", required)
});
Before:
group.errors
{
login: {required: true},
password: {required: true},
passwordConfirm: {required: true},
}
After:
group.errors
{
controls: {
login: {required: true},
password: {required: true},
passwordConfirm: {required: true},
}
}
2015-10-16 19:13:14 -04:00
|
|
|
return function(c: AbstractControl) {
|
2015-02-11 14:10:31 -05:00
|
|
|
var r = {};
|
|
|
|
r[key] = error;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
describe('Validators', () => {
|
|
|
|
describe('required', () => {
|
|
|
|
it('should error on an empty string',
|
|
|
|
() => { expect(Validators.required(new Control(''))).toEqual({'required': true}); });
|
2015-02-11 14:10:31 -05:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
it('should error on null',
|
|
|
|
() => { expect(Validators.required(new Control(null))).toEqual({'required': true}); });
|
2015-02-11 14:10:31 -05:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
it('should not error on a non-empty string',
|
|
|
|
() => { expect(Validators.required(new Control('not empty'))).toEqual(null); });
|
2016-01-21 08:25:31 -05:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
it('should accept zero as valid',
|
2016-01-21 08:25:31 -05:00
|
|
|
() => { expect(Validators.required(new Control(0))).toEqual(null); });
|
2015-02-11 14:10:31 -05:00
|
|
|
});
|
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
describe('minLength', () => {
|
|
|
|
it('should not error on an empty string',
|
|
|
|
() => { expect(Validators.minLength(2)(new Control(''))).toEqual(null); });
|
2015-10-13 17:38:13 -04:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
it('should not error on null',
|
2015-10-13 17:38:13 -04:00
|
|
|
() => { expect(Validators.minLength(2)(new Control(null))).toEqual(null); });
|
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
it('should not error on valid strings',
|
|
|
|
() => { expect(Validators.minLength(2)(new Control('aa'))).toEqual(null); });
|
2015-10-13 17:38:13 -04:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
it('should error on short strings', () => {
|
|
|
|
expect(Validators.minLength(2)(new Control('a'))).toEqual({
|
|
|
|
'minlength': {'requiredLength': 2, 'actualLength': 1}
|
|
|
|
});
|
2015-10-13 17:38:13 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
describe('maxLength', () => {
|
|
|
|
it('should not error on an empty string',
|
|
|
|
() => { expect(Validators.maxLength(2)(new Control(''))).toEqual(null); });
|
2015-10-13 17:38:13 -04:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
it('should not error on null',
|
2015-10-13 17:38:13 -04:00
|
|
|
() => { expect(Validators.maxLength(2)(new Control(null))).toEqual(null); });
|
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
it('should not error on valid strings',
|
|
|
|
() => { expect(Validators.maxLength(2)(new Control('aa'))).toEqual(null); });
|
2015-10-13 17:38:13 -04:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
it('should error on long strings', () => {
|
|
|
|
expect(Validators.maxLength(2)(new Control('aaa'))).toEqual({
|
|
|
|
'maxlength': {'requiredLength': 2, 'actualLength': 3}
|
|
|
|
});
|
2015-10-13 17:38:13 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
describe('pattern', () => {
|
|
|
|
it('should not error on an empty string',
|
|
|
|
() => { expect(Validators.pattern('[a-zA-Z ]*')(new Control(''))).toEqual(null); });
|
2015-11-20 01:36:28 -05:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
it('should not error on null',
|
|
|
|
() => { expect(Validators.pattern('[a-zA-Z ]*')(new Control(null))).toEqual(null); });
|
2015-11-20 01:36:28 -05:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
it('should not error on valid strings',
|
|
|
|
() => { expect(Validators.pattern('[a-zA-Z ]*')(new Control('aaAA'))).toEqual(null); });
|
2015-11-20 01:36:28 -05:00
|
|
|
|
2016-04-07 20:17:50 -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'}
|
|
|
|
});
|
2015-11-20 01:36:28 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
describe('compose', () => {
|
|
|
|
it('should return null when given null',
|
2015-11-02 12:59:40 -05:00
|
|
|
() => { expect(Validators.compose(null)).toBe(null); });
|
2015-09-02 13:24:22 -04:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
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});
|
2015-02-11 14:10:31 -05:00
|
|
|
});
|
|
|
|
|
2016-04-07 20:17:50 -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});
|
2015-02-11 14:10:31 -05:00
|
|
|
});
|
2015-02-24 14:59:10 -05:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
it('should return null when no errors', () => {
|
2015-03-19 17:21:40 -04:00
|
|
|
var c = Validators.compose([Validators.nullValidator, Validators.nullValidator]);
|
2016-04-07 20:17:50 -04:00
|
|
|
expect(c(new Control(''))).toEqual(null);
|
2015-02-24 14:59:10 -05:00
|
|
|
});
|
2015-10-29 20:45:24 -04:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
it('should ignore nulls', () => {
|
2015-10-29 20:45:24 -04:00
|
|
|
var c = Validators.compose([null, Validators.required]);
|
2016-04-07 20:17:50 -04:00
|
|
|
expect(c(new Control(''))).toEqual({'required': true});
|
2015-10-29 20:45:24 -04:00
|
|
|
});
|
2015-02-11 14:10:31 -05:00
|
|
|
});
|
2015-11-02 12:59:40 -05:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
describe('composeAsync', () => {
|
2015-11-05 20:18:16 -05:00
|
|
|
function asyncValidator(expected, response) {
|
2015-11-02 12:59:40 -05:00
|
|
|
return (c) => {
|
2015-11-05 20:18:16 -05:00
|
|
|
var emitter = new EventEmitter();
|
2015-11-02 12:59:40 -05:00
|
|
|
var res = c.value != expected ? response : null;
|
2015-11-05 20:18:16 -05:00
|
|
|
|
|
|
|
PromiseWrapper.scheduleMicrotask(() => {
|
2015-11-16 02:58:59 -05:00
|
|
|
ObservableWrapper.callEmit(emitter, res);
|
2015-11-05 20:18:16 -05:00
|
|
|
// this is required because of a bug in ObservableWrapper
|
2015-11-16 02:58:59 -05:00
|
|
|
// where callComplete can fire before callEmit
|
2015-11-05 20:18:16 -05:00
|
|
|
// remove this one the bug is fixed
|
|
|
|
TimerWrapper.setTimeout(() => { ObservableWrapper.callComplete(emitter); }, 0);
|
|
|
|
});
|
|
|
|
return emitter;
|
2015-11-02 12:59:40 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
it('should return null when given null',
|
2015-11-02 12:59:40 -05:00
|
|
|
() => { expect(Validators.composeAsync(null)).toEqual(null); });
|
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
it('should collect errors from all the validators', fakeAsync(() => {
|
2015-11-02 12:59:40 -05:00
|
|
|
var c = Validators.composeAsync([
|
2016-04-07 20:17:50 -04:00
|
|
|
asyncValidator('expected', {'one': true}), asyncValidator('expected', {'two': true})
|
2015-11-02 12:59:40 -05:00
|
|
|
]);
|
|
|
|
|
|
|
|
var value = null;
|
2016-04-07 20:17:50 -04:00
|
|
|
(<Promise<any>>c(new Control('invalid'))).then(v => value = v);
|
2015-11-02 12:59:40 -05:00
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
expect(value).toEqual({'one': true, 'two': true});
|
2015-11-02 12:59:40 -05:00
|
|
|
}));
|
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
it('should return null when no errors', fakeAsync(() => {
|
|
|
|
var c = Validators.composeAsync([asyncValidator('expected', {'one': true})]);
|
2015-11-02 12:59:40 -05:00
|
|
|
|
|
|
|
var value = null;
|
2016-04-07 20:17:50 -04:00
|
|
|
(<Promise<any>>c(new Control('expected'))).then(v => value = v);
|
2015-11-02 12:59:40 -05:00
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
|
|
|
expect(value).toEqual(null);
|
|
|
|
}));
|
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
it('should ignore nulls', fakeAsync(() => {
|
|
|
|
var c = Validators.composeAsync([asyncValidator('expected', {'one': true}), null]);
|
2015-11-02 12:59:40 -05:00
|
|
|
|
|
|
|
var value = null;
|
2016-04-07 20:17:50 -04:00
|
|
|
(<Promise<any>>c(new Control('invalid'))).then(v => value = v);
|
2015-11-02 12:59:40 -05:00
|
|
|
|
|
|
|
tick(1);
|
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
expect(value).toEqual({'one': true});
|
2015-11-02 12:59:40 -05:00
|
|
|
}));
|
|
|
|
});
|
2015-02-11 14:10:31 -05:00
|
|
|
});
|
feat(validators): Allow errors at both the group/array level or their children
Allow ControlGroups and ControlArrays to contain errors from their level, and
errors from their children. [Design Doc](https://docs.google.com/document/d/1EnJ3-_iFpVKFz1ifN1LkXSGQ7h3A72OQGry2g8eo7IA/edit?pli=1#heading=h.j53rt81eegm4)
BREAKING CHANGE: errors format has changed from validators. Now errors from
a control or an array's children are prefixed with 'controls' while errors
from the object itself are left at the root level.
Example:
Given a Control group as follows:
var group = new ControlGroup({
login: new Control("", required),
password: new Control("", required),
passwordConfirm: new Control("", required)
});
Before:
group.errors
{
login: {required: true},
password: {required: true},
passwordConfirm: {required: true},
}
After:
group.errors
{
controls: {
login: {required: true},
password: {required: true},
passwordConfirm: {required: true},
}
}
2015-10-16 19:13:14 -04:00
|
|
|
}
|