Skip to content

Commit 1cdfaa8

Browse files
benjamingrtargos
authored andcommitted
events: add a few tests
PR-URL: #35806 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent d3f1cde commit 1cdfaa8

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎lib/internal/event_target.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ const isTrusted = ObjectGetOwnPropertyDescriptor({
7272
},'isTrusted').get;
7373

7474
classEvent{
75-
constructor(type,options){
75+
constructor(type,options=null){
7676
if(arguments.length===0)
7777
thrownewERR_MISSING_ARGS('type');
78-
if(options!=null)
78+
if(options!==null)
7979
validateObject(options,'options');
8080
const{ cancelable, bubbles, composed }={ ...options};
8181
this[kCancelable]=!!cancelable;

‎test/parallel/test-eventtarget.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ let asyncTest = Promise.resolve();
414414
}
415415

416416
{
417+
// Event Statics
417418
strictEqual(Event.NONE,0);
418419
strictEqual(Event.CAPTURING_PHASE,1);
419420
strictEqual(Event.AT_TARGET,2);
@@ -424,6 +425,8 @@ let asyncTest = Promise.resolve();
424425
strictEqual(e.eventPhase,Event.AT_TARGET);
425426
}),{once: true});
426427
target.dispatchEvent(newEvent('foo'));
428+
// Event is a function
429+
strictEqual(Event.length,1);
427430
}
428431

429432
{
@@ -485,3 +488,32 @@ let asyncTest = Promise.resolve();
485488
eventTarget.dispatchEvent(event);
486489
strictEqual(event.target,eventTarget);
487490
}
491+
{
492+
// Event target exported keys
493+
consteventTarget=newEventTarget();
494+
deepStrictEqual(Object.keys(eventTarget),[]);
495+
deepStrictEqual(Object.getOwnPropertyNames(eventTarget),[]);
496+
constparentKeys=Object.keys(Object.getPrototypeOf(eventTarget)).sort();
497+
constkeys=['addEventListener','dispatchEvent','removeEventListener'];
498+
deepStrictEqual(parentKeys,keys);
499+
}
500+
{
501+
// Subclassing
502+
classSubTargetextendsEventTarget{}
503+
consttarget=newSubTarget();
504+
target.addEventListener('foo',common.mustCall());
505+
target.dispatchEvent(newEvent('foo'));
506+
}
507+
{
508+
// Test event order
509+
consttarget=newEventTarget();
510+
letstate=0;
511+
target.addEventListener('foo',common.mustCall(()=>{
512+
strictEqual(state,0);
513+
state++;
514+
}));
515+
target.addEventListener('foo',common.mustCall(()=>{
516+
strictEqual(state,1);
517+
}));
518+
target.dispatchEvent(newEvent('foo'));
519+
}

0 commit comments

Comments
 (0)