Skip to content

Commit

Permalink
Merge pull request sinonjs#1312 from Boshen/fix-ajax-upload-event-lis…
Browse files Browse the repository at this point in the history
…teners

Fix xhr UploadProgress.eventListerners to handle "on" + event
  • Loading branch information
fatso83 authored Mar 5, 2017
2 parents 421c869 + d2292d6 commit e0cfccc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 52 deletions.
61 changes: 15 additions & 46 deletions lib/sinon/util/fake_xml_http_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,76 +58,45 @@ var unsafeHeaders = {
"Via": true
};

// An upload object is created for each
// FakeXMLHttpRequest and allows upload
// events to be simulated using uploadProgress
// and uploadError.
function UploadProgress() {
this.eventListeners = {
abort: [],
error: [],
load: [],
loadend: [],
progress: []
};
}

UploadProgress.prototype.addEventListener = function addEventListener(event, listener) {
this.eventListeners[event].push(listener);
};
function EventTargetHandler() {
var self = this;
var events = ["loadstart", "progress", "abort", "error", "load", "timeout", "loadend"];

UploadProgress.prototype.removeEventListener = function removeEventListener(event, listener) {
var listeners = this.eventListeners[event] || [];
var index = listeners.indexOf(listener);
function addEventListener(eventName) {
self.addEventListener(eventName, function (event) {
var listener = self["on" + eventName];

if (index === -1) {
return;
if (listener && typeof listener === "function") {
listener.call(this, event);
}
});
}

listeners.splice(index, 1);
};
events.forEach(addEventListener);
}

UploadProgress.prototype.dispatchEvent = function dispatchEvent(event) {
var listeners = this.eventListeners[event.type] || [];

listeners.forEach(function (listener) {
listener(event);
});
};
EventTargetHandler.prototype = sinonEvent.EventTarget;

// Note that for FakeXMLHttpRequest to work pre ES5
// we lose some of the alignment with the spec.
// To ensure as close a match as possible,
// set responseType before calling open, send or respond;
function FakeXMLHttpRequest(config) {
EventTargetHandler.call(this);
this.readyState = FakeXMLHttpRequest.UNSENT;
this.requestHeaders = {};
this.requestBody = null;
this.status = 0;
this.statusText = "";
this.upload = new UploadProgress();
this.upload = new EventTargetHandler();
this.responseType = "";
this.response = "";
this.logError = configureLogError(config);
if (sinonXhr.supportsCORS) {
this.withCredentials = false;
}

var xhr = this;
var events = ["loadstart", "load", "abort", "error", "loadend", "progress"];

function addEventListener(eventName) {
xhr.addEventListener(eventName, function (event) {
var listener = xhr["on" + eventName];

if (listener && typeof listener === "function") {
listener.call(this, event);
}
});
}

events.forEach(addEventListener);

if (typeof FakeXMLHttpRequest.onCreate === "function") {
FakeXMLHttpRequest.onCreate(this);
}
Expand Down
19 changes: 13 additions & 6 deletions test/util/fake-xml-http-request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2201,6 +2201,19 @@ if (typeof window !== "undefined") {
loaded: 20
});
});

it("calls .onprogress", function (done) {
this.xhr.upload.onprogress = function (e) {
assert.equals(e.total, 100);
assert.equals(e.loaded, 20);
assert.isTrue(e.lengthComputable);
done();
};
this.xhr.uploadProgress({
total: 100,
loaded: 20
});
});
}

it("triggers 'load' event on success", function (done) {
Expand Down Expand Up @@ -2302,12 +2315,6 @@ if (typeof window !== "undefined") {
});
}

it("event listeners can be removed", function () {
var callback = function () {};
this.xhr.upload.addEventListener("load", callback);
this.xhr.upload.removeEventListener("load", callback);
assert.equals(this.xhr.upload.eventListeners.load.length, 0);
});
});
});
}

0 comments on commit e0cfccc

Please sign in to comment.