node/test/parallel/test-eventtarget-whatwg-passive.js
Anna Henningsen eee522ac29 lib: add EventTarget-related browser globals
Add

- Event
- EventTarget
- MessagePort
- MessageChannel
- MessageEvent

to the set of global objects, since they are available now and behave
like they do in the browser.

Fixes: https://github.com/nodejs/node/issues/35495

PR-URL: https://github.com/nodejs/node/pull/35496
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Shelley Vohr <codebytere@gmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
2020-10-11 14:35:17 +00:00

64 lines
1.8 KiB
JavaScript

'use strict';
const common = require('../common');
const {
fail,
ok,
strictEqual
} = require('assert');
// Manually ported from WPT AddEventListenerOptions-passive.html
{
const document = new EventTarget();
let supportsPassive = false;
const query_options = {
get passive() {
supportsPassive = true;
return false;
},
get dummy() {
fail('dummy value getter invoked');
return false;
}
};
document.addEventListener('test_event', null, query_options);
ok(supportsPassive);
supportsPassive = false;
document.removeEventListener('test_event', null, query_options);
strictEqual(supportsPassive, false);
}
{
function testPassiveValue(optionsValue, expectedDefaultPrevented) {
const document = new EventTarget();
let defaultPrevented;
function handler(e) {
if (e.defaultPrevented) {
fail('Event prematurely marked defaultPrevented');
}
e.preventDefault();
defaultPrevented = e.defaultPrevented;
}
document.addEventListener('test', handler, optionsValue);
// TODO the WHATWG test is more extensive here and tests dispatching on
// document.body, if we ever support getParent we should amend this
const ev = new Event('test', { bubbles: true, cancelable: true });
const uncanceled = document.dispatchEvent(ev);
strictEqual(defaultPrevented, expectedDefaultPrevented);
strictEqual(uncanceled, !expectedDefaultPrevented);
document.removeEventListener('test', handler, optionsValue);
}
testPassiveValue(undefined, true);
testPassiveValue({}, true);
testPassiveValue({ passive: false }, true);
common.skip('TODO: passive listeners is still broken');
testPassiveValue({ passive: 1 }, false);
testPassiveValue({ passive: true }, false);
testPassiveValue({ passive: 0 }, true);
}