2018-01-02 07:05:50 +00:00
|
|
|
describe('Directive validation', () => {
|
|
|
|
let scope;
|
|
|
|
let element;
|
|
|
|
let compile;
|
|
|
|
|
2019-09-13 14:09:14 +00:00
|
|
|
beforeEach(angular.mock.module('vnCore', $translateProvider => {
|
|
|
|
$translateProvider.translations('en', {});
|
|
|
|
}));
|
2018-01-02 07:05:50 +00:00
|
|
|
|
|
|
|
compile = (_element, validations, value) => {
|
2018-12-22 10:59:26 +00:00
|
|
|
inject(($compile, $rootScope, $window) => {
|
2018-01-02 07:05:50 +00:00
|
|
|
$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
|
|
|
|
2018-01-02 13:49:11 +00:00
|
|
|
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');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
});
|
2018-01-02 07:05:50 +00:00
|
|
|
});
|