From c011d7542dcde7b38236d4f0a8e19b2bbbdf0f61 Mon Sep 17 00:00:00 2001 From: "Davis W. Frank, dwfrank & Hunter Gillane" Date: Thu, 16 Sep 2010 14:49:46 -0700 Subject: [PATCH] Mock Ajax tests for Prototype.js cleaned up; small renames of interface to AjaxRequests --- lib/mock-ajax.js | 25 ++- .../javascripts/mock-ajax-prototypejs-spec.js | 211 ++++++------------ 2 files changed, 79 insertions(+), 157 deletions(-) diff --git a/lib/mock-ajax.js b/lib/mock-ajax.js index 27638ef..e025a64 100644 --- a/lib/mock-ajax.js +++ b/lib/mock-ajax.js @@ -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) { diff --git a/spec/javascripts/mock-ajax-prototypejs-spec.js b/spec/javascripts/mock-ajax-prototypejs-spec.js index 40205a8..73e2afb 100644 --- a/spec/javascripts/mock-ajax-prototypejs-spec.js +++ b/spec/javascripts/mock-ajax-prototypejs-spec.js @@ -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'}"); - }); - }); -});