From f8b6a1a302b1307089543c15463d0d1fed65ea78 Mon Sep 17 00:00:00 2001 From: Feng Yu Date: Sun, 17 Jul 2022 20:06:46 +0800 Subject: [PATCH] test: clean up whatwg events test --- ...-events-add-event-listener-options-once.js | 132 --------------- ...ents-add-event-listener-options-passive.js | 65 ------- ...vents-add-event-listener-options-signal.js | 159 ------------------ 3 files changed, 356 deletions(-) delete mode 100644 test/parallel/test-whatwg-events-add-event-listener-options-once.js delete mode 100644 test/parallel/test-whatwg-events-add-event-listener-options-passive.js delete mode 100644 test/parallel/test-whatwg-events-add-event-listener-options-signal.js diff --git a/test/parallel/test-whatwg-events-add-event-listener-options-once.js b/test/parallel/test-whatwg-events-add-event-listener-options-once.js deleted file mode 100644 index 9c74829d761306..00000000000000 --- a/test/parallel/test-whatwg-events-add-event-listener-options-once.js +++ /dev/null @@ -1,132 +0,0 @@ -'use strict'; - -const common = require('../common'); - -const { - strictEqual, -} = require('assert'); - -// Manually ported from: wpt@dom/events/AddEventListenerOptions-once.html -{ - const document = new EventTarget(); - let invoked_once = false; - let invoked_normal = false; - function handler_once() { - invoked_once = true; - } - - function handler_normal() { - invoked_normal = true; - } - - document.addEventListener('test', handler_once, { once: true }); - document.addEventListener('test', handler_normal); - document.dispatchEvent(new Event('test')); - strictEqual(invoked_once, true, 'Once handler should be invoked'); - strictEqual(invoked_normal, true, 'Normal handler should be invoked'); - - invoked_once = false; - invoked_normal = false; - document.dispatchEvent(new Event('test')); - strictEqual(invoked_once, false, 'Once handler shouldn\'t be invoked again'); - strictEqual(invoked_normal, true, 'Normal handler should be invoked again'); - document.removeEventListener('test', handler_normal); -} - - -{ - // Manually ported from AddEventListenerOptions-once.html - const document = new EventTarget(); - let invoked_count = 0; - function handler() { - invoked_count++; - } - document.addEventListener('test', handler, { once: true }); - document.addEventListener('test', handler); - document.dispatchEvent(new Event('test')); - strictEqual(invoked_count, 1, 'The handler should only be added once'); - - invoked_count = 0; - document.dispatchEvent(new Event('test')); - strictEqual(invoked_count, 0, 'The handler was added as a once listener'); - - invoked_count = 0; - document.addEventListener('test', handler, { once: true }); - document.removeEventListener('test', handler); - document.dispatchEvent(new Event('test')); - strictEqual(invoked_count, 0, 'The handler should have been removed'); -} - -{ - // TODO(benjamingr) fix EventTarget recursion - common.skip('EventTarget recursion is currently broken'); - const document = new EventTarget(); - let invoked_count = 0; - function handler() { - invoked_count++; - if (invoked_count === 1) - document.dispatchEvent(new Event('test')); - } - document.addEventListener('test', handler, { once: true }); - document.dispatchEvent(new Event('test')); - strictEqual(invoked_count, 1, 'Once handler should only be invoked once'); - - invoked_count = 0; - function handler2() { - invoked_count++; - if (invoked_count === 1) - document.addEventListener('test', handler2, { once: true }); - if (invoked_count <= 2) - document.dispatchEvent(new Event('test')); - } - document.addEventListener('test', handler2, { once: true }); - document.dispatchEvent(new Event('test')); - strictEqual(invoked_count, 2, 'Once handler should only be invoked once'); -} - -// Manually converted from https://github.com/web-platform-tests/wpt/blob/master/dom/events/AddEventListenerOptions-once.html -// in order to define the `document` ourselves - -{ - const document = new EventTarget(); - - // Should only fire for first event - document.addEventListener('test', common.mustCall(1), { once: true }); - // Should fire for both events - document.addEventListener('test', common.mustCall(2)); - // Fire events - document.dispatchEvent(new Event('test')); - document.dispatchEvent(new Event('test')); -} -{ - const document = new EventTarget(); - - const handler = common.mustCall(2); - // Both should only fire on first event - document.addEventListener('test', handler.bind(), { once: true }); - document.addEventListener('test', handler.bind(), { once: true }); - // Fire events - document.dispatchEvent(new Event('test')); - document.dispatchEvent(new Event('test')); -} -{ - const document = new EventTarget(); - - const handler = common.mustCall(2); - - // Should only fire once on first event - document.addEventListener('test', common.mustCall(1), { once: true }); - // Should fire twice until removed - document.addEventListener('test', handler); - // Fire two events - document.dispatchEvent(new Event('test')); - document.dispatchEvent(new Event('test')); - - // Should only fire once on the next event - document.addEventListener('test', common.mustCall(1), { once: true }); - // The previous handler should no longer fire - document.removeEventListener('test', handler); - - // Fire final event triggering - document.dispatchEvent(new Event('test')); -} diff --git a/test/parallel/test-whatwg-events-add-event-listener-options-passive.js b/test/parallel/test-whatwg-events-add-event-listener-options-passive.js deleted file mode 100644 index 97984bd9aff828..00000000000000 --- a/test/parallel/test-whatwg-events-add-event-listener-options-passive.js +++ /dev/null @@ -1,65 +0,0 @@ -'use strict'; - -const common = require('../common'); - -// Manually converted from https://github.com/web-platform-tests/wpt/blob/master/dom/events/AddEventListenerOptions-passive.html -// in order to define the `document` ourselves - -const { - fail, - ok, - strictEqual -} = require('assert'); - -{ - 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); -} diff --git a/test/parallel/test-whatwg-events-add-event-listener-options-signal.js b/test/parallel/test-whatwg-events-add-event-listener-options-signal.js deleted file mode 100644 index dfe5810180d90e..00000000000000 --- a/test/parallel/test-whatwg-events-add-event-listener-options-signal.js +++ /dev/null @@ -1,159 +0,0 @@ -'use strict'; - -require('../common'); - -const { - strictEqual, -} = require('assert'); - -// Manually ported from: wpt@dom/events/AddEventListenerOptions-signal.any.js - -{ - // Passing an AbortSignal to addEventListener does not prevent - // removeEventListener - let count = 0; - function handler() { - count++; - } - const et = new EventTarget(); - const controller = new AbortController(); - et.addEventListener('test', handler, { signal: controller.signal }); - et.dispatchEvent(new Event('test')); - strictEqual(count, 1, 'Adding a signal still adds a listener'); - et.dispatchEvent(new Event('test')); - strictEqual(count, 2, 'The listener was not added with the once flag'); - controller.abort(); - et.dispatchEvent(new Event('test')); - strictEqual(count, 2, 'Aborting on the controller removes the listener'); - // See: https://github.com/nodejs/node/pull/37696 , adding an event listener - // should always return undefined. - strictEqual( - et.addEventListener('test', handler, { signal: controller.signal }), - undefined); - et.dispatchEvent(new Event('test')); - strictEqual(count, 2, 'Passing an aborted signal never adds the handler'); -} - -{ - // Passing an AbortSignal to addEventListener works with the once flag - let count = 0; - function handler() { - count++; - } - const et = new EventTarget(); - const controller = new AbortController(); - et.addEventListener('test', handler, { signal: controller.signal }); - et.removeEventListener('test', handler); - et.dispatchEvent(new Event('test')); - strictEqual(count, 0, 'The listener was still removed'); -} - -{ - // Removing a once listener works with a passed signal - let count = 0; - function handler() { - count++; - } - const et = new EventTarget(); - const controller = new AbortController(); - const options = { signal: controller.signal, once: true }; - et.addEventListener('test', handler, options); - controller.abort(); - et.dispatchEvent(new Event('test')); - strictEqual(count, 0, 'The listener was still removed'); -} - -{ - let count = 0; - function handler() { - count++; - } - const et = new EventTarget(); - const controller = new AbortController(); - const options = { signal: controller.signal, once: true }; - et.addEventListener('test', handler, options); - et.removeEventListener('test', handler); - et.dispatchEvent(new Event('test')); - strictEqual(count, 0, 'The listener was still removed'); -} - -{ - // Passing an AbortSignal to multiple listeners - let count = 0; - function handler() { - count++; - } - const et = new EventTarget(); - const controller = new AbortController(); - const options = { signal: controller.signal, once: true }; - et.addEventListener('first', handler, options); - et.addEventListener('second', handler, options); - controller.abort(); - et.dispatchEvent(new Event('first')); - et.dispatchEvent(new Event('second')); - strictEqual(count, 0, 'The listener was still removed'); -} - -{ - // Passing an AbortSignal to addEventListener works with the capture flag - let count = 0; - function handler() { - count++; - } - const et = new EventTarget(); - const controller = new AbortController(); - const options = { signal: controller.signal, capture: true }; - et.addEventListener('test', handler, options); - controller.abort(); - et.dispatchEvent(new Event('test')); - strictEqual(count, 0, 'The listener was still removed'); -} - -{ - // Aborting from a listener does not call future listeners - let count = 0; - function handler() { - count++; - } - const et = new EventTarget(); - const controller = new AbortController(); - const options = { signal: controller.signal }; - et.addEventListener('test', () => { - controller.abort(); - }, options); - et.addEventListener('test', handler, options); - et.dispatchEvent(new Event('test')); - strictEqual(count, 0, 'The listener was still removed'); -} - -{ - // Adding then aborting a listener in another listener does not call it - let count = 0; - function handler() { - count++; - } - const et = new EventTarget(); - const controller = new AbortController(); - et.addEventListener('test', () => { - et.addEventListener('test', handler, { signal: controller.signal }); - controller.abort(); - }, { signal: controller.signal }); - et.dispatchEvent(new Event('test')); - strictEqual(count, 0, 'The listener was still removed'); -} - -{ - // Aborting from a nested listener should remove it - const et = new EventTarget(); - const ac = new AbortController(); - let count = 0; - et.addEventListener('foo', () => { - et.addEventListener('foo', () => { - count++; - if (count > 5) ac.abort(); - et.dispatchEvent(new Event('foo')); - }, { signal: ac.signal }); - et.dispatchEvent(new Event('foo')); - }, { once: true }); - et.dispatchEvent(new Event('foo')); -}