diff --git a/fake_xml_http_request.js b/fake_xml_http_request.js index cf8593d..a30a32f 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 = { @@ -191,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; @@ -382,10 +382,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..34f547d 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 = { @@ -185,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; @@ -376,10 +376,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(){ 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