When it comes to testing angularjs controllers with jasmine and karma, there seems to be an issue with testing codeblocks within the sweetalert function. How can I verify that the sweet function is being called from my test class in order to test whether $scope.getCategory() is executed or not? Below is a snippet of code from my controller along with a jasmine test case.
Controller:
$scope.changeCategoryStatus = function(selectedId, selectedActive, topId) {
sweet.show({
title : "Kategoriyi "+(!selectedActive ? 'aktif' : 'pasif')+ " hale getirmek istiyor musunuz?",
type : "warning",
showCancelButton : true,
confirmButtonColor : "#DD6B55",
confirmButtonText : "Evet, değiştir!",
closeOnConfirm : false,
showLoaderOnConfirm : true,
html : false
}, function() {
$http({
method : "POST",
url : webRootUrl+"ajax/category/setActivation",
data : {
"id" : selectedId,
"active" : !selectedActive
}
}).then(function(response) {
//console.log(JSON.stringify(response.data))
if(response.data.outPutDouble==-7){
swal("Değiştirilemedi!", response.data.outPutString,"error");
}else{
$scope.getCategory(topId);
swal("Bu kategorinin durumu değiştirildi","",
"success");
}
}, function myError(response) {
swal("Bu kategorinin durumu değiştirilemedi","","error");
//console.log("***aaa" + JSON.stringify(response))
});
});
}
Jasmine Test Case:
it("changeCategoryStatus success", function () {
$scope.changeCategoryStatus(21,true,0)
spyOn($scope,'getCategory')
expect($scope.getCategory).toHaveBeenCalled(); });
Wondering if anyone has encountered a similar challenge before. Appreciate any assistance provided. Thank you!