Skip to content

Commit

Permalink
Event listeners for progress, load and readystatechange in the …
Browse files Browse the repository at this point in the history
…`readyStateChange` function in `FakeXMLHttpRequest` are dispatched in a different order in comparison to a browser. Reorder the events dispatched to reflect general browser behaviour.
  • Loading branch information
Gavin Boulton committed Apr 8, 2015
1 parent 2b55848 commit 435a888
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/sinon/util/fake_xml_http_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,19 +367,19 @@
}
}

this.dispatchEvent(new sinon.Event("readystatechange"));

switch (this.readyState) {
case FakeXMLHttpRequest.DONE:
this.dispatchEvent(new sinon.Event("load", false, false, this));
this.dispatchEvent(new sinon.Event("loadend", false, false, this));
this.upload.dispatchEvent(new sinon.Event("load", false, false, this));
if (supportsProgress) {
this.upload.dispatchEvent(new sinon.ProgressEvent("progress", {loaded: 100, total: 100}));
this.dispatchEvent(new sinon.ProgressEvent("progress", {loaded: 100, total: 100}));
}
this.upload.dispatchEvent(new sinon.Event("load", false, false, this));
this.dispatchEvent(new sinon.Event("load", false, false, this));
this.dispatchEvent(new sinon.Event("loadend", false, false, this));
break;
}

this.dispatchEvent(new sinon.Event("readystatechange"));
},

setRequestHeader: function setRequestHeader(header, value) {
Expand Down
22 changes: 22 additions & 0 deletions test/sinon/util/fake_xml_http_request_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,28 @@
this.xhr.respond(200, {}, "");
},

"fires events in an order similar to a browser": function (done) {
var xhr = this.xhr,
events = [];

this.xhr.upload.addEventListener("progress", function (e) {
events.push(e.type);
});
this.xhr.upload.addEventListener("load", function (e) {
events.push(e.type);
});
this.xhr.addEventListener("readystatechange", function (e) {
if (xhr.readyState === 4) {
events.push(e.type);
assert.equals(events, ["progress", "load", "readystatechange"]);
done();
}
});

this.xhr.send();
this.xhr.respond(200, {}, "");
},

"calls 'abort' on cancel": function (done) {
var xhr = this.xhr;

Expand Down

0 comments on commit 435a888

Please sign in to comment.