unit tests for valudation > validator inclusion, exclusion and format
This commit is contained in:
parent
b172f12ff6
commit
67d99e788e
|
@ -145,36 +145,42 @@ describe('Directive validation', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// it(`should not validate the name as it's format is incorrect`, () => {
|
describe('Validator inclusion()', () => {
|
||||||
// let html = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
|
it('should not validate the phone number as it is not an integer', () => {
|
||||||
// let validations = {User: {validations: {name: [{validation: 'presence'}]}}};
|
let html = `<form><input type="text" ng-model="user.phone" vn-validation="user.phone"/></form>`;
|
||||||
// compile(html, validations, 'spiderman');
|
let validations = {User: {validations: {phone: [{validation: 'inclusion'}]}}};
|
||||||
// scope.user.name = '';
|
compile(html, validations, 'spiderman');
|
||||||
// scope.$digest();
|
scope.user.phone = 'this is not a phone number!';
|
||||||
|
scope.$digest();
|
||||||
|
|
||||||
// expect(element[0].classList).toContain('ng-invalid');
|
expect(element[0].classList).toContain('ng-invalid');
|
||||||
// expect(element[0].classList).not.toContain('ng-valid');
|
expect(element[0].classList).not.toContain('ng-valid');
|
||||||
// });
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// it(`should validate the email as it's format is correct`, () => {
|
describe('Validator exclusion()', () => {
|
||||||
// let html = `<form><input type="email" ng-model="user.email" vn-validation="user.email"/></form>`;
|
it('should validate the phone number as it is an integer', () => {
|
||||||
// let validations = {User: {validations: {email: [{validation: 'presence'}]}}};
|
let html = `<form><input type="text" ng-model="user.phone" vn-validation="user.phone"/></form>`;
|
||||||
// compile(html, validations, 'spiderman');
|
let validations = {User: {validations: {phone: [{validation: 'exclusion'}]}}};
|
||||||
// scope.user.email = 'user@verdnatura.es';
|
compile(html, validations, 'spiderman');
|
||||||
// scope.$digest();
|
scope.user.phone = '555555555';
|
||||||
|
scope.$digest();
|
||||||
|
|
||||||
// expect(element[0].classList).toContain('ng-valid');
|
expect(element[0].classList).toContain('ng-valid');
|
||||||
// expect(element[0].classList).not.toContain('ng-invalid');
|
expect(element[0].classList).not.toContain('ng-invalid');
|
||||||
// });
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// it(`should not validate the email as it's missing the @`, () => {
|
describe('Validator format()', () => {
|
||||||
// let html = `<form><input type="email" ng-model="user.email" vn-validation="user.email"/></form>`;
|
it('should not validate the email number as it doesnt contain @', () => {
|
||||||
// let validations = {User: {validations: {email: [{validation: 'presence'}]}}};
|
let html = `<form><input type="text" ng-model="user.email" vn-validation="user.email"/></form>`;
|
||||||
// compile(html, validations, 'spiderman');
|
let validations = {User: {validations: {email: [{validation: 'format', with: '@'}]}}};
|
||||||
// scope.user.email = 'userverdnatura.es';
|
compile(html, validations, 'spiderman');
|
||||||
// scope.$digest();
|
scope.user.email = 'userverdnatura.es';
|
||||||
|
scope.$digest();
|
||||||
|
|
||||||
// expect(element[0].classList).toContain('ng-invalid');
|
expect(element[0].classList).toContain('ng-invalid');
|
||||||
// expect(element[0].classList).not.toContain('ng-valid');
|
expect(element[0].classList).not.toContain('ng-valid');
|
||||||
// });
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -34,19 +34,19 @@ export const validators = {
|
||||||
} else if (!validator.isNumeric(value))
|
} else if (!validator.isNumeric(value))
|
||||||
throw new Error(`Value should be a number`);
|
throw new Error(`Value should be a number`);
|
||||||
},
|
},
|
||||||
inclusion: function(value, conf) {
|
inclusion: (value, conf) => {
|
||||||
if (!validator.isIn(value, conf.in))
|
if (!validator.isIn(value, conf.in))
|
||||||
throw new Error(`Invalid value`);
|
throw new Error(`Invalid value`);
|
||||||
},
|
},
|
||||||
exclusion: function(value, conf) {
|
exclusion: (value, conf) => {
|
||||||
if (validator.isIn(value, conf.in))
|
if (validator.isIn(value, conf.in))
|
||||||
throw new Error(`Invalid value`);
|
throw new Error(`Invalid value`);
|
||||||
},
|
},
|
||||||
format: function(value, conf) {
|
format: (value, conf) => {
|
||||||
if (!validator.matches(value, conf.with))
|
if (!validator.matches(value, conf.with))
|
||||||
throw new Error(`Invalid value`);
|
throw new Error(`Invalid value`);
|
||||||
},
|
},
|
||||||
custom: function(value, conf) {
|
custom: (value, conf) => {
|
||||||
if (!conf.bindedFunction(value))
|
if (!conf.bindedFunction(value))
|
||||||
throw new Error(`Invalid value`);
|
throw new Error(`Invalid value`);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue