Mock Ajax tests for Prototype.js cleaned up; small renames of interface to AjaxRequests

This commit is contained in:
Davis W. Frank, dwfrank & Hunter Gillane 2010-09-16 14:49:46 -07:00
parent e7ca84008c
commit c011d7542d
2 changed files with 79 additions and 157 deletions

View File

@ -36,21 +36,22 @@ Ajax.RealRequest = Class.create(Ajax.Request, {
}
});
// generic
var ajaxRequests = [];
FakeAjaxTransport = Class.create({
initialize: function() {
this.overrideMimeType = false;
this.readyState = 0;
this.setRequestHeader = jasmine.createSpy("setRequestHeader");
this.open = jasmine.createSpy("open");
this.send = jasmine.createSpy("send");
this.abort = jasmine.createSpy("abort");
},
getResponseHeader: function(name) {
function FakeAjaxTransport() {
this.overrideMimeType = false;
this.readyState = 0;
this.setRequestHeader = jasmine.createSpy("setRequestHeader");
this.open = jasmine.createSpy("open");
this.send = jasmine.createSpy("send");
this.abort = jasmine.createSpy("abort");
this.getResponseHeader = function(name) {
return this.responseHeaders[name];
}
});
};
}
function mostRecentAjaxRequest() {
if (ajaxRequests.length > 0) {

View File

@ -189,57 +189,75 @@ describe("Jasmine Mock Ajax (for Prototype.js)", function() {
sharedAjaxResponseBehavior(sharedContext);
});
describe("the content type defaults to application/json", function () {
beforeEach(function() {
var response = {status: 200, responseText: "OK!"};
request.response(response);
sharedContext.responseCallback = onSuccess;
sharedContext.status = response.status;
sharedContext.contentType = "application/json";
sharedContext.responseText = response.responseText;
});
it("should call the success handler", function() {
expect(onSuccess).toHaveBeenCalled();
});
it("should not call the failure handler", function() {
expect(onFailure).not.toHaveBeenCalled();
});
it("should call the complete handler", function() {
expect(onComplete).toHaveBeenCalled();
});
sharedAjaxResponseBehavior(sharedContext);
});
describe("and the response is null", function () {
var on0;
beforeEach(function() {
on0 = jasmine.createSpy('on0');
request = new Ajax.Request("idontcare", {
method: 'get',
on0: on0,
onSuccess: onSuccess,
onFailure: onFailure,
onComplete: onComplete
});
var response = {status: null, responseText: "whoops!"};
request.response(response);
sharedContext.responseCallback = on0;
sharedContext.status = 0;
sharedContext.contentType = 'application/json';
sharedContext.responseText = response.responseText;
});
it("should not call the success handler", function() {
expect(onSuccess).not.toHaveBeenCalled();
});
it("should not call the failure handler", function() {
expect(onFailure).not.toHaveBeenCalled();
});
it("should call the on0 handler", function() {
expect(on0).toHaveBeenCalled();
});
it("should call the complete handler", function() {
expect(onComplete).toHaveBeenCalled();
});
sharedAjaxResponseBehavior(sharedContext);
});
});
});
// describe(".response", function() {
// it("should present responseJSON if contentType is application/json", function() {
// request = new Ajax.Request("http://example.com/someApi", {
// onSuccess: onSuccess,
// onFailure: onFailure,
// onComplete: onComplete
// });
//
// request.response({status: 201, contentType: "application/json", responseText: "{'foo':'bar'}"});
// expect(onSuccess).toHaveBeenCalled();
// var response = onSuccess.mostRecentCall.args[0];
// expect(response.getHeader('Content-type')).toEqual("application/json");
// expect(response.responseJSON).toEqual({foo: "bar"});
// });
//
// it("should default to status 200", function() {
// request.response({contentType: "text/html", responseText: "Yay!"});
// expect(onSuccess).toHaveBeenCalled();
// expect(onComplete).toHaveBeenCalled();
// var response = onSuccess.mostRecentCall.args[0];
// expect(response.status).toEqual(200);
// });
//
// it("should default to contentType application/json", function() {
// request.response({status: 200, responseText: "{'foo':'bar'}"});
// expect(onComplete).toHaveBeenCalled();
// var response = onComplete.mostRecentCall.args[0];
// expect(response.getHeader('Content-type')).toEqual("application/json");
// expect(response.responseJSON).toEqual({foo: "bar"});
// });
//
// it("should convert null response status to status 0", function() {
// request.response({status: null, responseText: ""});
// expect(onSuccess).toHaveBeenCalled();
// expect(onComplete).toHaveBeenCalled();
// var response = onSuccess.mostRecentCall.args[0];
// expect(response.status).toEqual(0);
// });
//
// it("should accept a string", function() {
// request.response("{'foo': 'bar'}");
// expect(onSuccess).toHaveBeenCalled();
// var response = onSuccess.mostRecentCall.args[0];
// expect(response.responseText).toEqual("{'foo': 'bar'}");
// });
// });
//});
function sharedAjaxResponseBehavior(context) {
describe("the response", function () {
var response;
@ -260,100 +278,3 @@ function sharedAjaxResponseBehavior(context) {
});
});
}
xdescribe("pockets.mock_ajax", function() {
var request, success, failure, complete;
beforeEach(function() {
success = jasmine.createSpy("success");
failure = jasmine.createSpy("failure");
complete = jasmine.createSpy("complete");
request = new Ajax.Request("balthazarurl", {onSuccess: success, onFailure: failure,
onComplete: complete});
});
it("should store URL and transport", function() {
expect(request.url).toEqual("balthazarurl");
expect(request.transport).toBeTruthy();
});
describe("AjaxRequests", function() {
it("should attach new AJAX requests to AjaxRequests.requests", function() {
expect(AjaxRequests.requests.length).toEqual(1);
expect(AjaxRequests.activeRequest()).toEqual(request);
var request2 = new Ajax.Request("balthazarurl2", {onSuccess: success, onFailure: failure,
onComplete: complete});
expect(AjaxRequests.requests.length).toEqual(2);
expect(AjaxRequests.activeRequest()).toEqual(request2);
});
it("should let you clear AJAX requests", function() {
clearAjaxRequests();
expect(AjaxRequests.requests.length).toEqual(0);
});
});
describe(".response", function() {
it("should pretend that the AJAX request returned a response with status, contentType, and responseText", function() {
request.response({status: 201, contentType: "text/html", responseText: "You have been redirected."});
expect(success).wasCalled();
expect(complete).wasCalled();
expect(failure).wasNotCalled();
var response = success.mostRecentCall.args[0];
expect(response.status).toEqual(201);
expect(response.getHeader('Content-type')).toEqual("text/html");
expect(response.responseText).toEqual("You have been redirected.");
expect(response.responseJSON).toEqual(null);
});
it("should call failure for error statuses", function() {
request.response({status: 500, contentType: "text/html", responseText: "Ar mateys."});
expect(success).wasNotCalled();
expect(complete).wasCalled();
expect(failure).wasCalled();
var response = failure.mostRecentCall.args[0];
expect(response.status).toEqual(500);
expect(response.getHeader('Content-type')).toEqual("text/html");
expect(response.responseText).toEqual("Ar mateys.");
});
it("should present responseJSON if contentType is application/json", function() {
request.response({status: 201, contentType: "application/json", responseText: "{'foo':'bar'}"});
expect(success).wasCalled();
var response = success.mostRecentCall.args[0];
expect(response.getHeader('Content-type')).toEqual("application/json");
expect(response.responseJSON).toEqual({foo: "bar"});
});
it("should default to status 200", function() {
request.response({contentType: "text/html", responseText: "Yay!"});
expect(success).wasCalled();
expect(complete).wasCalled();
var response = success.mostRecentCall.args[0];
expect(response.status).toEqual(200);
});
it("should default to contentType application/json", function() {
request.response({status: 200, responseText: "{'foo':'bar'}"});
expect(complete).wasCalled();
var response = complete.mostRecentCall.args[0];
expect(response.getHeader('Content-type')).toEqual("application/json");
expect(response.responseJSON).toEqual({foo: "bar"});
});
it("should convert null response status to status 0", function() {
request.response({status: null, responseText: ""});
expect(success).wasCalled();
expect(complete).wasCalled();
var response = success.mostRecentCall.args[0];
expect(response.status).toEqual(0);
});
it("should accept a string", function() {
request.response("{'foo': 'bar'}");
expect(success).wasCalled();
var response = success.mostRecentCall.args[0];
expect(response.responseText).toEqual("{'foo': 'bar'}");
});
});
});