From 3ebf994356739901f160f686238416b37b2fcd46 Mon Sep 17 00:00:00 2001 From: slackersoft Date: Fri, 17 Jan 2014 10:41:23 -0800 Subject: [PATCH] trigger onreadystatechange everywhere we set readyState - Partially addresses #19 --- lib/mock-ajax.js | 3 +++ spec/javascripts/fake-xml-http-request-spec.js | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/mock-ajax.js b/lib/mock-ajax.js index d4aaba9..2a635f2 100644 --- a/lib/mock-ajax.js +++ b/lib/mock-ajax.js @@ -109,6 +109,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. this.username = arguments[3]; this.password = arguments[4]; this.readyState = 1; + this.onreadystatechange(); }, setRequestHeader: function(header, value) { @@ -119,6 +120,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. this.readyState = 0; this.status = 0; this.statusText = "abort"; + this.onreadystatechange(); }, readyState: 0, @@ -134,6 +136,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. send: function(data) { this.params = data; this.readyState = 2; + this.onreadystatechange(); var stub = stubTracker.findStub(this.url); if (stub) { diff --git a/spec/javascripts/fake-xml-http-request-spec.js b/spec/javascripts/fake-xml-http-request-spec.js index 8476361..f00ff2d 100644 --- a/spec/javascripts/fake-xml-http-request-spec.js +++ b/spec/javascripts/fake-xml-http-request-spec.js @@ -43,30 +43,39 @@ describe("FakeXMLHttpRequest", function() { describe("when opened", function() { beforeEach(function() { + spyOn(xhr, 'onreadystatechange'); xhr.open("GET", "http://example.com"); }); + it("should have a readyState of 1 (open)", function() { expect(xhr.readyState).toEqual(1); + expect(xhr.onreadystatechange).toHaveBeenCalled(); }); describe("when sent", function() { it("should have a readyState of 2 (sent)", function() { + xhr.onreadystatechange.calls.reset(); xhr.send(null); expect(xhr.readyState).toEqual(2); + expect(xhr.onreadystatechange).toHaveBeenCalled(); }); }); describe("when a response comes in", function() { it("should have a readyState of 4 (loaded)", function() { + xhr.onreadystatechange.calls.reset(); xhr.response({status: 200}); expect(xhr.readyState).toEqual(4); + expect(xhr.onreadystatechange).toHaveBeenCalled(); }); }); describe("when aborted", function() { it("should have a readyState of 0 (uninitialized)", function() { + xhr.onreadystatechange.calls.reset(); xhr.abort(); expect(xhr.readyState).toEqual(0); + expect(xhr.onreadystatechange).toHaveBeenCalled(); expect(xhr.status).toEqual(0); expect(xhr.statusText).toEqual("abort"); });