validation unit tests for absence, length and numericality

This commit is contained in:
Carlos Jimenez 2018-01-02 14:10:17 +01:00
parent 86630be398
commit b172f12ff6
3 changed files with 131 additions and 24 deletions

View File

@ -26,7 +26,7 @@ describe('Directive validation', () => {
}).toThrow(new Error(`vnValidation: Attribute must have this syntax: [entity].[field]`));
});
it('should throw an error if the window.validations are not defined', () => {
it('should throw an error if the window.validations aint defined', () => {
let html = `<input type="name" ng-model="user.name" vn-validation="user.name"/>`;
expect(() => {
@ -34,15 +34,12 @@ describe('Directive validation', () => {
}).toThrow(new Error(`vnValidation: Entity 'User' doesn't exist`));
});
describe('Validator Presence()', () => {
describe('Validator presence()', () => {
it('should not validate the user name as it is an empty string', () => {
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 = '';
expect(element[0].classList).toContain('ng-valid');
expect(element[0].classList).not.toContain('ng-invalid');
scope.$digest();
expect(element[0].classList).toContain('ng-invalid');
@ -50,25 +47,134 @@ describe('Directive validation', () => {
});
});
it(`should not validate the name as it's format is incorrect`, () => {
describe('Validator absence()', () => {
it('should not validate the entity as it should be an empty string', () => {
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 = '';
let validations = {User: {validations: {name: [{validation: 'absence'}]}}};
compile(html, validations, 'Spiderman');
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, 'user@verdnatura.es');
scope.user.email = 'user@verdnatura.es';
it('should validate the entity as it is an empty string', () => {
let html = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
let validations = {User: {validations: {name: [{validation: 'absence'}]}}};
compile(html, validations, '');
scope.$digest();
expect(element[0].classList).toContain('ng-valid');
expect(element[0].classList).not.toContain('ng-invalid');
});
});
describe('Validator length()', () => {
it('should not validate the user name as it should have min length of 15', () => {
let html = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
let validations = {User: {validations: {name: [{validation: 'length', min: 10, max: 50, is: 15}]}}};
compile(html, validations, 'fifteen!');
scope.$digest();
expect(element[0].classList).toContain('ng-invalid');
expect(element[0].classList).not.toContain('ng-valid');
});
it('should validate the user name as it has length of 15', () => {
let html = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
let validations = {User: {validations: {name: [{validation: 'length', min: 10, max: 50, is: 15}]}}};
compile(html, validations, 'fifteen length!');
scope.$digest();
expect(element[0].classList).toContain('ng-valid');
expect(element[0].classList).not.toContain('ng-invalid');
});
it('should not validate the user name as it should have min length of 10', () => {
let html = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
let validations = {User: {validations: {name: [{validation: 'length', min: 10}]}}};
compile(html, validations, 'shortname');
scope.$digest();
expect(element[0].classList).toContain('ng-invalid');
expect(element[0].classList).not.toContain('ng-valid');
});
it('should validate the user name as its length is greater then the minimum', () => {
let html = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
let validations = {User: {validations: {name: [{validation: 'length', min: 10}]}}};
compile(html, validations, 'verylongname');
scope.$digest();
expect(element[0].classList).toContain('ng-valid');
expect(element[0].classList).not.toContain('ng-invalid');
});
it('should not validate the user name as its length is greater then the maximum', () => {
let html = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
let validations = {User: {validations: {name: [{validation: 'length', max: 10}]}}};
compile(html, validations, 'toolongname');
scope.$digest();
expect(element[0].classList).toContain('ng-invalid');
expect(element[0].classList).not.toContain('ng-valid');
});
});
describe('Validator numericality()', () => {
it('should not validate the phone number as it should a integer', () => {
let html = `<form><input type="text" ng-model="user.phone" vn-validation="user.phone"/></form>`;
let validations = {User: {validations: {phone: [{validation: 'numericality', is: 'what is this?'}]}}};
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 phone number as it an integer', () => {
let html = `<form><input type="text" ng-model="user.phone" vn-validation="user.phone"/></form>`;
let validations = {User: {validations: {phone: [{validation: 'numericality', is: 'what is this?'}]}}};
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');
});
});
// 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();
// 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');
// });
});

View File

@ -40,7 +40,7 @@ export function directive(interpolate, compile, $window) {
let errorShown = false;
input.$options.$$options.allowInvalid = true;
input.$validators.entity = function(value) {
input.$validators.entity = value => {
try {
validateAll(value, validations);
return true;

View File

@ -9,11 +9,12 @@ export const validators = {
if (!validator.isEmpty(value))
throw new Error(`Value should be empty`);
},
length: function(value, conf) {
length: (value, conf) => {
let options = {
min: conf.min || conf.is,
max: conf.max || conf.is
};
if (!validator.isLength(value, options)) {
if (conf.is) {
throw new Error(`Value should be ${conf.is} characters long`);
@ -26,7 +27,7 @@ export const validators = {
}
}
},
numericality: function(value, conf) {
numericality: (value, conf) => {
if (conf.int) {
if (!validator.isInt(value))
throw new Error(`Value should be integer`);