Merge pull request #104 from mjeanroy/fix-97

Event readystatechange is triggered when headers are available
This commit is contained in:
Gregg Van Hove 2015-04-09 13:44:38 -07:00
commit 60a31169f0
2 changed files with 47 additions and 7 deletions

View File

@ -125,14 +125,15 @@ describe('FakeRequest', function() {
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
});
it('has a ready state of 2 (sent) when sent', function() {
it('has a ready state of 1 (sent) when sent', function() {
this.request.open();
this.fakeEventBus.trigger.calls.reset();
this.request.send();
expect(this.request.readyState).toBe(2);
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
expect(this.request.readyState).toBe(1);
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('loadstart');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('readystatechange');
});
it('has a ready state of 4 (loaded) when timed out', function() {
@ -170,6 +171,43 @@ describe('FakeRequest', function() {
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
});
it('has a ready state of 2, then 4 (loaded) when responding', function() {
this.request.open();
this.request.send();
this.fakeEventBus.trigger.calls.reset();
var request = this.request;
var events = [];
var headers = [
{ name: 'X-Header', value: 'foo' }
];
this.fakeEventBus.trigger.and.callFake(function(event) {
if (event === 'readystatechange') {
events.push({
readyState: request.readyState,
status: request.status,
statusText: request.statusText,
responseHeaders: request.responseHeaders
});
}
});
this.request.respondWith({
status: 200,
statusText: 'OK',
responseHeaders: headers
});
expect(this.request.readyState).toBe(4);
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
expect(events.length).toBe(2);
expect(events).toEqual([
{ readyState: 2, status: 200, statusText: 'OK', responseHeaders: headers },
{ readyState: 4, status: 200, statusText: 'OK', responseHeaders: headers }
]);
});
it('throws an error when timing out a request that has completed', function() {
this.request.open();
this.request.send();
@ -270,7 +308,7 @@ describe('FakeRequest', function() {
this.request.send();
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('loadstart');
expect(this.fakeEventBus.trigger).toHaveBeenCalledWith('readystatechange');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('readystatechange');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('progress');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('abort');
expect(this.fakeEventBus.trigger).not.toHaveBeenCalledWith('error');

View File

@ -164,9 +164,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
send: function(data) {
this.params = data;
this.readyState = 2;
this.eventBus.trigger('loadstart');
this.eventBus.trigger('readystatechange');
var stub = stubTracker.findStub(this.url, data, this.method);
if (stub) {
@ -237,12 +235,16 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
if (this.readyState === 4) {
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.eventBus.trigger('readystatechange');
this.responseText = response.responseText || "";
this.responseType = response.responseType || "";
this.readyState = 4;
this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType);
this.responseXML = getResponseXml(response.responseText, this.getResponseHeader('content-type') || '');
if (this.responseXML) {
this.responseType = 'document';