Mock Ajax refactor & simplification

This commit is contained in:
Davis W. Frank, dwfrank & Hunter Gillane 2010-09-16 17:07:01 -07:00
parent cdbe9fe362
commit 83ef1be6e9
2 changed files with 27 additions and 70 deletions

View File

@ -1,42 +1,20 @@
Ajax.Request.prototype.response = function(responseOptions) {
this.transport.readyState = 4;
if (typeof(responseOptions) == "string") {
responseOptions = {responseText: responseOptions};
// Jasmine-Ajax interface
var ajaxRequests = [];
function mostRecentAjaxRequest() {
if (ajaxRequests.length > 0) {
return ajaxRequests[ajaxRequests.length - 1];
} else {
return null;
}
}
this.transport.responseHeaders = responseOptions.responseHeaders ||
{"Content-type": responseOptions.contentType || Ajax.Response.defaultContentType};
this.transport.status = typeof(responseOptions.status) == "undefined" ? 200 : responseOptions.status;
this.transport.responseText = responseOptions.responseText;
this.transport.onreadystatechange();
};
function clearAjaxRequests() {
ajaxRequests = [];
}
Ajax.Response.defaultContentType = "application/json";
Ajax.Request.prototype.oldRequest = Ajax.Request.prototype.request;
Ajax.Request.prototype.request = function(url) {
this.oldRequest(url);
ajaxRequests.push(this);
};
Ajax.RealRequest = Class.create(Ajax.Request, {
request: function(url) {
this.transport = Try.these(
function() {
return new XMLHttpRequest()
},
function() {
return new ActiveXObject('Msxml2.XMLHTTP')
},
function() {
return new ActiveXObject('Microsoft.XMLHTTP')
}
) || false;
this.oldRequest(url);
}
});
function stubXhr(options) {
// Fake XHR for mocking Ajax Requests & Responses
function FakeXMLHttpRequest(options) {
var xhr = {
requestHeaders: {},
@ -52,7 +30,7 @@ function stubXhr(options) {
abort: function() {
},
readyState: 1,
readyState: 0,
status: null,
@ -71,7 +49,7 @@ function stubXhr(options) {
xhr.responseText = response.responseText || "";
xhr.readyState = 4;
xhr.responseHeaders = response.responseHeaders ||
{"Content-type": response.contentType || Ajax.Response.defaultContentType};
{"Content-type": response.contentType || "application/json" };
// uncomment for jquery 1.3.x support
// jasmine.Clock.tick(20);
@ -85,35 +63,14 @@ function stubXhr(options) {
return xhr;
}
// generic
// Jasmine-Ajax Glue code for Prototype.js
Ajax.Request.prototype.originalRequest = Ajax.Request.prototype.request;
Ajax.Request.prototype.request = function(url) {
this.originalRequest(url);
ajaxRequests.push(this);
};
var ajaxRequests = [];
Ajax.Request.prototype.response = function(responseOptions) {
return this.transport.response(responseOptions);
};
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) {
return ajaxRequests[ajaxRequests.length - 1];
} else {
return null;
}
}
function clearAjaxRequests() {
ajaxRequests = [];
}
XMLHttpRequest

View File

@ -4,11 +4,11 @@ beforeEach(function() {
clearAjaxRequests();
spyOn(Ajax, "getTransport").andCallFake(function() {
return new FakeAjaxTransport();
return new FakeXMLHttpRequest();
});
spyOn(jQuery.ajaxSettings, 'xhr').andCallFake(function() {
var newXhr = stubXhr();
var newXhr = new FakeXMLHttpRequest();
ajaxRequests.push(newXhr);
return newXhr;
});