From 26e475dfb253f9db2b3176de1e32d990ecbbe7cc Mon Sep 17 00:00:00 2001 From: Hunter Gillane Date: Sun, 7 Aug 2011 14:20:37 -0600 Subject: [PATCH] pull jquery and prototype mocks into named functions --- .pairs | 6 ++ lib/mock-ajax.js | 66 ++++++++++--------- .../javascripts/fake-xml-http-request-spec.js | 12 ++-- spec/javascripts/mock-ajax-spec.js | 4 +- 4 files changed, 49 insertions(+), 39 deletions(-) create mode 100644 .pairs diff --git a/.pairs b/.pairs new file mode 100644 index 0000000..27ede7b --- /dev/null +++ b/.pairs @@ -0,0 +1,6 @@ +pairs: + hg: Hunter Gillane + +email: + prefix: pair + domain: pivotallabs.com diff --git a/lib/mock-ajax.js b/lib/mock-ajax.js index 48437ab..5c99627 100644 --- a/lib/mock-ajax.js +++ b/lib/mock-ajax.js @@ -1,35 +1,35 @@ /* -Jasmine-Ajax : a set of helpers for testing AJAX requests under the Jasmine -BDD framework for JavaScript. + Jasmine-Ajax : a set of helpers for testing AJAX requests under the Jasmine + BDD framework for JavaScript. -Supports both Prototype.js and jQuery. + Supports both Prototype.js and jQuery. -http://github.com/pivotal/jasmine-ajax + http://github.com/pivotal/jasmine-ajax -Jasmine Home page: http://pivotal.github.com/jasmine + Jasmine Home page: http://pivotal.github.com/jasmine -Copyright (c) 2008-2010 Pivotal Labs + Copyright (c) 2008-2010 Pivotal Labs -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*/ + */ // Jasmine-Ajax interface var ajaxRequests = []; @@ -151,11 +151,7 @@ jasmine.Ajax = { installJquery: function() { jasmine.Ajax.mode = 'jQuery'; jasmine.Ajax.real = jQuery.ajaxSettings.xhr; - jQuery.ajaxSettings.xhr = function() { - var newXhr = new FakeXMLHttpRequest(); - ajaxRequests.push(newXhr); - return newXhr; - } + jQuery.ajaxSettings.xhr = jasmine.Ajax.jQueryMock; }, @@ -163,9 +159,7 @@ jasmine.Ajax = { jasmine.Ajax.mode = 'Prototype'; jasmine.Ajax.real = Ajax.getTransport; - Ajax.getTransport = function() { - return new FakeXMLHttpRequest(); - }; + Ajax.getTransport = jasmine.Ajax.prototypeMock; }, uninstallMock: function() { @@ -184,6 +178,16 @@ jasmine.Ajax = { jasmine.Ajax.real = null; }, + jQueryMock: function() { + var newXhr = new FakeXMLHttpRequest(); + ajaxRequests.push(newXhr); + return newXhr; + }, + + prototypeMock: function() { + return new FakeXMLHttpRequest(); + }, + installed: false, mode: null } diff --git a/spec/javascripts/fake-xml-http-request-spec.js b/spec/javascripts/fake-xml-http-request-spec.js index 365f5e9..38b2b5e 100644 --- a/spec/javascripts/fake-xml-http-request-spec.js +++ b/spec/javascripts/fake-xml-http-request-spec.js @@ -36,10 +36,10 @@ describe("FakeXMLHttpRequest", function() { }); }); - it("can be extended", function(){ - FakeXMLHttpRequest.prototype.foo = function(){ - return "foo"; - } - expect(new FakeXMLHttpRequest().foo()).toEqual("foo"); - }); + it("can be extended", function(){ + FakeXMLHttpRequest.prototype.foo = function(){ + return "foo"; + } + expect(new FakeXMLHttpRequest().foo()).toEqual("foo"); + }); }); diff --git a/spec/javascripts/mock-ajax-spec.js b/spec/javascripts/mock-ajax-spec.js index ff8b02c..75b20bf 100644 --- a/spec/javascripts/mock-ajax-spec.js +++ b/spec/javascripts/mock-ajax-spec.js @@ -39,7 +39,7 @@ describe("jasmine.Ajax", function() { it("installs the mock", function() { withoutPrototype(function() { jasmine.Ajax.installMock(); - expect(jQuery.ajaxSettings.xhr).toBe(FakeXMLHttpRequest); + expect(jQuery.ajaxSettings.xhr).toBe(jasmine.Ajax.jQueryMock); }); }); @@ -63,7 +63,7 @@ describe("jasmine.Ajax", function() { it("installs the mock", function() { withoutJquery(function() { jasmine.Ajax.installMock(); - expect(Ajax.getTransport).toBe(FakeXMLHttpRequest); + expect(Ajax.getTransport).toBe(jasmine.Ajax.prototypeMock); }); });