2018-01-02 07:05:50 +00:00
|
|
|
describe('Directive validation', () => {
|
|
|
|
let scope;
|
|
|
|
let element;
|
|
|
|
let compile;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
angular.mock.module('client');
|
|
|
|
});
|
|
|
|
|
|
|
|
compile = (_element, validations, value) => {
|
|
|
|
inject(($compile, $rootScope, aclService, _$timeout_, $window) => {
|
|
|
|
$window.validations = validations;
|
|
|
|
scope = $rootScope.$new();
|
|
|
|
scope.user = {name: value};
|
|
|
|
element = angular.element(_element);
|
|
|
|
$compile(element)(scope);
|
|
|
|
scope.$digest();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
it(`should throw an error if the vnValidation doesn't have the right syntax`, () => {
|
|
|
|
let html = `<input type="name" ng-model="user.name" vn-validation="user"/>`;
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
compile(html, {});
|
|
|
|
}).toThrow(new Error(`vnValidation: Attribute must have this syntax: [entity].[field]`));
|
|
|
|
});
|
|
|
|
|
2018-01-02 13:10:17 +00:00
|
|
|
it('should throw an error if the window.validations aint defined', () => {
|
2018-01-02 07:05:50 +00:00
|
|
|
let html = `<input type="name" ng-model="user.name" vn-validation="user.name"/>`;
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
compile(html, {});
|
|
|
|
}).toThrow(new Error(`vnValidation: Entity 'User' doesn't exist`));
|
|
|
|
});
|
|
|
|
|
2018-01-02 13:10:17 +00:00
|
|
|
describe('Validator presence()', () => {
|
2018-01-02 07:05:50 +00:00
|
|
|
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 = '';
|
2018-01-02 13:10:17 +00:00
|
|
|
scope.$digest();
|
|
|
|
|
|
|
|
expect(element[0].classList).toContain('ng-invalid');
|
|
|
|
expect(element[0].classList).not.toContain('ng-valid');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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: '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 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();
|
2018-01-02 07:05:50 +00:00
|
|
|
|
|
|
|
expect(element[0].classList).toContain('ng-valid');
|
|
|
|
expect(element[0].classList).not.toContain('ng-invalid');
|
2018-01-02 13:10:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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!');
|
2018-01-02 07:05:50 +00:00
|
|
|
scope.$digest();
|
|
|
|
|
|
|
|
expect(element[0].classList).toContain('ng-invalid');
|
|
|
|
expect(element[0].classList).not.toContain('ng-valid');
|
|
|
|
});
|
|
|
|
|
2018-01-02 13:10:17 +00:00
|
|
|
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();
|
2018-01-02 07:05:50 +00:00
|
|
|
|
2018-01-02 13:10:17 +00:00
|
|
|
expect(element[0].classList).toContain('ng-invalid');
|
|
|
|
expect(element[0].classList).not.toContain('ng-valid');
|
|
|
|
});
|
2018-01-02 07:05:50 +00:00
|
|
|
});
|
|
|
|
|
2018-01-02 13:10:17 +00:00
|
|
|
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();
|
2018-01-02 07:05:50 +00:00
|
|
|
|
2018-01-02 13:10:17 +00:00
|
|
|
expect(element[0].classList).toContain('ng-valid');
|
|
|
|
expect(element[0].classList).not.toContain('ng-invalid');
|
|
|
|
});
|
2018-01-02 07:05:50 +00:00
|
|
|
});
|
2018-01-02 13:10:17 +00:00
|
|
|
|
|
|
|
// 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');
|
|
|
|
// });
|
2018-01-02 07:05:50 +00:00
|
|
|
});
|