From 9908439d8113f398ecb76ea2c42f17b49b694e71 Mon Sep 17 00:00:00 2001 From: Kevin Montag Date: Fri, 11 Jul 2014 00:23:13 -0700 Subject: [PATCH] Throw an error if response() is called more than once on requests Closes #71 Fixes #70 --- lib/mock-ajax.js | 6 ++++++ spec/javascripts/fake-xml-http-request-spec.js | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/mock-ajax.js b/lib/mock-ajax.js index 224c13d..7cc57b5 100644 --- a/lib/mock-ajax.js +++ b/lib/mock-ajax.js @@ -249,6 +249,9 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. responseText: null, response: function(response) { + if (this.readyState === 4) { + throw new Error("FakeXMLHttpRequest already completed"); + } this.status = response.status; this.statusText = response.statusText || ""; this.responseText = response.responseText || ""; @@ -261,6 +264,9 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. }, responseTimeout: function() { + if (this.readyState === 4) { + throw new Error("FakeXMLHttpRequest already completed"); + } this.readyState = 4; jasmine.clock().tick(30000); this.onreadystatechange('timeout'); diff --git a/spec/javascripts/fake-xml-http-request-spec.js b/spec/javascripts/fake-xml-http-request-spec.js index 197414f..8751a86 100644 --- a/spec/javascripts/fake-xml-http-request-spec.js +++ b/spec/javascripts/fake-xml-http-request-spec.js @@ -81,6 +81,22 @@ describe("FakeXMLHttpRequest", function() { expect(xhr.readyState).toEqual(4); expect(xhr.onreadystatechange).toHaveBeenCalled(); }); + + describe("when a second response comes in", function() { + it("should throw an error", function() { + xhr.onreadystatechange.calls.reset(); + xhr.response({status: 200}); + expect(function() { xhr.response({status: 200}); }).toThrowError('FakeXMLHttpRequest already completed'); + }); + }); + + describe("when a second response comes in as a timout", function() { + it("should throw an error", function() { + xhr.onreadystatechange.calls.reset(); + xhr.response({status: 200}); + expect(function() { xhr.responseTimeout(); }).toThrowError('FakeXMLHttpRequest already completed'); + }); + }); }); describe("when aborted", function() {