validation client side unit tests
This commit is contained in:
parent
edb29c7ced
commit
f2872da4db
|
@ -0,0 +1,81 @@
|
||||||
|
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]`));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an error if the window.validations are not defined', () => {
|
||||||
|
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`));
|
||||||
|
});
|
||||||
|
|
||||||
|
// should adapt the test to whathver needs to test the user name instead of mail validation as it should be in mail-validation.js
|
||||||
|
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');
|
||||||
|
expect(element[0].classList).not.toContain('ng-valid');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
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.email = '1234?';
|
||||||
|
|
||||||
|
expect(element[0].classList).toContain('ng-valid');
|
||||||
|
expect(element[0].classList).not.toContain('ng-invalid');
|
||||||
|
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';
|
||||||
|
|
||||||
|
expect(element[0].classList).toContain('ng-valid');
|
||||||
|
expect(element[0].classList).not.toContain('ng-invalid');
|
||||||
|
scope.$digest();
|
||||||
|
|
||||||
|
expect(element[0].classList).toContain('ng-valid');
|
||||||
|
expect(element[0].classList).not.toContain('ng-invalid');
|
||||||
|
});
|
||||||
|
});
|
|
@ -51,9 +51,9 @@ export function directive(interpolate, compile, $window) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
scope.$watch(function() {
|
scope.$watch(() => {
|
||||||
return (form.$submitted || input.$dirty) && input.$invalid;
|
return (form.$submitted || input.$dirty) && input.$invalid;
|
||||||
}, function(value) {
|
}, value => {
|
||||||
let parent = element.parent();
|
let parent = element.parent();
|
||||||
|
|
||||||
if (value) {
|
if (value) {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import {validator} from 'vendor';
|
import {validator} from 'vendor';
|
||||||
|
|
||||||
export const validators = {
|
export const validators = {
|
||||||
presence: function(value, conf) {
|
presence: value => {
|
||||||
if (validator.isEmpty(value))
|
if (validator.isEmpty(value))
|
||||||
throw new Error(`Value can't be empty`);
|
throw new Error(`Value can't be empty`);
|
||||||
},
|
},
|
||||||
|
|
|
@ -47,8 +47,7 @@ module.exports = {
|
||||||
this.request.user = {
|
this.request.user = {
|
||||||
id: token.userId,
|
id: token.userId,
|
||||||
token: this.getToken()
|
token: this.getToken()
|
||||||
}
|
};
|
||||||
|
|
||||||
this.next();
|
this.next();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
|
@ -15,4 +15,4 @@ try {
|
||||||
config.proxy = require('../../nginx/config.json');
|
config.proxy = require('../../nginx/config.json');
|
||||||
config.package = require('../package.json');
|
config.package = require('../package.json');
|
||||||
|
|
||||||
module.exports = config;
|
module.exports = config;
|
||||||
|
|
Loading…
Reference in New Issue