From 95b36b27edae14a9b79da94f77d902c39093dcb3 Mon Sep 17 00:00:00 2001 From: Matthias Reuter Date: Thu, 7 Jul 2016 11:49:10 +0200 Subject: [PATCH] Add static state types --- lib/mock-ajax.js | 32 ++++++++++++++++++++------------ spec/fakeRequestSpec.js | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/lib/mock-ajax.js b/lib/mock-ajax.js index 8667d0a..19fda97 100644 --- a/lib/mock-ajax.js +++ b/lib/mock-ajax.js @@ -44,7 +44,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. } }(typeof window !== 'undefined' ? window : global, function (global, getJasmineRequireObj) { -// +// getJasmineRequireObj().ajax = function(jRequire) { var $ajax = {}; @@ -284,6 +284,14 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) { return null; } + extend(FakeXMLHttpRequest, { + UNSENT: 0, + OPENED: 1, + HEADERS_RECEIVED: 2, + LOADING: 3, + DONE: 4 + }); + var iePropertiesThatCannotBeCopied = ['responseBody', 'responseText', 'responseXML', 'status', 'statusText', 'responseTimeout', 'responseURL']; extend(FakeXMLHttpRequest.prototype, new global.XMLHttpRequest(), iePropertiesThatCannotBeCopied); extend(FakeXMLHttpRequest.prototype, { @@ -292,7 +300,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) { this.url = arguments[1]; this.username = arguments[3]; this.password = arguments[4]; - this.readyState = 1; + this.readyState = FakeXMLHttpRequest.OPENED; this.requestHeaders = {}; this.eventBus.trigger('readystatechange'); }, @@ -310,7 +318,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) { }, abort: function() { - this.readyState = 0; + this.readyState = FakeXMLHttpRequest.UNSENT; this.status = 0; this.statusText = "abort"; this.eventBus.trigger('readystatechange'); @@ -319,7 +327,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) { this.eventBus.trigger('loadend'); }, - readyState: 0, + readyState: FakeXMLHttpRequest.UNSENT, onloadstart: null, onprogress: null, @@ -403,7 +411,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) { case null: case "": case "text": - return this.readyState >= 3 ? this.responseText : ""; + return this.readyState >= FakeXMLHttpRequest.LOADING ? this.responseText : ""; case "json": return JSON.parse(this.responseText); case "arraybuffer": @@ -417,20 +425,20 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) { respondWith: function(response) { - if (this.readyState === 4) { + if (this.readyState === FakeXMLHttpRequest.DONE) { throw new Error("FakeXMLHttpRequest already completed"); } this.status = response.status; this.statusText = response.statusText || ""; this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType); - this.readyState = 2; + this.readyState = FakeXMLHttpRequest.HEADERS_RECEIVED; this.eventBus.trigger('readystatechange'); this.responseText = response.responseText || ""; this.responseType = response.responseType || ""; this.responseURL = response.responseURL || null; - this.readyState = 4; + this.readyState = FakeXMLHttpRequest.DONE; this.responseXML = getResponseXml(response.responseText, this.getResponseHeader('content-type') || ''); if (this.responseXML) { this.responseType = 'document'; @@ -449,10 +457,10 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) { }, responseTimeout: function() { - if (this.readyState === 4) { + if (this.readyState === FakeXMLHttpRequest.DONE) { throw new Error("FakeXMLHttpRequest already completed"); } - this.readyState = 4; + this.readyState = FakeXMLHttpRequest.DONE; jasmine.clock().tick(30000); this.eventBus.trigger('readystatechange'); this.eventBus.trigger('progress'); @@ -461,10 +469,10 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) { }, responseError: function() { - if (this.readyState === 4) { + if (this.readyState === FakeXMLHttpRequest.DONE) { throw new Error("FakeXMLHttpRequest already completed"); } - this.readyState = 4; + this.readyState = FakeXMLHttpRequest.DONE; this.eventBus.trigger('readystatechange'); this.eventBus.trigger('progress'); this.eventBus.trigger('error'); diff --git a/spec/fakeRequestSpec.js b/spec/fakeRequestSpec.js index 5243d7e..bf90728 100644 --- a/spec/fakeRequestSpec.js +++ b/spec/fakeRequestSpec.js @@ -117,19 +117,39 @@ describe('FakeRequest', function() { this.request = new this.FakeRequest(); }); - it('has an initial ready state of 0 (uninitialized)', function() { + it('has a static state UNSENT', function () { + expect(this.FakeRequest.UNSENT).toBe(0); + }); + + it('has a static state OPENED', function () { + expect(this.FakeRequest.OPENED).toBe(1); + }); + + it('has a static state HEADERS_RECEIVED', function () { + expect(this.FakeRequest.HEADERS_RECEIVED).toBe(2); + }); + + it('has a static state LOADING', function () { + expect(this.FakeRequest.LOADING).toBe(3); + }); + + it('has a static state DONE', function () { + expect(this.FakeRequest.DONE).toBe(4); + }); + + it('has an initial ready state of 0 (unsent)', function() { expect(this.request.readyState).toBe(0); expect(this.fakeEventBus.trigger).not.toHaveBeenCalled(); }); - it('has a ready state of 1 (open) when opened', function() { + it('has a ready state of 1 (opened) when opened', function() { this.request.open(); expect(this.request.readyState).toBe(1); expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange'); }); - it('has a ready state of 0 (uninitialized) when aborted', function() { + it('has a ready state of 0 (unsent) when aborted', function() { this.request.open(); this.fakeEventBus.trigger.calls.reset(); @@ -139,7 +159,7 @@ describe('FakeRequest', function() { expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange'); }); - it('has a ready state of 1 (sent) when sent', function() { + it('has a ready state of 1 (opened) when sent', function() { this.request.open(); this.fakeEventBus.trigger.calls.reset(); @@ -150,7 +170,7 @@ describe('FakeRequest', function() { expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('readystatechange'); }); - it('has a ready state of 4 (loaded) when timed out', function() { + it('has a ready state of 4 (done) when timed out', function() { this.request.open(); this.request.send(); this.fakeEventBus.trigger.calls.reset(); @@ -163,7 +183,7 @@ describe('FakeRequest', function() { expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange'); }); - it('has a ready state of 4 (loaded) when network erroring', function() { + it('has a ready state of 4 (done) when network erroring', function() { this.request.open(); this.request.send(); this.fakeEventBus.trigger.calls.reset(); @@ -174,7 +194,7 @@ describe('FakeRequest', function() { expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange'); }); - it('has a ready state of 4 (loaded) when responding', function() { + it('has a ready state of 4 (done) when responding', function() { this.request.open(); this.request.send(); this.fakeEventBus.trigger.calls.reset(); @@ -185,7 +205,7 @@ describe('FakeRequest', function() { expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange'); }); - it('has a ready state of 2, then 4 (loaded) when responding', function() { + it('has a ready state of 2, then 4 (done) when responding', function() { this.request.open(); this.request.send(); this.fakeEventBus.trigger.calls.reset();