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

lib, test: integrate wpt abort_controller tests and fix onabort event #35869

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 2 additions & 3 deletions lib/internal/abort_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
} = primordials;

const {
defineEventHandler,
EventTarget,
Event,
kTrustEvent
Expand Down Expand Up @@ -53,9 +54,6 @@ function abortSignal(signal) {
const event = new Event('abort', {
[kTrustEvent]: true
});
if (typeof signal.onabort === 'function') {
signal.onabort(event);
}
signal.dispatchEvent(event);
}

Expand All @@ -67,6 +65,7 @@ class AbortController {
constructor() {
this[kSignal] = new AbortSignal();
emitExperimentalWarning('AbortController');
defineEventHandler(this[kSignal], 'abort');
}

get signal() { return this[kSignal]; }
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,13 @@ class EventTarget {
}
return event;
};
if (event !== undefined)
watilde marked this conversation as resolved.
Show resolved Hide resolved
event[kTarget] = this;

const root = this[kEvents].get(type);
if (root === undefined || root.next === undefined)
return true;

if (event !== undefined)
event[kTarget] = this;

let handler = root.next;
let next;

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/wpt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Last update:
- html/webappapis/timers: https://github.com/web-platform-tests/wpt/tree/264f12bc7b/html/webappapis/timers
- hr-time: https://github.com/web-platform-tests/wpt/tree/a5d1774ecf/hr-time
- common: https://github.com/web-platform-tests/wpt/tree/4dacb6e2ff/common
- dom/abort: https://github.com/web-platform-tests/wpt/tree/7caa3de747/dom/abort

[Web Platform Tests]: https://github.com/web-platform-tests/wpt
[`git node wpt`]: https://github.com/nodejs/node-core-utils/blob/master/docs/git-node.md#git-node-wpt
67 changes: 67 additions & 0 deletions test/fixtures/wpt/dom/abort/event.any.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
test(t => {
const c = new AbortController(),
s = c.signal;
let state = "begin";

assert_false(s.aborted);

s.addEventListener("abort",
t.step_func(e => {
assert_equals(state, "begin");
state = "aborted";
})
);
c.abort();

assert_equals(state, "aborted");
assert_true(s.aborted);

c.abort();
}, "AbortController abort() should fire event synchronously");

test(t => {
const controller = new AbortController();
const signal = controller.signal;
assert_equals(controller.signal, signal,
"value of controller.signal should not have changed");
controller.abort();
assert_equals(controller.signal, signal,
"value of controller.signal should still not have changed");
}, "controller.signal should always return the same object");

test(t => {
const controller = new AbortController();
const signal = controller.signal;
let eventCount = 0;
signal.onabort = () => {
++eventCount;
};
controller.abort();
assert_true(signal.aborted);
assert_equals(eventCount, 1, "event handler should have been called once");
controller.abort();
assert_true(signal.aborted);
assert_equals(eventCount, 1,
"event handler should not have been called again");
}, "controller.abort() should do nothing the second time it is called");

test(t => {
const controller = new AbortController();
controller.abort();
controller.signal.onabort =
t.unreached_func("event handler should not be called");
}, "event handler should not be called if added after controller.abort()");

test(t => {
const controller = new AbortController();
const signal = controller.signal;
signal.onabort = t.step_func(e => {
assert_equals(e.type, "abort", "event type should be abort");
assert_equals(e.target, signal, "event target should be signal");
assert_false(e.bubbles, "event should not bubble");
assert_true(e.isTrusted, "event should be trusted");
});
controller.abort();
}, "the abort event should have the right properties");

done();
6 changes: 5 additions & 1 deletion test/fixtures/wpt/versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@
"common": {
"commit": "4dacb6e2ff8dbabbe328fde2d8c75491598e4c10",
"path": "common"
},
"dom/abort": {
"commit": "7caa3de7471cf19b78ee9efa313c7341a462b5e3",
"path": "dom/abort"
}
}
}
6 changes: 6 additions & 0 deletions test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,9 @@ let asyncTest = Promise.resolve();
throws(() => eventTarget.addEventListener('foo', Symbol()), TypeError);
});
}
{
const eventTarget = new EventTarget();
const event = new Event('foo');
eventTarget.dispatchEvent(event);
strictEqual(event.target, eventTarget);
}
1 change: 1 addition & 0 deletions test/wpt/status/dom/abort.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
7 changes: 7 additions & 0 deletions test/wpt/test-abort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';
require('../common');
const { WPTRunner } = require('../common/wpt');

const runner = new WPTRunner('dom/abort');

runner.runJsTests();