I am currently testing a callback function that requires a response object as its sole parameter. The response object is the result of an HTTP request made from a different location, so using $httpBackend in this particular test is unnecessary as the request is not related to this function.
This function is located in home.js, which serves as the controller for the homepage of my application.
Below is the function that is being tested:
function submitLogin() {
LoginService.login(loginPost, ctrl.username, ctrl.password, successCallback, errorCallback);
}
// successCallback is invoked in LoginService if the login response is 201, otherwise, errorCallback is called
function successCallback(response) {
// retrieving necessary details to determine correct forms for the user
var sessionData = {
token: response.data.token,
user_id: response.data.user_id,
institution_name: response.data.institution_name,
status: response.data.status,
form_uri: getFormURI(response.data.forms) // extracting form URI for list of available forms for a specific app
};
ctrl.formCheckInProgress = true;
// updating user's forms from the backend and caching them
FormFetcherService.updateCachedForms(sessionData.user_id, sessionData.form_uri).then(function (response) {
if (response == 'updated') {
toastr.success('Your forms have been updated to the newest version', 'Forms Updated');
}
else {
toastr.success('Your forms are already up-to-date', 'No Update Required');
}
});
Login Service:
angular.module('appName').service('LoginService', ['$http', function ($http) {
this.login = function (url, username, password, successCallback, errorCallback) {
var data = {
username: username,
password: password
};
$http.post(url, $.param(data), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
timeout: 10000
}
).then(successCallback, errorCallback);
}
}]);
I am looking to provide an object to substitute the 'response' object that is being passed into the function.
Is there a way for me to include a .json file in my /tests directory, load and parse the JSON into a JavaScript object, and then utilize this object in my unit test?
I have researched possible solutions, but most of them assume that a request is being made in the function being tested - which is not the scenario in this case.
Thank you,
Dean