diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2020-03-05 08:36:13 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-05 08:36:13 -0500 |
commit | c850b258b4e2487b0456a95cd6b65c169ffb9de2 (patch) | |
tree | d631f6995c048b0c7cddb600d4f33297922ba5b1 /cli/js/event_target_test.ts | |
parent | 54a1688868810af0ef16f5c186dfb839f2fce23f (diff) |
Support async function and EventListenerObject as listeners (#4240)
Diffstat (limited to 'cli/js/event_target_test.ts')
-rw-r--r-- | cli/js/event_target_test.ts | 79 |
1 files changed, 77 insertions, 2 deletions
diff --git a/cli/js/event_target_test.ts b/cli/js/event_target_test.ts index af7895081..376c96dd0 100644 --- a/cli/js/event_target_test.ts +++ b/cli/js/event_target_test.ts @@ -39,7 +39,7 @@ unitTest(function anEventTargetCanBeSubclassed(): void { class NicerEventTarget extends EventTarget { on( type: string, - callback: (e: Event) => void | null, + callback: ((e: Event) => void) | null, options?: __domTypes.AddEventListenerOptions ): void { this.addEventListener(type, callback, options); @@ -47,7 +47,7 @@ unitTest(function anEventTargetCanBeSubclassed(): void { off( type: string, - callback: (e: Event) => void | null, + callback: ((e: Event) => void) | null, options?: __domTypes.EventListenerOptions ): void { this.removeEventListener(type, callback, options); @@ -154,3 +154,78 @@ unitTest(function eventTargetThisShouldDefaultToWindow(): void { dispatchEvent(event); assertEquals(n, 1); }); + +test(function eventTargetShouldAcceptEventListenerObject(): void { + const target = new EventTarget(); + const event = new Event("foo", { bubbles: true, cancelable: false }); + let callCount = 0; + + const listener = { + handleEvent(e: Event): void { + assertEquals(e, event); + ++callCount; + } + }; + + target.addEventListener("foo", listener); + + target.dispatchEvent(event); + assertEquals(callCount, 1); + + target.dispatchEvent(event); + assertEquals(callCount, 2); + + target.removeEventListener("foo", listener); + target.dispatchEvent(event); + assertEquals(callCount, 2); +}); + +test(function eventTargetShouldAcceptAsyncFunction(): void { + const target = new EventTarget(); + const event = new Event("foo", { bubbles: true, cancelable: false }); + let callCount = 0; + + const listener = async (e: Event): Promise<void> => { + assertEquals(e, event); + ++callCount; + }; + + target.addEventListener("foo", listener); + + target.dispatchEvent(event); + assertEquals(callCount, 1); + + target.dispatchEvent(event); + assertEquals(callCount, 2); + + target.removeEventListener("foo", listener); + target.dispatchEvent(event); + assertEquals(callCount, 2); +}); + +test( + function eventTargetShouldAcceptAsyncFunctionForEventListenerObject(): void { + const target = new EventTarget(); + const event = new Event("foo", { bubbles: true, cancelable: false }); + let callCount = 0; + + const listener = { + async handleEvent(e: Event): Promise<void> { + assertEquals(e, event); + ++callCount; + } + }; + + target.addEventListener("foo", listener); + + target.dispatchEvent(event); + assertEquals(callCount, 1); + + target.dispatchEvent(event); + assertEquals(callCount, 2); + + target.removeEventListener("foo", listener); + target.dispatchEvent(event); + assertEquals(callCount, 2); + } +); |