角度js中的测试指令链接

时间:2014-07-30 06:27:36

标签: angularjs-directive

我正在测试角度Jasmine单元测试中的以下指令

angular.module('components.privileges', [])


.directive('hasPrivilege', function(privileges) {
 return {
     link: function (scope, element, attrs, $rootScope) {
         if (!angular.isString(attrs.hasPrivilege)) {
             throw "hasPrivilege value must be a string";
         }

         function toggleVisibilityBasedOnPrivilege() {
             var hasPrivilege = privileges.evaluatePrivilegeExpression(attrs.hasPrivilege.trim());
             console.log(hasPrivilege);
             if (hasPrivilege) {
                 element.show();
             } else {
                 element.hide();
             }
         }

         toggleVisibilityBasedOnPrivilege();
         $rootScope.$on('privilegesChanged', function(event, data){
             toggleVisibilityBasedOnPrivilege();
         });
     }
 };
 }).factory('privileges', function ($rootScope) {
    var privilegesList = {};
    return {
        clearPrivileges: function () {
            privilegesList = {};
        },
        setPrivileges: function (privileges) {
            privilegesList["PRIVILEGES_SET_FLAG"] = true;
            if(angular.isDefined(privileges) && angular.isArray(privileges) && privileges.length > 0){
                for(var p in privileges) {
                    var k = privileges[p];
                    if( k && angular.isString(k) && k.trim()!==""){
                        privilegesList[privileges[p]] = true;
                    }
                }
            }
            $rootScope.$emit('privilegesChanged');
        },
        privilegesLoaded: function () {
            return angular.isDefined(privilegesList["PRIVILEGES_SET_FLAG"]);
        },
        hasPrivilege: function (p) {
            var k = p && angular.isString(p) ? p.trim() : "";
            return p === "NA" || ( k!=="" && angular.isDefined(privilegesList[k]) );
        },
        evaluatePrivilegeExpression: function (exp) {
            var isPrivileged = false;
            if( exp !== undefined && exp !== null && angular.isString(exp)){
                exp = exp.trim();
                if(exp !== "" && exp !== "!"){
                    var value = exp;
                    var notPrivilegeFlag = value[0] === '!';
                    if (notPrivilegeFlag) {
                        value = value.slice(1).trim();
                    }
                    var hasPrivilege = this.hasPrivilege(value);
                    isPrivileged =  (hasPrivilege && !notPrivilegeFlag || !hasPrivilege && notPrivilegeFlag);
                }
            }
            return isPrivileged;
        }

    };
});

在指令中,当我调用setprivileges时,我使用'$ rootScope。$ emit('privilegesChanged');'事件通知权限已更改。如果,我的div没有这个特权,那么它必须隐藏在其他地方。

And my test case is as follows:-

describe("directive: hasPrivilege: ",function(){
        var element, scope, priviledgesArray, $privilegesFactory, $compile;
        beforeEach(function(){
            module("components.privileges");

            inject(function(privileges, _$rootScope_, _$compile_) {
                $privilegesFactory = privileges;
                scope= _$rootScope_;
                $compile = _$compile_ ;

            });


            priviledgesArray = ["123"];
        });


         it("should hide the element when privileges have not been set", function(){
             element = $compile('<div has-privilege="123"></div>')(scope);
             scope.$digest();
             $privilegesFactory.setPrivileges(priviledgesArray);
             console.log($(element).find("div"));
             expect($(element).find("div").style.display).toBe("block");

         });
    });

});

当我运行测试用例时,出现了以下错误    TypeError:'undefined'不是函数(评估'element.hide()')

我没有得到什么问题。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

在指令中尝试将元素引用为数组,即element[0].show();element[0].hide();