Javascript 茉莉花替代品的检测
谁能告诉我如何测试被替换元素的指令?我面临一个问题,当我传入一个要在jasmine中编译的指令元素时,编译器不会返回替换的元素,而是返回未编译的元素。有没有合适的方法来测试这些类型的指令?谢谢 我的代码:Javascript 茉莉花替代品的检测,javascript,angularjs,angularjs-directive,jasmine,Javascript,Angularjs,Angularjs Directive,Jasmine,谁能告诉我如何测试被替换元素的指令?我面临一个问题,当我传入一个要在jasmine中编译的指令元素时,编译器不会返回替换的元素,而是返回未编译的元素。有没有合适的方法来测试这些类型的指令?谢谢 我的代码: var svgDirective = angular.module('svgDirective', ['adFactory']); svgDirective.directive("svg", ['$compile', '$timeout', '$http', 'ad' , function (
var svgDirective = angular.module('svgDirective', ['adFactory']);
svgDirective.directive("svg", ['$compile', '$timeout', '$http', 'ad' , function ($compile, $timeout, $http, ad) {
return {
restrict: 'EA',
link: function (scope, elem, attr) {
var refreshAd = function (id, missingFile) {
ad.notProcessed(missingFile).then(function () {
var timer = $timeout(function () {
refreshAd(id, missingFile)
}, 2000);
scope.$on("$destroy", function () {
$timeout.cancel(timer);
});
}, function () {
$http.get("/ads/" + id + ".json", {
cache: false
}).then(function (data) {
if (data["data"]["svg_image"]["url"] && missingFile.split('.').pop() === 'svg') {
elem.replaceWith("<object style='width: 75px;height:75px' data=" + data["data"]["svg_image"]["url"] + "></object>");
}
if (data["data"]["json_image"]["url"] && missingFile.split('.').pop() === 'json') {
scope.ad.json_image.url = data["data"]["json_image"]["url"];
}
});
});
};
if (scope.ad.is_svg_processed) {
elem.replaceWith("<object style='width:75px;height:75px' data=" + scope.ad.svg_image.url + "><object>");
} else {
refreshAd(attr.class, scope.ad.svg_file);
}
if (!scope.ad.is_json_processed) {
refreshAd(attr.class, scope.ad.json_file);
}
}
}
}]);
var svgDirective=angular.module('svgDirective',['adFactory']);
指令(“svg”[“$compile”、“$timeout”、“$http”、“ad”、函数($compile、$timeout、$http、ad){
返回{
限制:“EA”,
链接:功能(范围、要素、属性){
var refreshAd=函数(id,missingFile){
ad.notProcessed(missingFile).then(函数(){
变量计时器=$timeout(函数(){
refreshAd(id,丢失文件)
}, 2000);
作用域:“$destroy”,函数(){
$timeout.cancel(计时器);
});
},函数(){
$http.get(“/ads/”+id+“.json”{
缓存:false
}).then(功能(数据){
if(data[“data”][“svg_image”][“url”]&&missingFile.split('.').pop()='svg'){
要素替换为(“”);
}
if(data[“data”][“json_image”][“url”]&&missingFile.split('.').pop()='json'){
scope.ad.json_image.url=data[“data”][“json_image”][“url”];
}
});
});
};
if(scope.ad.is\u svg\u已处理){
要素替换为(“”);
}否则{
refreshAd(attr.class,scope.ad.svg_文件);
}
如果(!scope.ad.is_json_processed){
refreshAd(attr.class,scope.ad.json_文件);
}
}
}
}]);
我的测试代码:
describe("svg Directive", function(){
var $compile, $scope, $templateCache, $timeout, $http, ad, element;
beforeEach(angular.mock.module("svgDirective"));
beforeEach(module("templates"));
beforeEach(inject(function(_$compile_,_$rootScope_,_$templateCache_,_$timeout_, _$http_, _ad_){
$compile = _$compile_;
$scope = _$rootScope_.$new();
$templateCache = _$templateCache_;
$timeout = _$timeout_;
$http = _$http_;
ad = _ad_;
element = '<svg></svg>'
}));
it("should show svg images for ads whose svg images are processed", function(){
$scope.ad = {is_svg_processed:true,is_json_processed:true, svg_image:{url:"https://dicerocket.s3.amazonaws.com/ad/svg_image/622/1_1398608090_28021105597015874837455612979611260004.svg"}};
element = $compile(element)($scope);
$scope.$digest();
console.log(element);
});
});
description(“svg指令”,函数(){
var$compile、$scope、$templateCache、$timeout、$http、ad、element;
在每个之前(angular.mock.module(“svgDirective”);
在每个(模块(“模板”)之前;
beforeach(注入函数($compile、$rootScope、$templateCache、$timeout、$http、u ad){
$compile=\$compile;
$scope=\$rootScope\$new();
$templateCache=\u$templateCache;
$timeout=$timeout;
$http=\$http;
ad=_ad u;
元素=“”
}));
它(“应该为处理svg图像的广告显示svg图像”,函数(){
$scope.ad={is_svg_processed:true,is_json_processed:true,svg_image:{url:https://dicerocket.s3.amazonaws.com/ad/svg_image/622/1_1398608090_28021105597015874837455612979611260004.svg"}};
元素=$compile(元素)($scope);
$scope.$digest();
控制台日志(元素);
});
});
当console.log(element)
时,我得到的是
,而不是我想要替换的对象标记。谁能告诉我为什么
指令运行正常,但我似乎无法测试它。谢谢。这里有一个类似的问题: 总之,您需要将指令包装在其他元素中,如DIV
element = '<div><svg></svg></div>';
元素=“”;
这里有一个类似的问题:
总之,您需要将指令包装在其他元素中,如DIV
element = '<div><svg></svg></div>';
元素=“”;