Skip to content

Commit

Permalink
Only install Prototype.js glue code if Prototype.js is included; Spec…
Browse files Browse the repository at this point in the history
… Helper will now install either or both mocking code
  • Loading branch information
Davis W. Frank, dwfrank committed Sep 17, 2010
1 parent ace08b2 commit ec1867e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
29 changes: 15 additions & 14 deletions lib/mock-ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ var ajaxRequests = [];

function mostRecentAjaxRequest() {
if (ajaxRequests.length > 0) {
return ajaxRequests[ajaxRequests.length - 1];
} else {
return null;
}
return ajaxRequests[ajaxRequests.length - 1];
} else {
return null;
}
}

function clearAjaxRequests() {
Expand Down Expand Up @@ -49,7 +49,7 @@ function FakeXMLHttpRequest() {
xhr.responseText = response.responseText || "";
xhr.readyState = 4;
xhr.responseHeaders = response.responseHeaders ||
{"Content-type": response.contentType || "application/json" };
{"Content-type": response.contentType || "application/json" };

// uncomment for jquery 1.3.x support
// jasmine.Clock.tick(20);
Expand All @@ -62,13 +62,14 @@ function FakeXMLHttpRequest() {
}

// 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);
};

Ajax.Request.prototype.response = function(responseOptions) {
return this.transport.response(responseOptions);
};
if (typeof Prototype != 'undefined' && Ajax && Ajax.Request) {
Ajax.Request.prototype.originalRequest = Ajax.Request.prototype.request;
Ajax.Request.prototype.request = function(url) {
this.originalRequest(url);
ajaxRequests.push(this);
};

Ajax.Request.prototype.response = function(responseOptions) {
return this.transport.response(responseOptions);
};
}
5 changes: 4 additions & 1 deletion lib/spec-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ beforeEach(function() {
ajaxRequests.push(newXhr);
return newXhr;
});
} else {
}

if (typeof Prototype != 'undefined') {
spyOn(Ajax, "getTransport").andCallFake(function() {
return new FakeXMLHttpRequest();
});
}

clearAjaxRequests();

});

0 comments on commit ec1867e

Please sign in to comment.