I have the below code:
'use strict';
angular
.module('testmodule')
.factory('TestService', ['$q', '$timeout',
function ($q, $timeout) {
var timeoutRetries = 0; // need to mock this from here
var api = new TestApi();
function getResults(id, prevDeferred) {
var deferred = prevDeferred || $q.defer();
function handleSuccessResponse(data) {
if (data.status === 'ready') {
results.put(id, data);
deferred.resolve(data);
} else {
if (++timeoutRetries > 30) { // It wont get in here
handleErrorResponse();
} else {
$timeout(function () {
getResults(id, deferred);
}, 2000);
}
}
}
function handleErrorResponse(response) {
deferred.reject(response);
}
if (results.get(id)) {
deferred.resolve(doSomething.get(id));
return deferred.promise;
}
api.get({id: id}).then(handleSuccessResponse, handleErrorResponse);
return deferred.promise;
}
return {
getResults: getResults
};
}]);
I am trying to mock the timeoutRetries entry from karma but i am not able to do it. Is it the ideal way of declaring it or should i want to move the variable to some function and update or which is the best way to mock it up from karma?
Tried with inject, declared the variable before calling the function. Still no success.