Am a newbie in Jasmine
. am trying to write a snippet to test my function
which is written in jQuery
ajax
.
Here is the snippet
function submitLoginForm(e){
e.preventDefault();
try {
var data = $("form").serialize();
$.ajax({
type: "POST",
data: data,
url : "/auth/login",
dataType: 'json',
success: function(data){
submitLoginFormSuccess(data)
},
error: function( xhr, status, errorThrown ){
submitLoginFormError(xhr, status, errorThrown)
}
});
}catch (e) {
throw new Error(e.message);
}
}
and the callback functions are
function submitLoginFormSuccess(data){
if(data.status == "success"){
window.location = "/app"
}
else{
toastr["error"]("Invalid Credentials!")
}
}
function submitLoginFormError(xhr, status, errorThrown){
console.log( "Error: " + errorThrown );
console.log( "Status: " + status );
console.dir( xhr );
}
Please someone help me.
Issues am getting are;-
event is undefined.
I didn't have any idea how to check success and failure response
Update below snippet is running fine. How should i test other scenerio?
describe("#submitLoginForm", function() {
it("should make sure that all parameter of ajax is not changed", function() {
spyOn($, "ajax");
submitLoginForm(event);
var result = $.ajax.mostRecentCall.args[0];
expect(result.url).toEqual("/auth/login");
expect(result.dataType).toEqual("json");
expect(result.type).toEqual("POST");
});
});