jQuery specs are GREEN.

This commit is contained in:
Davis W. Frank, dwfrank & Hunter Gillane 2010-09-16 16:51:52 -07:00
parent c129ed0675
commit cdbe9fe362
3 changed files with 121 additions and 72 deletions

View File

@ -39,6 +39,7 @@ Ajax.RealRequest = Class.create(Ajax.Request, {
function stubXhr(options) { function stubXhr(options) {
var xhr = { var xhr = {
requestHeaders: {}, requestHeaders: {},
open: function() { open: function() {
xhr.method = arguments[0]; xhr.method = arguments[0];
xhr.url = arguments[1]; xhr.url = arguments[1];
@ -59,7 +60,9 @@ function stubXhr(options) {
xhr.params = data; xhr.params = data;
}, },
getResponseHeader: function() {}, getResponseHeader: function(name) {
return xhr.responseHeaders[name];
},
responseText: null, responseText: null,
@ -67,6 +70,8 @@ function stubXhr(options) {
xhr.status = response.status; xhr.status = response.status;
xhr.responseText = response.responseText || ""; xhr.responseText = response.responseText || "";
xhr.readyState = 4; xhr.readyState = 4;
xhr.responseHeaders = response.responseHeaders ||
{"Content-type": response.contentType || Ajax.Response.defaultContentType};
// uncomment for jquery 1.3.x support // uncomment for jquery 1.3.x support
// jasmine.Clock.tick(20); // jasmine.Clock.tick(20);
@ -80,10 +85,6 @@ function stubXhr(options) {
return xhr; return xhr;
} }
// generic // generic
var ajaxRequests = []; var ajaxRequests = [];
@ -115,3 +116,4 @@ function clearAjaxRequests() {
XMLHttpRequest

View File

@ -1,5 +1,6 @@
describe("Jasmine Mock Ajax (for jQuery)", function() { describe("Jasmine Mock Ajax (for jQuery)", function() {
var request, anotherRequest, success, error, complete; var request, anotherRequest, response;
var success, error, complete;
var sharedContext = {}; var sharedContext = {};
beforeEach(function() { beforeEach(function() {
@ -98,20 +99,21 @@ describe("Jasmine Mock Ajax (for jQuery)", function() {
}); });
}); });
xdescribe("when simulating a response with request.response", function () { describe("when simulating a response with request.response", function () {
beforeEach(function() {
request = new Ajax.Request("idontcare", {
method: 'get',
onSuccess: success,
onFailure: error,
onComplete: complete
});
});
describe("and the response is Success", function () { describe("and the response is Success", function () {
beforeEach(function() { beforeEach(function() {
var response = {status: 200, contentType: "text/html", responseText: "OK!"}; request = jQuery.ajax({
url: "example.com/someApi",
type: "GET",
dataType: 'text',
success: success,
complete: complete,
error: error
});
response = {status: 200, contentType: "text/html", responseText: "OK!"};
request.response(response); request.response(response);
sharedContext.responseCallback = success; sharedContext.responseCallback = success;
sharedContext.status = response.status; sharedContext.status = response.status;
sharedContext.contentType = response.contentType; sharedContext.contentType = response.contentType;
@ -130,38 +132,21 @@ describe("Jasmine Mock Ajax (for jQuery)", function() {
expect(complete).toHaveBeenCalled(); expect(complete).toHaveBeenCalled();
}); });
sharedAjaxResponseBehavior(sharedContext); sharedAjaxResponseBehaviorForJQuery_Success(sharedContext);
});
describe("and the response is Failure", function () {
beforeEach(function() {
var response = {status: 500, contentType: "text/html", responseText: "(._){"};
request.response(response);
sharedContext.responseCallback = error;
sharedContext.status = response.status;
sharedContext.contentType = response.contentType;
sharedContext.responseText = response.responseText;
});
it("should not call the success handler", function() {
expect(success).not.toHaveBeenCalled();
});
it("should call the failure handler", function() {
expect(error).toHaveBeenCalled();
});
it("should call the complete handler", function() {
expect(complete).toHaveBeenCalled();
});
sharedAjaxResponseBehavior(sharedContext);
}); });
describe("and the response is Success, but with JSON", function () { describe("and the response is Success, but with JSON", function () {
var response;
beforeEach(function() { beforeEach(function() {
var responseObject = {status: 200, contentType: "application/json", responseText: "{'foo':'bar'}"}; request = jQuery.ajax({
url: "example.com/someApi",
type: "GET",
dataType: 'json',
success: success,
complete: complete,
error: error
});
var responseObject = {status: 200, contentType: "application/json", responseText: '{"foo":"bar"}'};
request.response(responseObject); request.response(responseObject);
@ -170,7 +155,7 @@ describe("Jasmine Mock Ajax (for jQuery)", function() {
sharedContext.contentType = responseObject.contentType; sharedContext.contentType = responseObject.contentType;
sharedContext.responseText = responseObject.responseText; sharedContext.responseText = responseObject.responseText;
response = success.mostRecentCall.args[0]; response = success.mostRecentCall.args[2];
}); });
it("should call the success handler", function() { it("should call the success handler", function() {
@ -186,16 +171,24 @@ describe("Jasmine Mock Ajax (for jQuery)", function() {
}); });
it("should return a JavaScript object", function() { it("should return a JavaScript object", function() {
window.response = response; expect(success.mostRecentCall.args[0]).toEqual({foo: "bar"});
expect(response.responseJSON).toEqual({foo: "bar"});
}); });
sharedAjaxResponseBehavior(sharedContext); sharedAjaxResponseBehaviorForJQuery_Success(sharedContext);
}); });
describe("the content type defaults to application/json", function () { describe("the content type defaults to application/json", function () {
beforeEach(function() { beforeEach(function() {
var response = {status: 200, responseText: "OK!"}; request = jQuery.ajax({
url: "example.com/someApi",
type: "GET",
dataType: 'json',
success: success,
complete: complete,
error: error
});
response = {status: 200, responseText: '{"foo": "valid JSON, dammit."}'};
request.response(response); request.response(response);
sharedContext.responseCallback = success; sharedContext.responseCallback = success;
@ -216,67 +209,121 @@ describe("Jasmine Mock Ajax (for jQuery)", function() {
expect(complete).toHaveBeenCalled(); expect(complete).toHaveBeenCalled();
}); });
sharedAjaxResponseBehavior(sharedContext); sharedAjaxResponseBehaviorForJQuery_Success(sharedContext);
}); });
describe("and the response is null", function () { describe("and the status/response code is 0", function () {
beforeEach(function() { beforeEach(function() {
request = jQuery.ajax({ request = jQuery.ajax({
url: "example.com/someApi", url: "example.com/someApi",
type: "GET", type: "GET",
dataType: "text",
success: success, success: success,
complete: complete, complete: complete,
error: error error: error
}); });
var response = {status: null, responseText: "whoops!"}; response = {status: 0, responseText: '{"foo": "whoops!"}'};
request.response(response); request.response(response);
sharedContext.responseCallback = on0; sharedContext.responseCallback = success;
sharedContext.status = 0; sharedContext.status = 0;
sharedContext.contentType = 'application/json'; sharedContext.contentType = 'application/json';
sharedContext.responseText = response.responseText; sharedContext.responseText = response.responseText;
}); });
it("should call the success handler", function() {
expect(success).toHaveBeenCalled();
});
it("should not call the failure handler", function() {
expect(error).not.toHaveBeenCalled();
});
it("should call the complete handler", function() {
expect(complete).toHaveBeenCalled();
});
sharedAjaxResponseBehaviorForJQuery_Success(sharedContext);
});
});
describe("and the response is error", function () {
beforeEach(function() {
request = jQuery.ajax({
url: "example.com/someApi",
type: "GET",
dataType: "text",
success: success,
complete: complete,
error: error
});
response = {status: 500, contentType: "text/html", responseText: "(._){"};
request.response(response);
sharedContext.responseCallback = error;
sharedContext.status = response.status;
sharedContext.contentType = response.contentType;
sharedContext.responseText = response.responseText;
});
it("should not call the success handler", function() { it("should not call the success handler", function() {
expect(success).not.toHaveBeenCalled(); expect(success).not.toHaveBeenCalled();
}); });
it("should not call the failure handler", function() { it("should call the failure handler", function() {
expect(error).not.toHaveBeenCalled(); expect(error).toHaveBeenCalled();
});
it("should call the on0 handler", function() {
expect(on0).toHaveBeenCalled();
}); });
it("should call the complete handler", function() { it("should call the complete handler", function() {
expect(complete).toHaveBeenCalled(); expect(complete).toHaveBeenCalled();
}); });
sharedAjaxResponseBehavior(sharedContext); sharedAjaxResponseBehaviorForJQuery_Failure(sharedContext);
});
}); });
}); });
function sharedAjaxResponseBehavior(context) {
describe("the response", function () { function sharedAjaxResponseBehaviorForJQuery_Success(context) {
var response; describe("the success response", function () {
var xhr;
beforeEach(function() { beforeEach(function() {
response = context.responseCallback.mostRecentCall.args[0]; xhr = context.responseCallback.mostRecentCall.args[2];
}); });
it("should have the expected status code", function() { it("should have the expected status code", function() {
expect(response.status).toEqual(context.status); expect(xhr.status).toEqual(context.status);
}); });
it("should have the expected content type", function() { it("should have the expected content type", function() {
expect(response.getHeader('Content-type')).toEqual(context.contentType); expect(xhr.getResponseHeader('Content-type')).toEqual(context.contentType);
}); });
it("should have the expected response text", function() { it("should have the expected response text", function() {
expect(response.responseText).toEqual(context.responseText); expect(xhr.responseText).toEqual(context.responseText);
});
});
}
function sharedAjaxResponseBehaviorForJQuery_Failure(context) {
describe("the failure response", function () {
var xhr;
beforeEach(function() {
xhr = context.responseCallback.mostRecentCall.args[0];
console.error("=============> xhr: ", xhr);
});
it("should have the expected status code", function() {
expect(xhr.status).toEqual(context.status);
});
it("should have the expected content type", function() {
expect(xhr.getResponseHeader('Content-type')).toEqual(context.contentType);
});
it("should have the expected response text", function() {
expect(xhr.responseText).toEqual(context.responseText);
}); });
}); });
} }

View File

@ -214,7 +214,7 @@ describe("Jasmine Mock Ajax (for Prototype.js)", function() {
sharedAjaxResponseBehavior(sharedContext); sharedAjaxResponseBehavior(sharedContext);
}); });
describe("and the response is null", function () { describe("and the status/response code is null", function () {
var on0; var on0;
beforeEach(function() { beforeEach(function() {
on0 = jasmine.createSpy('on0'); on0 = jasmine.createSpy('on0');