Merge pull request #158 from gweax/static-state-constants

Add static state types
master
Gregg Van Hove 8 years ago committed by GitHub
commit d9b9b325c4

@ -44,7 +44,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
} }
}(typeof window !== 'undefined' ? window : global, function (global, getJasmineRequireObj) { }(typeof window !== 'undefined' ? window : global, function (global, getJasmineRequireObj) {
// //
getJasmineRequireObj().ajax = function(jRequire) { getJasmineRequireObj().ajax = function(jRequire) {
var $ajax = {}; var $ajax = {};
@ -284,6 +284,14 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
return null; return null;
} }
extend(FakeXMLHttpRequest, {
UNSENT: 0,
OPENED: 1,
HEADERS_RECEIVED: 2,
LOADING: 3,
DONE: 4
});
var iePropertiesThatCannotBeCopied = ['responseBody', 'responseText', 'responseXML', 'status', 'statusText', 'responseTimeout', 'responseURL']; var iePropertiesThatCannotBeCopied = ['responseBody', 'responseText', 'responseXML', 'status', 'statusText', 'responseTimeout', 'responseURL'];
extend(FakeXMLHttpRequest.prototype, new global.XMLHttpRequest(), iePropertiesThatCannotBeCopied); extend(FakeXMLHttpRequest.prototype, new global.XMLHttpRequest(), iePropertiesThatCannotBeCopied);
extend(FakeXMLHttpRequest.prototype, { extend(FakeXMLHttpRequest.prototype, {
@ -292,7 +300,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
this.url = arguments[1]; this.url = arguments[1];
this.username = arguments[3]; this.username = arguments[3];
this.password = arguments[4]; this.password = arguments[4];
this.readyState = 1; this.readyState = FakeXMLHttpRequest.OPENED;
this.requestHeaders = {}; this.requestHeaders = {};
this.eventBus.trigger('readystatechange'); this.eventBus.trigger('readystatechange');
}, },
@ -310,7 +318,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
}, },
abort: function() { abort: function() {
this.readyState = 0; this.readyState = FakeXMLHttpRequest.UNSENT;
this.status = 0; this.status = 0;
this.statusText = "abort"; this.statusText = "abort";
this.eventBus.trigger('readystatechange'); this.eventBus.trigger('readystatechange');
@ -319,7 +327,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
this.eventBus.trigger('loadend'); this.eventBus.trigger('loadend');
}, },
readyState: 0, readyState: FakeXMLHttpRequest.UNSENT,
onloadstart: null, onloadstart: null,
onprogress: null, onprogress: null,
@ -410,7 +418,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
case null: case null:
case "": case "":
case "text": case "text":
return this.readyState >= 3 ? this.responseText : ""; return this.readyState >= FakeXMLHttpRequest.LOADING ? this.responseText : "";
case "json": case "json":
return JSON.parse(this.responseText); return JSON.parse(this.responseText);
case "arraybuffer": case "arraybuffer":
@ -424,20 +432,20 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
respondWith: function(response) { respondWith: function(response) {
if (this.readyState === 4) { if (this.readyState === FakeXMLHttpRequest.DONE) {
throw new Error("FakeXMLHttpRequest already completed"); throw new Error("FakeXMLHttpRequest already completed");
} }
this.status = response.status; this.status = response.status;
this.statusText = response.statusText || ""; this.statusText = response.statusText || "";
this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType); this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType);
this.readyState = 2; this.readyState = FakeXMLHttpRequest.HEADERS_RECEIVED;
this.eventBus.trigger('readystatechange'); this.eventBus.trigger('readystatechange');
this.responseText = response.responseText || ""; this.responseText = response.responseText || "";
this.responseType = response.responseType || ""; this.responseType = response.responseType || "";
this.responseURL = response.responseURL || null; this.responseURL = response.responseURL || null;
this.readyState = 4; this.readyState = FakeXMLHttpRequest.DONE;
this.responseXML = getResponseXml(response.responseText, this.getResponseHeader('content-type') || ''); this.responseXML = getResponseXml(response.responseText, this.getResponseHeader('content-type') || '');
if (this.responseXML) { if (this.responseXML) {
this.responseType = 'document'; this.responseType = 'document';
@ -456,10 +464,10 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
}, },
responseTimeout: function() { responseTimeout: function() {
if (this.readyState === 4) { if (this.readyState === FakeXMLHttpRequest.DONE) {
throw new Error("FakeXMLHttpRequest already completed"); throw new Error("FakeXMLHttpRequest already completed");
} }
this.readyState = 4; this.readyState = FakeXMLHttpRequest.DONE;
jasmine.clock().tick(30000); jasmine.clock().tick(30000);
this.eventBus.trigger('readystatechange'); this.eventBus.trigger('readystatechange');
this.eventBus.trigger('progress'); this.eventBus.trigger('progress');
@ -468,10 +476,10 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
}, },
responseError: function() { responseError: function() {
if (this.readyState === 4) { if (this.readyState === FakeXMLHttpRequest.DONE) {
throw new Error("FakeXMLHttpRequest already completed"); throw new Error("FakeXMLHttpRequest already completed");
} }
this.readyState = 4; this.readyState = FakeXMLHttpRequest.DONE;
this.eventBus.trigger('readystatechange'); this.eventBus.trigger('readystatechange');
this.eventBus.trigger('progress'); this.eventBus.trigger('progress');
this.eventBus.trigger('error'); this.eventBus.trigger('error');

@ -117,19 +117,39 @@ describe('FakeRequest', function() {
this.request = new this.FakeRequest(); 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.request.readyState).toBe(0);
expect(this.fakeEventBus.trigger).not.toHaveBeenCalled(); 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(); this.request.open();
expect(this.request.readyState).toBe(1); expect(this.request.readyState).toBe(1);
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange'); 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.request.open();
this.fakeEventBus.trigger.calls.reset(); this.fakeEventBus.trigger.calls.reset();
@ -139,7 +159,7 @@ describe('FakeRequest', function() {
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange'); 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.request.open();
this.fakeEventBus.trigger.calls.reset(); this.fakeEventBus.trigger.calls.reset();
@ -150,7 +170,7 @@ describe('FakeRequest', function() {
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('readystatechange'); 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.open();
this.request.send(); this.request.send();
this.fakeEventBus.trigger.calls.reset(); this.fakeEventBus.trigger.calls.reset();
@ -163,7 +183,7 @@ describe('FakeRequest', function() {
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange'); 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.open();
this.request.send(); this.request.send();
this.fakeEventBus.trigger.calls.reset(); this.fakeEventBus.trigger.calls.reset();
@ -174,7 +194,7 @@ describe('FakeRequest', function() {
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange'); 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.open();
this.request.send(); this.request.send();
this.fakeEventBus.trigger.calls.reset(); this.fakeEventBus.trigger.calls.reset();
@ -185,7 +205,7 @@ describe('FakeRequest', function() {
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange'); 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.open();
this.request.send(); this.request.send();
this.fakeEventBus.trigger.calls.reset(); this.fakeEventBus.trigger.calls.reset();

@ -107,6 +107,14 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
return null; return null;
} }
extend(FakeXMLHttpRequest, {
UNSENT: 0,
OPENED: 1,
HEADERS_RECEIVED: 2,
LOADING: 3,
DONE: 4
});
var iePropertiesThatCannotBeCopied = ['responseBody', 'responseText', 'responseXML', 'status', 'statusText', 'responseTimeout', 'responseURL']; var iePropertiesThatCannotBeCopied = ['responseBody', 'responseText', 'responseXML', 'status', 'statusText', 'responseTimeout', 'responseURL'];
extend(FakeXMLHttpRequest.prototype, new global.XMLHttpRequest(), iePropertiesThatCannotBeCopied); extend(FakeXMLHttpRequest.prototype, new global.XMLHttpRequest(), iePropertiesThatCannotBeCopied);
extend(FakeXMLHttpRequest.prototype, { extend(FakeXMLHttpRequest.prototype, {
@ -115,7 +123,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
this.url = arguments[1]; this.url = arguments[1];
this.username = arguments[3]; this.username = arguments[3];
this.password = arguments[4]; this.password = arguments[4];
this.readyState = 1; this.readyState = FakeXMLHttpRequest.OPENED;
this.requestHeaders = {}; this.requestHeaders = {};
this.eventBus.trigger('readystatechange'); this.eventBus.trigger('readystatechange');
}, },
@ -133,7 +141,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
}, },
abort: function() { abort: function() {
this.readyState = 0; this.readyState = FakeXMLHttpRequest.UNSENT;
this.status = 0; this.status = 0;
this.statusText = "abort"; this.statusText = "abort";
this.eventBus.trigger('readystatechange'); this.eventBus.trigger('readystatechange');
@ -142,7 +150,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
this.eventBus.trigger('loadend'); this.eventBus.trigger('loadend');
}, },
readyState: 0, readyState: FakeXMLHttpRequest.UNSENT,
onloadstart: null, onloadstart: null,
onprogress: null, onprogress: null,
@ -233,7 +241,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
case null: case null:
case "": case "":
case "text": case "text":
return this.readyState >= 3 ? this.responseText : ""; return this.readyState >= FakeXMLHttpRequest.LOADING ? this.responseText : "";
case "json": case "json":
return JSON.parse(this.responseText); return JSON.parse(this.responseText);
case "arraybuffer": case "arraybuffer":
@ -247,20 +255,20 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
respondWith: function(response) { respondWith: function(response) {
if (this.readyState === 4) { if (this.readyState === FakeXMLHttpRequest.DONE) {
throw new Error("FakeXMLHttpRequest already completed"); throw new Error("FakeXMLHttpRequest already completed");
} }
this.status = response.status; this.status = response.status;
this.statusText = response.statusText || ""; this.statusText = response.statusText || "";
this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType); this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType);
this.readyState = 2; this.readyState = FakeXMLHttpRequest.HEADERS_RECEIVED;
this.eventBus.trigger('readystatechange'); this.eventBus.trigger('readystatechange');
this.responseText = response.responseText || ""; this.responseText = response.responseText || "";
this.responseType = response.responseType || ""; this.responseType = response.responseType || "";
this.responseURL = response.responseURL || null; this.responseURL = response.responseURL || null;
this.readyState = 4; this.readyState = FakeXMLHttpRequest.DONE;
this.responseXML = getResponseXml(response.responseText, this.getResponseHeader('content-type') || ''); this.responseXML = getResponseXml(response.responseText, this.getResponseHeader('content-type') || '');
if (this.responseXML) { if (this.responseXML) {
this.responseType = 'document'; this.responseType = 'document';
@ -279,10 +287,10 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
}, },
responseTimeout: function() { responseTimeout: function() {
if (this.readyState === 4) { if (this.readyState === FakeXMLHttpRequest.DONE) {
throw new Error("FakeXMLHttpRequest already completed"); throw new Error("FakeXMLHttpRequest already completed");
} }
this.readyState = 4; this.readyState = FakeXMLHttpRequest.DONE;
jasmine.clock().tick(30000); jasmine.clock().tick(30000);
this.eventBus.trigger('readystatechange'); this.eventBus.trigger('readystatechange');
this.eventBus.trigger('progress'); this.eventBus.trigger('progress');
@ -291,10 +299,10 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
}, },
responseError: function() { responseError: function() {
if (this.readyState === 4) { if (this.readyState === FakeXMLHttpRequest.DONE) {
throw new Error("FakeXMLHttpRequest already completed"); throw new Error("FakeXMLHttpRequest already completed");
} }
this.readyState = 4; this.readyState = FakeXMLHttpRequest.DONE;
this.eventBus.trigger('readystatechange'); this.eventBus.trigger('readystatechange');
this.eventBus.trigger('progress'); this.eventBus.trigger('progress');
this.eventBus.trigger('error'); this.eventBus.trigger('error');

Loading…
Cancel
Save