Support the remainder of XHR events

- onloadstart
- onprogress
- onabort
- onerror
- onload
- ontimeout
- onloadend

Fixes #81 Fixes #82
This commit is contained in:
slackersoft 2014-11-01 20:43:21 -07:00
parent 9681f79ece
commit 80297da4c1
3 changed files with 126 additions and 2 deletions

View File

@ -150,13 +150,34 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
this.status = 0;
this.statusText = "abort";
this.onreadystatechange();
this.onprogress();
this.onabort();
this.onloadend();
},
readyState: 0,
onloadstart: function() {
},
onprogress: function() {
},
onabort: function() {
},
onerror: function() {
},
onload: function() {
},
ontimeout: function() {
},
onloadend: function() {
},
onreadystatechange: function(isTimeout) {
},
@ -165,6 +186,7 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
send: function(data) {
this.params = data;
this.readyState = 2;
this.onloadstart();
this.onreadystatechange();
var stub = stubTracker.findStub(this.url, data, this.method);
@ -223,8 +245,10 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType);
this.responseXML = getResponseXml(response.responseText, this.getResponseHeader('content-type') || '');
this.onload();
this.onreadystatechange();
this.onprogress();
this.onload();
this.onloadend();
},
responseTimeout: function() {
@ -234,6 +258,20 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
this.readyState = 4;
jasmine.clock().tick(30000);
this.onreadystatechange('timeout');
this.onprogress();
this.ontimeout();
this.onloadend();
},
responseError: function() {
if (this.readyState === 4) {
throw new Error("FakeXMLHttpRequest already completed");
}
this.readyState = 4;
this.onreadystatechange();
this.onprogress();
this.onerror();
this.onloadend();
}
});

View File

@ -122,16 +122,22 @@ describe('FakeRequest', function() {
this.request.open();
this.request.onreadystatechange.calls.reset();
this.request.onloadstart = jasmine.createSpy('loadstart');
this.request.send();
expect(this.request.readyState).toBe(2);
expect(this.request.onreadystatechange).toHaveBeenCalled();
expect(this.request.onloadstart).toHaveBeenCalled();
});
it('has a ready state of 4 (loaded) when timed out', function() {
this.request.open();
this.request.send();
this.request.onreadystatechange.calls.reset();
this.request.ontimeout = jasmine.createSpy('timeout');
this.request.onprogress = jasmine.createSpy('progress');
this.request.onloadend = jasmine.createSpy('loadend');
jasmine.clock().install();
this.request.responseTimeout();
@ -139,6 +145,26 @@ describe('FakeRequest', function() {
expect(this.request.readyState).toBe(4);
expect(this.request.onreadystatechange).toHaveBeenCalledWith('timeout');
expect(this.request.ontimeout).toHaveBeenCalled();
expect(this.request.onprogress).toHaveBeenCalled();
expect(this.request.onloadend).toHaveBeenCalled();
});
it('has a ready state of 4 (loaded) when network erroring', function() {
this.request.open();
this.request.send();
this.request.onreadystatechange.calls.reset();
this.request.onerror = jasmine.createSpy('error');
this.request.onprogress = jasmine.createSpy('progress');
this.request.onloadend = jasmine.createSpy('loadend');
this.request.responseError();
expect(this.request.readyState).toBe(4);
expect(this.request.onreadystatechange).toHaveBeenCalled();
expect(this.request.onerror).toHaveBeenCalled();
expect(this.request.onprogress).toHaveBeenCalled();
expect(this.request.onloadend).toHaveBeenCalled();
});
it('has a ready state of 4 (loaded) when responding', function() {
@ -146,13 +172,17 @@ describe('FakeRequest', function() {
this.request.send();
this.request.onreadystatechange.calls.reset();
this.request.onprogress = jasmine.createSpy('onprogress');
this.request.onload = jasmine.createSpy('onload');
this.request.onloadend = jasmine.createSpy('onloadend');
this.request.response({});
expect(this.request.readyState).toBe(4);
expect(this.request.onreadystatechange).toHaveBeenCalled();
expect(this.request.onprogress).toHaveBeenCalled();
expect(this.request.onload).toHaveBeenCalled();
expect(this.request.onloadend).toHaveBeenCalled();
});
it('throws an error when timing out a request that has completed', function() {
@ -176,6 +206,17 @@ describe('FakeRequest', function() {
request.response({});
}).toThrowError('FakeXMLHttpRequest already completed');
});
it('throws an error when erroring a request that has completed', function() {
this.request.open();
this.request.send();
this.request.response({});
var request = this.request;
expect(function() {
request.responseError({});
}).toThrowError('FakeXMLHttpRequest already completed');
});
});
it('ticks the jasmine clock on timeout', function() {
@ -199,10 +240,17 @@ describe('FakeRequest', function() {
it('has an aborted status', function() {
var request = new this.FakeRequest();
request.onabort = jasmine.createSpy('onabort');
request.onprogress = jasmine.createSpy('progress');
request.onloadend = jasmine.createSpy('loadend');
request.abort();
expect(request.status).toBe(0);
expect(request.statusText).toBe('abort');
expect(request.onabort).toHaveBeenCalled();
expect(request.onprogress).toHaveBeenCalled();
expect(request.onloadend).toHaveBeenCalled();
});
it('has a status from the response', function() {

View File

@ -105,13 +105,34 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
this.status = 0;
this.statusText = "abort";
this.onreadystatechange();
this.onprogress();
this.onabort();
this.onloadend();
},
readyState: 0,
onloadstart: function() {
},
onprogress: function() {
},
onabort: function() {
},
onerror: function() {
},
onload: function() {
},
ontimeout: function() {
},
onloadend: function() {
},
onreadystatechange: function(isTimeout) {
},
@ -120,6 +141,7 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
send: function(data) {
this.params = data;
this.readyState = 2;
this.onloadstart();
this.onreadystatechange();
var stub = stubTracker.findStub(this.url, data, this.method);
@ -178,8 +200,10 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType);
this.responseXML = getResponseXml(response.responseText, this.getResponseHeader('content-type') || '');
this.onload();
this.onreadystatechange();
this.onprogress();
this.onload();
this.onloadend();
},
responseTimeout: function() {
@ -189,6 +213,20 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
this.readyState = 4;
jasmine.clock().tick(30000);
this.onreadystatechange('timeout');
this.onprogress();
this.ontimeout();
this.onloadend();
},
responseError: function() {
if (this.readyState === 4) {
throw new Error("FakeXMLHttpRequest already completed");
}
this.readyState = 4;
this.onreadystatechange();
this.onprogress();
this.onerror();
this.onloadend();
}
});