Browse Source

Tests fixed

pull/65/head
Sebastian Stehle 9 years ago
parent
commit
1f2d097397
  1. 2
      src/Squidex/app/features/administration/pages/users/user-page.component.ts
  2. 2
      src/Squidex/app/framework/angular/control-errors.component.ts
  3. 82
      src/Squidex/app/framework/angular/validators.spec.ts
  4. 20
      src/Squidex/app/framework/angular/validators.ts
  5. 2
      src/Squidex/app/framework/services/panel.service.spec.ts

2
src/Squidex/app/features/administration/pages/users/user-page.component.ts

@ -137,7 +137,7 @@ export class UserPageComponent extends ComponentBase implements OnInit {
this.userForm.addControl('password', new FormControl('', Validators.required));
}
this.userForm.addControl('passwordConfirm', new FormControl('', ValidatorsEx.matchOther('password', 'Passwords must be the same.')));
this.userForm.addControl('passwordConfirm', new FormControl('', ValidatorsEx.match('password', 'Passwords must be the same.')));
this.isCurrentUser = this.userId === this.currentUserId;
}

2
src/Squidex/app/framework/angular/control-errors.component.ts

@ -18,7 +18,7 @@ const DEFAULT_ERRORS: { [key: string]: string } = {
maxvalue: '{field} must be smaller than {maxValue}.',
minlength: '{field} must have more than {requiredLength} characters.',
maxlength: '{field} cannot have more than {requiredLength} characters.',
matchOther: '{message}',
match: '{message}',
validdatetime: '{field} is not a valid date time',
validnumber: '{field} is not a valid number.',
validvalues: '{field} is not a valid value.'

82
src/Squidex/app/framework/angular/validators.spec.ts

@ -5,7 +5,7 @@
* Copyright (c) Sebastian Stehle. All rights reserved
*/
import { FormControl, Validators } from '@angular/forms';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { ValidatorsEx } from './../';
@ -16,12 +16,12 @@ describe('ValidatorsEx.between', () => {
expect(validator).toBe(Validators.nullValidator);
});
it('should return empty value when value is valid', () => {
it('should return null when value is valid', () => {
const input = new FormControl(4);
const error = <any>ValidatorsEx.between(1, 5)(input);
expect(error).toEqual({});
expect(error).toBeNull();
});
it('should return error when not a number', () => {
@ -56,12 +56,12 @@ describe('ValidatorsEx.validValues', () => {
expect(validator).toBe(Validators.nullValidator);
});
it('should return empty value if value is in allowed values', () => {
it('should return null if value is in allowed values', () => {
const input = new FormControl(10);
const error = ValidatorsEx.validValues([10, 20, 30])(input);
expect(error).toEqual({});
expect(error).toBeNull();
});
it('should return error if value is not in allowed values', () => {
@ -73,6 +73,66 @@ describe('ValidatorsEx.validValues', () => {
});
});
describe('ValidatorsEx.match', () => {
it('should revalidate if other control changes', () => {
const validator = ValidatorsEx.match('password', 'Passwords are not the same.');
const form = new FormGroup({
password: new FormControl('1'),
passwordConfirm: new FormControl('2', validator)
});
form.controls['passwordConfirm'].setValue('1');
expect(form.valid).toBeTruthy();
form.controls['password'].setValue('2');
expect(form.controls['password'].valid).toBeTruthy();
expect(form.controls['passwordConfirm'].valid).toBeFalsy();
});
it('should return error if not the same value', () => {
const validator = ValidatorsEx.match('password', 'Passwords are not the same.');
const form = new FormGroup({
password: new FormControl('1'),
passwordConfirm: new FormControl('2', validator)
});
expect(validator(form.controls['passwordConfirm'])).toEqual({ match: { message: 'Passwords are not the same.' }});
});
it('should return empty object if values are the same', () => {
const validator = ValidatorsEx.match('password', 'Passwords are not the same.');
const form = new FormGroup({
password: new FormControl('1'),
passwordConfirm: new FormControl('1', validator)
});
expect(validator(form.controls['passwordConfirm'])).toBeNull();
});
it('should throw error if other object is not found', () => {
const validator = ValidatorsEx.match('password', 'Passwords are not the same.');
const form = new FormGroup({
passwordConfirm: new FormControl('2', validator)
});
expect(() => validator(form.controls['passwordConfirm'])).toThrow();
});
it('should return empty object if control has no parent', () => {
const validator = ValidatorsEx.match('password', 'Passwords are not the same.');
const control = new FormControl('2', validator);
expect(validator(control)).toBeNull();
});
});
describe('ValidatorsEx.pattern', () => {
it('should return null validator if pattern not defined', () => {
const validator = ValidatorsEx.pattern(undefined!, undefined);
@ -80,28 +140,28 @@ describe('ValidatorsEx.pattern', () => {
expect(validator).toBe(Validators.nullValidator);
});
it('should return empty value when value is valid pattern', () => {
it('should return null when value is valid pattern', () => {
const input = new FormControl('1234');
const error = ValidatorsEx.pattern(/^[0-9]{1,4}$/)(input);
expect(error).toEqual({});
expect(error).toBeNull();
});
it('should return empty value when value is null string', () => {
it('should return null when value is null string', () => {
const input = new FormControl(null);
const error = ValidatorsEx.pattern(/^[0-9]{1,4}$/)(input);
expect(error).toEqual({});
expect(error).toBeNull();
});
it('should return empty value when value is empty string', () => {
it('should return null when value is empty string', () => {
const input = new FormControl('');
const error = ValidatorsEx.pattern(/^[0-9]{1,4}$/)(input);
expect(error).toEqual({});
expect(error).toBeNull();
});
it('should return error with message if value does not match pattern string', () => {

20
src/Squidex/app/framework/angular/validators.ts

@ -34,7 +34,7 @@ export module ValidatorsEx {
const n: string = control.value;
if (n == null || n.length === 0) {
return {};
return null;
}
if (!regex.test(n)) {
@ -45,23 +45,23 @@ export module ValidatorsEx {
}
}
return {};
return null;
};
}
export function matchOther(otherControlName: string, message: string) {
export function match(otherControlName: string, message: string) {
let otherControl: AbstractControl = null;
return (control: AbstractControl): { [key: string]: any } => {
if (!control.parent) {
return {};
return null;
}
if (otherControl === null) {
otherControl = control.parent.get(otherControlName) || undefined;
if (!otherControl) {
throw new Error('matchOtherValidator(): other control is not found in parent group');
throw new Error('matchValidator(): other control is not found in parent group');
}
otherControl.valueChanges.subscribe(() => {
@ -70,10 +70,10 @@ export module ValidatorsEx {
}
if (otherControl && otherControl.value !== control.value) {
return { matchOther: { message } };
return { match: { message } };
}
return {};
return null;
};
}
@ -89,7 +89,7 @@ export module ValidatorsEx {
}
}
return {};
return null;
};
}
@ -109,7 +109,7 @@ export module ValidatorsEx {
return { maxvalue: { maxValue, actualValue: n } };
}
return {};
return null;
};
}
@ -125,7 +125,7 @@ export module ValidatorsEx {
return { validvalues: false };
}
return {};
return null;
};
}
}

2
src/Squidex/app/framework/services/panel.service.spec.ts

@ -79,6 +79,6 @@ describe('PanelService', () => {
{ element: element2, property: 'z-index', value: '10' }
]);
expect(numPublished).toBe(4);
expect(numPublished).toBe(1);
});
});
Loading…
Cancel
Save