unit tests for valudation > validator inclusion, exclusion and format

This commit is contained in:
Carlos Jimenez 2018-01-02 14:49:11 +01:00
parent b172f12ff6
commit 67d99e788e
2 changed files with 37 additions and 31 deletions

View File

@ -145,36 +145,42 @@ describe('Directive validation', () => {
});
});
// it(`should not validate the name as it's format is incorrect`, () => {
// let html = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
// let validations = {User: {validations: {name: [{validation: 'presence'}]}}};
// compile(html, validations, 'spiderman');
// scope.user.name = '';
// scope.$digest();
describe('Validator inclusion()', () => {
it('should not validate the phone number as it is not an integer', () => {
let html = `<form><input type="text" ng-model="user.phone" vn-validation="user.phone"/></form>`;
let validations = {User: {validations: {phone: [{validation: 'inclusion'}]}}};
compile(html, validations, 'spiderman');
scope.user.phone = 'this is not a phone number!';
scope.$digest();
// expect(element[0].classList).toContain('ng-invalid');
// expect(element[0].classList).not.toContain('ng-valid');
// });
// it(`should validate the email as it's format is correct`, () => {
// let html = `<form><input type="email" ng-model="user.email" vn-validation="user.email"/></form>`;
// let validations = {User: {validations: {email: [{validation: 'presence'}]}}};
// compile(html, validations, 'spiderman');
// scope.user.email = 'user@verdnatura.es';
// scope.$digest();
// expect(element[0].classList).toContain('ng-valid');
// expect(element[0].classList).not.toContain('ng-invalid');
// });
// it(`should not validate the email as it's missing the @`, () => {
// let html = `<form><input type="email" ng-model="user.email" vn-validation="user.email"/></form>`;
// let validations = {User: {validations: {email: [{validation: 'presence'}]}}};
// compile(html, validations, 'spiderman');
// scope.user.email = 'userverdnatura.es';
// scope.$digest();
// expect(element[0].classList).toContain('ng-invalid');
// expect(element[0].classList).not.toContain('ng-valid');
// });
expect(element[0].classList).toContain('ng-invalid');
expect(element[0].classList).not.toContain('ng-valid');
});
});
describe('Validator exclusion()', () => {
it('should validate the phone number as it is an integer', () => {
let html = `<form><input type="text" ng-model="user.phone" vn-validation="user.phone"/></form>`;
let validations = {User: {validations: {phone: [{validation: 'exclusion'}]}}};
compile(html, validations, 'spiderman');
scope.user.phone = '555555555';
scope.$digest();
expect(element[0].classList).toContain('ng-valid');
expect(element[0].classList).not.toContain('ng-invalid');
});
});
describe('Validator format()', () => {
it('should not validate the email number as it doesnt contain @', () => {
let html = `<form><input type="text" ng-model="user.email" vn-validation="user.email"/></form>`;
let validations = {User: {validations: {email: [{validation: 'format', with: '@'}]}}};
compile(html, validations, 'spiderman');
scope.user.email = 'userverdnatura.es';
scope.$digest();
expect(element[0].classList).toContain('ng-invalid');
expect(element[0].classList).not.toContain('ng-valid');
});
});
});

View File

@ -34,19 +34,19 @@ export const validators = {
} else if (!validator.isNumeric(value))
throw new Error(`Value should be a number`);
},
inclusion: function(value, conf) {
inclusion: (value, conf) => {
if (!validator.isIn(value, conf.in))
throw new Error(`Invalid value`);
},
exclusion: function(value, conf) {
exclusion: (value, conf) => {
if (validator.isIn(value, conf.in))
throw new Error(`Invalid value`);
},
format: function(value, conf) {
format: (value, conf) => {
if (!validator.matches(value, conf.with))
throw new Error(`Invalid value`);
},
custom: function(value, conf) {
custom: (value, conf) => {
if (!conf.bindedFunction(value))
throw new Error(`Invalid value`);
}