Mock Ajax refactor & simplification
This commit is contained in:
parent
cdbe9fe362
commit
83ef1be6e9
@ -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
|
@ -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;
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user