From 388da6f700e4ffd614c04b1bd78bfa97912e1a11 Mon Sep 17 00:00:00 2001 From: Andy Stanford-Bluntish Date: Tue, 1 Aug 2017 10:31:24 +1000 Subject: [PATCH 1/2] Set currentTarget on readystatechange events When listening for events (e.g. readystatechange) some libraries expect `event.currentTarget` to be set to the XHR, as well as `event.target`. Since the `readystatechange` event doesn't bubble, the `target` and `currentTarget` will always be the same. --- fake_xml_http_request.js | 5 +++-- src/fake-xml-http-request.js | 5 +++-- test/responding_test.js | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/fake_xml_http_request.js b/fake_xml_http_request.js index cf8593d..44b88ad 100644 --- a/fake_xml_http_request.js +++ b/fake_xml_http_request.js @@ -22,6 +22,7 @@ this.bubbles = bubbles; this.cancelable = cancelable; this.target = target; + this.currentTarget = target; }; _Event.prototype = { @@ -382,10 +383,10 @@ this.readyState = state; if (typeof this.onreadystatechange == "function") { - this.onreadystatechange(new _Event("readystatechange")); + this.onreadystatechange(new _Event("readystatechange", false, false, this)); } - this.dispatchEvent(new _Event("readystatechange")); + this.dispatchEvent(new _Event("readystatechange", false, false, this)); if (this.readyState == FakeXMLHttpRequest.DONE) { this.dispatchEvent(new _Event("load", false, false, this)); diff --git a/src/fake-xml-http-request.js b/src/fake-xml-http-request.js index 41b61c9..1c1f204 100644 --- a/src/fake-xml-http-request.js +++ b/src/fake-xml-http-request.js @@ -16,6 +16,7 @@ var _Event = function Event(type, bubbles, cancelable, target) { this.bubbles = bubbles; this.cancelable = cancelable; this.target = target; + this.currentTarget = target; }; _Event.prototype = { @@ -376,10 +377,10 @@ var FakeXMLHttpRequestProto = { this.readyState = state; if (typeof this.onreadystatechange == "function") { - this.onreadystatechange(new _Event("readystatechange")); + this.onreadystatechange(new _Event("readystatechange", false, false, this)); } - this.dispatchEvent(new _Event("readystatechange")); + this.dispatchEvent(new _Event("readystatechange", false, false, this)); if (this.readyState == FakeXMLHttpRequest.DONE) { this.dispatchEvent(new _Event("load", false, false, this)); diff --git a/test/responding_test.js b/test/responding_test.js index ad3af21..1806df9 100644 --- a/test/responding_test.js +++ b/test/responding_test.js @@ -100,6 +100,18 @@ test("passes event target as context to onload", function() { deepEqual(context, event.target); }); +test("event currentTarget matches event target", function() { + var event; + + xhr.onload = function(ev){ + event = ev; + }; + + xhr.respond(200, {}, ""); + + strictEqual(event.currentTarget, event.target); +}); + test("calls onreadystatechange for each state change", function() { var states = []; @@ -122,6 +134,7 @@ test("calls onreadystatechange for each state change", function() { test("passes event to onreadystatechange", function() { var event = null; + xhr.onreadystatechange = function(e) { event = e; }; @@ -130,6 +143,9 @@ test("passes event to onreadystatechange", function() { ok(event && event.type === 'readystatechange', 'passes event with type "readystatechange"'); + strictEqual(xhr, event.target, 'event target is the xhr object'); + strictEqual(event.target, event.currentTarget, + 'event currentTarget matches event target'); }); test("overrideMimeType overrides content-type responseHeader", function(){ From 894c9dd0d0e5d2b37b1baf3a52628dba495ed318 Mon Sep 17 00:00:00 2001 From: Andy Stanford-Bluntish Date: Tue, 6 Mar 2018 15:19:32 +1100 Subject: [PATCH 2/2] Set currentTarget on progress events Set the event target for the progress event via the constructor, for consistency with other events. Also ensures currentTarget is set at the same time. --- fake_xml_http_request.js | 3 +-- src/fake-xml-http-request.js | 3 +-- test/upload_test.js | 3 +++ 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/fake_xml_http_request.js b/fake_xml_http_request.js index 44b88ad..a30a32f 100644 --- a/fake_xml_http_request.js +++ b/fake_xml_http_request.js @@ -192,8 +192,7 @@ Triggers an `onprogress` event with the given parameters. */ _progress: function _progress(lengthComputable, loaded, total) { - var event = new _Event('progress'); - event.target = this; + var event = new _Event("progress", false, false, this); event.lengthComputable = lengthComputable; event.loaded = loaded; event.total = total; diff --git a/src/fake-xml-http-request.js b/src/fake-xml-http-request.js index 1c1f204..34f547d 100644 --- a/src/fake-xml-http-request.js +++ b/src/fake-xml-http-request.js @@ -186,8 +186,7 @@ EventedObject.prototype = { Triggers an `onprogress` event with the given parameters. */ _progress: function _progress(lengthComputable, loaded, total) { - var event = new _Event('progress'); - event.target = this; + var event = new _Event("progress", false, false, this); event.lengthComputable = lengthComputable; event.loaded = loaded; event.total = total; diff --git a/test/upload_test.js b/test/upload_test.js index 1f9f970..69ec7bc 100644 --- a/test/upload_test.js +++ b/test/upload_test.js @@ -22,4 +22,7 @@ test("_progress triggers the onprogress event", function() { ok(event.lengthComputable, "ProgressEvent.lengthComputable"); equal(event.loaded, 10, "ProgressEvent.loaded"); equal(event.total, 100, "ProgressEvent.total"); + strictEqual(event.target, upload, "ProgressEvent.target is the upload object"); + strictEqual(event.target, event.currentTarget, + "ProgressEvent.target matches ProgressEvent.currentTarget"); }); \ No newline at end of file