Skip to content

Commit

Permalink
fix respond() & readystatechange process flow (#184)
Browse files Browse the repository at this point in the history
* codejedi365-fix-readystatechange-flow > add test

### Rationale
Added issue replication test for issue #165 where readystatechange
event is occuring after loadend event occurs.

* codejedi365-fix-readystatechange-flow > resolved

### Rationale
The code was using fall through logic which caused the extra
readystatechange event to be thrown after the loadend event.  The logic
was there to delinate from a finished XHR request and throw the loadend
but not to stop processing. Note that a readystatechange did have to be
added prior to the loadend otherwise some requests would not resolve
and it was noticeable through other unit-tests. This commit has all
passing tests to include the one designed to replicate the issue of

Resolves #165

* codejedi365-fix-readystatechange-flow > improve codestyle

### Rationale
Ensure logic is clear by avoiding empty return statements and fall-through logic
  • Loading branch information
codejedi365 committed May 21, 2021
1 parent dfc05c2 commit 70ca4ca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/fake-xhr/index.js
Expand Up @@ -513,8 +513,6 @@ function fakeXMLHttpRequestFor(globalScope) {
false,
this
);
var event, progress;

if (typeof this.onreadystatechange === "function") {
try {
this.onreadystatechange(readyStateChangeEvent);
Expand All @@ -523,7 +521,11 @@ function fakeXMLHttpRequestFor(globalScope) {
}
}

if (this.readyState === FakeXMLHttpRequest.DONE) {
if (this.readyState !== FakeXMLHttpRequest.DONE) {
this.dispatchEvent(readyStateChangeEvent);
} else {
var event, progress;

if (this.timedOut || this.aborted || this.status === 0) {
progress = { loaded: 0, total: 0 };
event =
Expand Down Expand Up @@ -553,12 +555,11 @@ function fakeXMLHttpRequestFor(globalScope) {
this.dispatchEvent(
new sinonEvent.ProgressEvent(event, progress, this)
);
this.dispatchEvent(readyStateChangeEvent);
this.dispatchEvent(
new sinonEvent.ProgressEvent("loadend", progress, this)
);
}

this.dispatchEvent(readyStateChangeEvent);
},

// Ref https://xhr.spec.whatwg.org/#the-setrequestheader()-method
Expand Down
23 changes: 23 additions & 0 deletions lib/fake-xhr/index.test.js
Expand Up @@ -1212,6 +1212,29 @@ describe("FakeXMLHttpRequest", function() {
assert.equals(status, 204);
assert.equals(statusText, "No Content");
});

it("only performs readystatechange events prior to loadend event during xhr response", function() {
var eventLog = [];
this.xhr.addEventListener("readystatechange", function() {
eventLog.push({
event: "readystatechange",
timestamp: Date.now()
});
});
this.xhr.addEventListener("loadend", function() {
eventLog.push({ event: "loadend", timestamp: Date.now() });
});
this.xhr.respond(200);

// Loadend is last event
assert.equals(eventLog[eventLog.length - 1].event, "loadend");
// Only 1 loadend event occurs
assert.equals(
eventLog.filter(item => item.event !== "readystatechange")
.length,
1
);
});
});

describe(".getResponseHeader", function() {
Expand Down

0 comments on commit 70ca4ca

Please sign in to comment.