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(() => {
input.setAttribute("disabled", "true");
});
$element[0].querySelectorAll('i, vn-drop-down').forEach(i => {
i.parentNode.removeChild(i);
$element[0].querySelectorAll('i, vn-drop-down').forEach(element => {
element.parentNode.removeChild(element);
});
}
} else {

View File

@ -2,6 +2,7 @@ describe('Directive acl', () => {
let scope;
let element;
let compile;
let $timeout;
beforeEach(angular.mock.module($provide => {
$provide.value('aclService', {aclPermission: () => {}});
@ -11,18 +12,20 @@ describe('Directive acl', () => {
angular.mock.module('client');
});
compile = (hasPermissions, action) => {
inject(['$compile', '$rootScope', 'aclService', ($compile, $rootScope, aclService) => {
compile = (hasPermissions, _element) => {
inject(($compile, $rootScope, aclService, _$timeout_) => {
spyOn(aclService, 'aclPermission').and.returnValue(hasPermissions);
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);
scope.$digest();
}]);
});
};
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');
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', () => {
compile(false);
let input = element.find('input');
let html = `<container><div vn-acl="administrative,client" vn-acl-action="anything but disabled"><input/></div></container>`;
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', () => {
compile(false, 'disabled');
let html = `<div vn-acl="administrative,client" vn-acl-action="disabled"><input/></div>`;
compile(false, html);
let input = element.find('input');
$timeout.flush();
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);
});
});