Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure target and currentTarget properties are set on events #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions fake_xml_http_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
this.bubbles = bubbles;
this.cancelable = cancelable;
this.target = target;
this.currentTarget = target;
};

_Event.prototype = {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
8 changes: 4 additions & 4 deletions src/fake-xml-http-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
16 changes: 16 additions & 0 deletions test/responding_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand All @@ -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;
};
Expand All @@ -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(){
Expand Down
3 changes: 3 additions & 0 deletions test/upload_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});