client side unit test for acl directive plus small refactor

This commit is contained in:
Carlos 2017-10-19 19:11:49 +02:00
parent e8473d56fb
commit a530c67336
2 changed files with 24 additions and 13 deletions

View File

@ -19,8 +19,8 @@ function vnAcl(aclService, $timeout) {
$timeout(() => { $timeout(() => {
input.setAttribute("disabled", "true"); input.setAttribute("disabled", "true");
}); });
$element[0].querySelectorAll('i, vn-drop-down').forEach(i => { $element[0].querySelectorAll('i, vn-drop-down').forEach(element => {
i.parentNode.removeChild(i); element.parentNode.removeChild(element);
}); });
} }
} else { } else {

View File

@ -2,6 +2,7 @@ describe('Directive acl', () => {
let scope; let scope;
let element; let element;
let compile; let compile;
let $timeout;
beforeEach(angular.mock.module($provide => { beforeEach(angular.mock.module($provide => {
$provide.value('aclService', {aclPermission: () => {}}); $provide.value('aclService', {aclPermission: () => {}});
@ -11,18 +12,20 @@ describe('Directive acl', () => {
angular.mock.module('client'); angular.mock.module('client');
}); });
compile = (hasPermissions, action) => { compile = (hasPermissions, _element) => {
inject(['$compile', '$rootScope', 'aclService', ($compile, $rootScope, aclService) => { inject(($compile, $rootScope, aclService, _$timeout_) => {
spyOn(aclService, 'aclPermission').and.returnValue(hasPermissions); spyOn(aclService, 'aclPermission').and.returnValue(hasPermissions);
scope = $rootScope.$new(); scope = $rootScope.$new();
element = angular.element(`<div><input vn-acl="administrative,client" vn-acl-action="${action}"/></div>`); $timeout = _$timeout_;
element = angular.element(_element);
$compile(element)(scope); $compile(element)(scope);
scope.$digest(); scope.$digest();
}]); });
}; };
it('should not disable the input element as the user has permision', () => { it('should not disable the input element as the user has permision', () => {
compile(true, 'disabled'); let html = `<div vn-acl="administrative,client" vn-acl-action="disabled"><input/></div>`;
compile(true, html);
let input = element.find('input'); let input = element.find('input');
expect(input).toBeDefined(); expect(input).toBeDefined();
@ -30,18 +33,26 @@ describe('Directive acl', () => {
}); });
it('should delete the element as the user does not have permission and there is no action', () => { it('should delete the element as the user does not have permission and there is no action', () => {
compile(false); let html = `<container><div vn-acl="administrative,client" vn-acl-action="anything but disabled"><input/></div></container>`;
let input = element.find('input'); compile(false, html);
expect(input.length).toEqual(0); expect(element.children().length).toEqual(0);
}); });
// check if the element shouldn't be deleted if the action is to disable it with Juan/Dani
it('should disable the element as the action is to disable it but the user has no permission but present', () => { it('should disable the element as the action is to disable it but the user has no permission but present', () => {
compile(false, 'disabled'); let html = `<div vn-acl="administrative,client" vn-acl-action="disabled"><input/></div>`;
compile(false, html);
let input = element.find('input'); let input = element.find('input');
$timeout.flush();
expect(input).toBeDefined(); expect(input).toBeDefined();
expect(input.attr('disabled')).toBeFalsy(); expect(input.attr('disabled')).toBeTruthy();
});
it('should delete any element with the tag i and vn-drop-down', () => {
let html = `<div vn-acl="administrative,client" vn-acl-action="disabled"><label><i/></label><input/></div>`;
compile(false, html);
expect(element.find('i').length).toBe(0);
}); });
}); });