validation unit tests for absence, length and numericality
This commit is contained in:
parent
86630be398
commit
b172f12ff6
|
@ -26,7 +26,7 @@ describe('Directive validation', () => {
|
||||||
}).toThrow(new Error(`vnValidation: Attribute must have this syntax: [entity].[field]`));
|
}).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"/>`;
|
let html = `<input type="name" ng-model="user.name" vn-validation="user.name"/>`;
|
||||||
|
|
||||||
expect(() => {
|
expect(() => {
|
||||||
|
@ -34,15 +34,12 @@ describe('Directive validation', () => {
|
||||||
}).toThrow(new Error(`vnValidation: Entity 'User' doesn't exist`));
|
}).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', () => {
|
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 html = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
|
||||||
let validations = {User: {validations: {name: [{validation: 'presence'}]}}};
|
let validations = {User: {validations: {name: [{validation: 'presence'}]}}};
|
||||||
compile(html, validations, 'Spiderman');
|
compile(html, validations, 'Spiderman');
|
||||||
scope.user.name = '';
|
scope.user.name = '';
|
||||||
|
|
||||||
expect(element[0].classList).toContain('ng-valid');
|
|
||||||
expect(element[0].classList).not.toContain('ng-invalid');
|
|
||||||
scope.$digest();
|
scope.$digest();
|
||||||
|
|
||||||
expect(element[0].classList).toContain('ng-invalid');
|
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()', () => {
|
||||||
let html = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
|
it('should not validate the entity as it should be an empty string', () => {
|
||||||
let validations = {User: {validations: {name: [{validation: 'presence'}]}}};
|
let html = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
|
||||||
compile(html, validations, 'spiderman');
|
let validations = {User: {validations: {name: [{validation: 'absence'}]}}};
|
||||||
scope.user.name = '';
|
compile(html, validations, 'Spiderman');
|
||||||
scope.$digest();
|
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 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');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should validate the email as it's format is correct`, () => {
|
describe('Validator length()', () => {
|
||||||
let html = `<form><input type="email" ng-model="user.email" vn-validation="user.email"/></form>`;
|
it('should not validate the user name as it should have min length of 15', () => {
|
||||||
let validations = {User: {validations: {email: [{validation: 'presence'}]}}};
|
let html = `<form><input type="name" ng-model="user.name" vn-validation="user.name"/></form>`;
|
||||||
compile(html, validations, 'user@verdnatura.es');
|
let validations = {User: {validations: {name: [{validation: 'length', min: 10, max: 50, is: 15}]}}};
|
||||||
scope.user.email = 'user@verdnatura.es';
|
compile(html, validations, 'fifteen!');
|
||||||
scope.$digest();
|
scope.$digest();
|
||||||
|
|
||||||
expect(element[0].classList).toContain('ng-valid');
|
expect(element[0].classList).toContain('ng-invalid');
|
||||||
expect(element[0].classList).not.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');
|
||||||
|
// });
|
||||||
});
|
});
|
||||||
|
|
|
@ -40,7 +40,7 @@ export function directive(interpolate, compile, $window) {
|
||||||
let errorShown = false;
|
let errorShown = false;
|
||||||
|
|
||||||
input.$options.$$options.allowInvalid = true;
|
input.$options.$$options.allowInvalid = true;
|
||||||
input.$validators.entity = function(value) {
|
input.$validators.entity = value => {
|
||||||
try {
|
try {
|
||||||
validateAll(value, validations);
|
validateAll(value, validations);
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -9,11 +9,12 @@ export const validators = {
|
||||||
if (!validator.isEmpty(value))
|
if (!validator.isEmpty(value))
|
||||||
throw new Error(`Value should be empty`);
|
throw new Error(`Value should be empty`);
|
||||||
},
|
},
|
||||||
length: function(value, conf) {
|
length: (value, conf) => {
|
||||||
let options = {
|
let options = {
|
||||||
min: conf.min || conf.is,
|
min: conf.min || conf.is,
|
||||||
max: conf.max || conf.is
|
max: conf.max || conf.is
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!validator.isLength(value, options)) {
|
if (!validator.isLength(value, options)) {
|
||||||
if (conf.is) {
|
if (conf.is) {
|
||||||
throw new Error(`Value should be ${conf.is} characters long`);
|
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 (conf.int) {
|
||||||
if (!validator.isInt(value))
|
if (!validator.isInt(value))
|
||||||
throw new Error(`Value should be integer`);
|
throw new Error(`Value should be integer`);
|
||||||
|
|
Loading…
Reference in New Issue